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
029@ElementTypesAreNonnullByDefault
030public enum RemovalCause {
031  /**
032   * The entry was manually removed by the user. This can result from the user invoking {@link
033   * Cache#invalidate}, {@link Cache#invalidateAll(Iterable)}, {@link Cache#invalidateAll()}, {@link
034   * Map#remove}, {@link ConcurrentMap#remove}, or {@link Iterator#remove}.
035   */
036  EXPLICIT {
037    @Override
038    boolean wasEvicted() {
039      return false;
040    }
041  },
042
043  /**
044   * The entry itself was not actually removed, but its value was replaced by the user. This can
045   * result from the user invoking {@link Cache#put}, {@link LoadingCache#refresh}, {@link Map#put},
046   * {@link Map#putAll}, {@link ConcurrentMap#replace(Object, Object)}, or {@link
047   * ConcurrentMap#replace(Object, Object, Object)}.
048   */
049  REPLACED {
050    @Override
051    boolean wasEvicted() {
052      return false;
053    }
054  },
055
056  /**
057   * The entry was removed automatically because its key or value was garbage-collected. This can
058   * occur when using {@link CacheBuilder#weakKeys}, {@link CacheBuilder#weakValues}, or {@link
059   * CacheBuilder#softValues}.
060   */
061  COLLECTED {
062    @Override
063    boolean wasEvicted() {
064      return true;
065    }
066  },
067
068  /**
069   * The entry's expiration timestamp has passed. This can occur when using {@link
070   * CacheBuilder#expireAfterWrite} or {@link CacheBuilder#expireAfterAccess}.
071   */
072  EXPIRED {
073    @Override
074    boolean wasEvicted() {
075      return true;
076    }
077  },
078
079  /**
080   * The entry was evicted due to size constraints. This can occur when using {@link
081   * CacheBuilder#maximumSize} or {@link CacheBuilder#maximumWeight}.
082   */
083  SIZE {
084    @Override
085    boolean wasEvicted() {
086      return true;
087    }
088  };
089
090  /**
091   * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
092   * {@link #EXPLICIT} nor {@link #REPLACED}).
093   */
094  abstract boolean wasEvicted();
095}