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.jspecify.annotations.Nullable; 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 extends @Nullable Object, V extends @Nullable Object> { 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(@Nullable 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 extends @Nullable Object> { 091 /** Returns the value from the left map (possibly null). */ 092 @ParametricNullness 093 V leftValue(); 094 095 /** Returns the value from the right map (possibly null). */ 096 @ParametricNullness 097 V rightValue(); 098 099 /** 100 * Two instances are considered equal if their {@link #leftValue()} values are equal and their 101 * {@link #rightValue()} values are also equal. 102 */ 103 @Override 104 boolean equals(@Nullable Object other); 105 106 /** 107 * The hash code equals the value {@code Arrays.asList(leftValue(), rightValue()).hashCode()}. 108 */ 109 @Override 110 int hashCode(); 111 } 112}