001/* 002 * Copyright (C) 2011 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.cache; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.base.Objects; 023 024import java.util.Map.Entry; 025 026import javax.annotation.Nullable; 027 028/** 029 * A notification of the removal of a single entry. The key and/or value may be null if they were 030 * already garbage collected. 031 * 032 * <p>Like other {@code Map.Entry} instances associated with {@code CacheBuilder}, this class holds 033 * strong references to the key and value, regardless of the type of references the cache may be 034 * using. 035 * 036 * @author Charles Fry 037 * @since 10.0 038 */ 039@GwtCompatible 040public final class RemovalNotification<K, V> implements Entry<K, V> { 041 @Nullable private final K key; 042 @Nullable private final V value; 043 private final RemovalCause cause; 044 045 /** 046 * Creates a new {@code RemovalNotification} for the given {@code key}/{@code value} pair, with 047 * the given {@code cause} for the removal. The {@code key} and/or {@code value} may be 048 * {@code null} if they were already garbage collected. 049 * 050 * @since 19.0 051 */ 052 public static <K, V> RemovalNotification<K, V> create( 053 @Nullable K key, @Nullable V value, RemovalCause cause) { 054 return new RemovalNotification(key, value, cause); 055 } 056 057 private RemovalNotification(@Nullable K key, @Nullable V value, RemovalCause cause) { 058 this.key = key; 059 this.value = value; 060 this.cause = checkNotNull(cause); 061 } 062 063 /** 064 * Returns the cause for which the entry was removed. 065 */ 066 public RemovalCause getCause() { 067 return cause; 068 } 069 070 /** 071 * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither 072 * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}). 073 */ 074 public boolean wasEvicted() { 075 return cause.wasEvicted(); 076 } 077 078 @Nullable @Override public K getKey() { 079 return key; 080 } 081 082 @Nullable @Override public V getValue() { 083 return value; 084 } 085 086 @Override public final V setValue(V value) { 087 throw new UnsupportedOperationException(); 088 } 089 090 @Override public boolean equals(@Nullable Object object) { 091 if (object instanceof Entry) { 092 Entry<?, ?> that = (Entry<?, ?>) object; 093 return Objects.equal(this.getKey(), that.getKey()) 094 && Objects.equal(this.getValue(), that.getValue()); 095 } 096 return false; 097 } 098 099 @Override public int hashCode() { 100 K k = getKey(); 101 V v = getValue(); 102 return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode()); 103 } 104 105 /** 106 * Returns a string representation of the form <code>{key}={value}</code>. 107 */ 108 @Override public String toString() { 109 return getKey() + "=" + getValue(); 110 } 111 private static final long serialVersionUID = 0; 112}