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.annotations.GwtCompatible;
23import com.google.common.base.Objects;
24
25import java.util.Map.Entry;
26
27import javax.annotation.Nullable;
28
29/**
30 * A notification of the removal of a single entry. The key and/or value may be null if they were
31 * already garbage collected.
32 *
33 * <p>Like other {@code Map.Entry} instances associated with {@code CacheBuilder}, this class holds
34 * strong references to the key and value, regardless of the type of references the cache may be
35 * using.
36 *
37 * @author Charles Fry
38 * @since 10.0
39 */
40@Beta
41@GwtCompatible
42public final class RemovalNotification<K, V> implements Entry<K, V> {
43  @Nullable private final K key;
44  @Nullable private final V value;
45  private final RemovalCause cause;
46
47  RemovalNotification(@Nullable K key, @Nullable V value, RemovalCause cause) {
48    this.key = key;
49    this.value = value;
50    this.cause = checkNotNull(cause);
51  }
52
53  /**
54   * Returns the cause for which the entry was removed.
55   */
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   */
64  public boolean wasEvicted() {
65    return cause.wasEvicted();
66  }
67
68  @Nullable @Override public K getKey() {
69    return key;
70  }
71
72  @Nullable @Override public V getValue() {
73    return value;
74  }
75
76  @Override public final V setValue(V value) {
77    throw new UnsupportedOperationException();
78  }
79
80  @Override public boolean equals(@Nullable Object object) {
81    if (object instanceof Entry) {
82      Entry<?, ?> that = (Entry<?, ?>) object;
83      return Objects.equal(this.getKey(), that.getKey())
84          && Objects.equal(this.getValue(), that.getValue());
85    }
86    return false;
87  }
88
89  @Override public int hashCode() {
90    K k = getKey();
91    V v = getValue();
92    return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
93  }
94
95  /**
96   * Returns a string representation of the form <code>{key}={value}</code>.
97   */
98  @Override public String toString() {
99    return getKey() + "=" + getValue();
100  }
101  private static final long serialVersionUID = 0;
102}
103