001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.cache;
016
017import com.google.common.annotations.GwtCompatible;
018import java.util.Iterator;
019import java.util.Map;
020import java.util.concurrent.ConcurrentMap;
021
022/**
023 * The reason why a cached entry was removed.
024 *
025 * @author Charles Fry
026 * @since 10.0
027 */
028@GwtCompatible
029public enum RemovalCause {
030  /**
031   * The entry was manually removed by the user. This can result from the user invoking {@link
032   * Cache#invalidate}, {@link Cache#invalidateAll(Iterable)}, {@link Cache#invalidateAll()}, {@link
033   * Map#remove}, {@link ConcurrentMap#remove}, or {@link Iterator#remove}.
034   */
035  EXPLICIT {
036    @Override
037    boolean wasEvicted() {
038      return false;
039    }
040  },
041
042  /**
043   * The entry itself was not actually removed, but its value was replaced by the user. This can
044   * result from the user invoking {@link Cache#put}, {@link LoadingCache#refresh}, {@link Map#put},
045   * {@link Map#putAll}, {@link ConcurrentMap#replace(Object, Object)}, or {@link
046   * ConcurrentMap#replace(Object, Object, Object)}.
047   */
048  REPLACED {
049    @Override
050    boolean wasEvicted() {
051      return false;
052    }
053  },
054
055  /**
056   * The entry was removed automatically because its key or value was garbage-collected. This can
057   * occur when using {@link CacheBuilder#weakKeys}, {@link CacheBuilder#weakValues}, or {@link
058   * CacheBuilder#softValues}.
059   */
060  COLLECTED {
061    @Override
062    boolean wasEvicted() {
063      return true;
064    }
065  },
066
067  /**
068   * The entry's expiration timestamp has passed. This can occur when using {@link
069   * CacheBuilder#expireAfterWrite} or {@link CacheBuilder#expireAfterAccess}.
070   */
071  EXPIRED {
072    @Override
073    boolean wasEvicted() {
074      return true;
075    }
076  },
077
078  /**
079   * The entry was evicted due to size constraints. This can occur when using {@link
080   * CacheBuilder#maximumSize} or {@link CacheBuilder#maximumWeight}.
081   */
082  SIZE {
083    @Override
084    boolean wasEvicted() {
085      return true;
086    }
087  };
088
089  /**
090   * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
091   * {@link #EXPLICIT} nor {@link #REPLACED}).
092   */
093  abstract boolean wasEvicted();
094}