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