1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.cache; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtCompatible; 20 import java.util.AbstractMap.SimpleImmutableEntry; 21 import org.checkerframework.checker.nullness.compatqual.NullableDecl; 22 23 /** 24 * A notification of the removal of a single entry. The key and/or value may be null if they were 25 * already garbage collected. 26 * 27 * <p>Like other {@code Entry} instances associated with {@code CacheBuilder}, this class holds 28 * strong references to the key and value, regardless of the type of references the cache may be 29 * using. 30 * 31 * @author Charles Fry 32 * @since 10.0 33 */ 34 @GwtCompatible 35 public final class RemovalNotification<K, V> extends SimpleImmutableEntry<K, V> { 36 private final RemovalCause cause; 37 38 /** 39 * Creates a new {@code RemovalNotification} for the given {@code key}/{@code value} pair, with 40 * the given {@code cause} for the removal. The {@code key} and/or {@code value} may be {@code 41 * null} if they were already garbage collected. 42 * 43 * @since 19.0 44 */ create( @ullableDecl K key, @NullableDecl V value, RemovalCause cause)45 public static <K, V> RemovalNotification<K, V> create( 46 @NullableDecl K key, @NullableDecl V value, RemovalCause cause) { 47 return new RemovalNotification(key, value, cause); 48 } 49 RemovalNotification(@ullableDecl K key, @NullableDecl V value, RemovalCause cause)50 private RemovalNotification(@NullableDecl K key, @NullableDecl V value, RemovalCause cause) { 51 super(key, value); 52 this.cause = checkNotNull(cause); 53 } 54 55 /** Returns the cause for which the entry was removed. */ getCause()56 public RemovalCause getCause() { 57 return cause; 58 } 59 60 /** 61 * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither 62 * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}). 63 */ wasEvicted()64 public boolean wasEvicted() { 65 return cause.wasEvicted(); 66 } 67 68 private static final long serialVersionUID = 0; 69 } 70