001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.errorprone.annotations.DoNotMock;
021import java.util.Map;
022import org.checkerframework.checker.nullness.compatqual.NullableDecl;
023
024/**
025 * An object representing the differences between two maps.
026 *
027 * @author Kevin Bourrillion
028 * @since 2.0
029 */
030@DoNotMock("Use Maps.difference")
031@GwtCompatible
032public interface MapDifference<K, V> {
033  /**
034   * Returns {@code true} if there are no differences between the two maps; that is, if the maps are
035   * equal.
036   */
037  boolean areEqual();
038
039  /**
040   * Returns an unmodifiable map containing the entries from the left map whose keys are not present
041   * in the right map.
042   */
043  Map<K, V> entriesOnlyOnLeft();
044
045  /**
046   * Returns an unmodifiable map containing the entries from the right map whose keys are not
047   * present in the left map.
048   */
049  Map<K, V> entriesOnlyOnRight();
050
051  /**
052   * Returns an unmodifiable map containing the entries that appear in both maps; that is, the
053   * intersection of the two maps.
054   */
055  Map<K, V> entriesInCommon();
056
057  /**
058   * Returns an unmodifiable map describing keys that appear in both maps, but with different
059   * values.
060   */
061  Map<K, ValueDifference<V>> entriesDiffering();
062
063  /**
064   * Compares the specified object with this instance for equality. Returns {@code true} if the
065   * given object is also a {@code MapDifference} and the values returned by the {@link
066   * #entriesOnlyOnLeft()}, {@link #entriesOnlyOnRight()}, {@link #entriesInCommon()} and {@link
067   * #entriesDiffering()} of the two instances are equal.
068   */
069  @Override
070  boolean equals(@NullableDecl Object object);
071
072  /**
073   * Returns the hash code for this instance. This is defined as the hash code of
074   *
075   * <pre>{@code
076   * Arrays.asList(entriesOnlyOnLeft(), entriesOnlyOnRight(),
077   *     entriesInCommon(), entriesDiffering())
078   * }</pre>
079   */
080  @Override
081  int hashCode();
082
083  /**
084   * A difference between the mappings from two maps with the same key. The {@link #leftValue} and
085   * {@link #rightValue} are not equal, and one but not both of them may be null.
086   *
087   * @since 2.0
088   */
089  @DoNotMock("Use Maps.difference")
090  interface ValueDifference<V> {
091    /** Returns the value from the left map (possibly null). */
092    V leftValue();
093
094    /** Returns the value from the right map (possibly null). */
095    V rightValue();
096
097    /**
098     * Two instances are considered equal if their {@link #leftValue()} values are equal and their
099     * {@link #rightValue()} values are also equal.
100     */
101    @Override
102    boolean equals(@NullableDecl Object other);
103
104    /**
105     * The hash code equals the value {@code Arrays.asList(leftValue(), rightValue()).hashCode()}.
106     */
107    @Override
108    int hashCode();
109  }
110}