1/*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.cache;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.annotations.Beta;
22import com.google.common.base.Objects;
23
24import java.util.Map.Entry;
25
26import javax.annotation.Nullable;
27
28/**
29 * A notification of the removal of a single entry. The key and/or value may be null if they were
30 * already garbage collected.
31 *
32 * <p>Like other {@code Map.Entry} instances associated with {@code CacheBuilder}, this class holds
33 * strong references to the key and value, regardless of the type of references the cache may be
34 * using.
35 *
36 * @author Charles Fry
37 * @since 10.0
38 */
39@Beta
40public final class RemovalNotification<K, V> implements Entry<K, V> {
41  @Nullable private final K key;
42  @Nullable private final V value;
43  private final RemovalCause cause;
44
45  RemovalNotification(@Nullable K key, @Nullable V value, RemovalCause cause) {
46    this.key = key;
47    this.value = value;
48    this.cause = checkNotNull(cause);
49  }
50
51  /**
52   * Returns the cause for which the entry was removed.
53   */
54  public RemovalCause getCause() {
55    return cause;
56  }
57
58  /**
59   * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
60   * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}).
61   */
62  public boolean wasEvicted() {
63    return cause.wasEvicted();
64  }
65
66  @Nullable @Override public K getKey() {
67    return key;
68  }
69
70  @Nullable @Override public V getValue() {
71    return value;
72  }
73
74  @Override public final V setValue(V value){
75    throw new UnsupportedOperationException();
76  }
77
78  @Override public boolean equals(@Nullable Object object) {
79    if (object instanceof Entry) {
80      Entry<?, ?> that = (Entry<?, ?>) object;
81      return Objects.equal(this.getKey(), that.getKey())
82          && Objects.equal(this.getValue(), that.getValue());
83    }
84    return false;
85  }
86
87  @Override public int hashCode() {
88    K k = getKey();
89    V v = getValue();
90    return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
91  }
92
93  /**
94   * Returns a string representation of the form <code>{key}={value}</code>.
95   */
96  @Override public String toString() {
97    return getKey() + "=" + getValue();
98  }
99  private static final long serialVersionUID = 0;
100}
101