    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
    017    package com.google.common.cache;
    018
    019    import static com.google.common.base.Preconditions.checkNotNull;
    020
    021    import com.google.common.annotations.Beta;
    022    import com.google.common.base.Objects;
    023
    024    import java.util.Map.Entry;
    025
    026    import 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    @Beta
    040    public 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      RemovalNotification(@Nullable K key, @Nullable V value, RemovalCause cause) {
    046        this.key = key;
    047        this.value = value;
    048        this.cause = checkNotNull(cause);
    049      }
    050
    051      /**
    052       * Returns the cause for which the entry was removed.
    053       */
    054      public RemovalCause getCause() {
    055        return cause;
    056      }
    057
    058      /**
    059       * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
    060       * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}).
    061       */
    062      public boolean wasEvicted() {
    063        return cause.wasEvicted();
    064      }
    065
    066      @Nullable @Override public K getKey() {
    067        return key;
    068      }
    069
    070      @Nullable @Override public V getValue() {
    071        return value;
    072      }
    073
    074      @Override public final V setValue(V value){
    075        throw new UnsupportedOperationException();
    076      }
    077
    078      @Override public boolean equals(@Nullable Object object) {
    079        if (object instanceof Entry) {
    080          Entry<?, ?> that = (Entry<?, ?>) object;
    081          return Objects.equal(this.getKey(), that.getKey())
    082              && Objects.equal(this.getValue(), that.getValue());
    083        }
    084        return false;
    085      }
    086
    087      @Override public int hashCode() {
    088        K k = getKey();
    089        V v = getValue();
    090        return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
    091      }
    092
    093      /**
    094       * Returns a string representation of the form <code>{key}={value}</code>.
    095       */
    096      @Override public String toString() {
    097        return getKey() + "=" + getValue();
    098      }
    099      private static final long serialVersionUID = 0;
    100    }