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