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