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 com.google.common.annotations.Beta;
020
021 import java.util.Iterator;
022 import java.util.Map;
023 import 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 @Beta
032 public 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
040 @Override
041 boolean wasEvicted() {
042 return false;
043 }
044 },
045
046 /**
047 * The entry itself was not actually removed, but its value was replaced by the user. This can
048 * result from the user invoking {@link Cache#put}, {@link LoadingCache#refresh}, {@link Map#put},
049 * {@link Map#putAll}, {@link ConcurrentMap#replace(Object, Object)}, or
050 * {@link ConcurrentMap#replace(Object, Object, Object)}.
051 */
052 REPLACED {
053
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
067 @Override
068 boolean wasEvicted() {
069 return true;
070 }
071 },
072
073 /**
074 * The entry's expiration timestamp has passed. This can occur when using
075 * {@link CacheBuilder#expireAfterWrite} or {@link CacheBuilder#expireAfterAccess}.
076 */
077 EXPIRED {
078
079 @Override
080 boolean wasEvicted() {
081 return true;
082 }
083 },
084
085 /**
086 * The entry was evicted due to size constraints. This can occur when using
087 * {@link CacheBuilder#maximumSize} or {@link CacheBuilder#maximumWeight}.
088 */
089 SIZE {
090
091 @Override
092 boolean wasEvicted() {
093 return true;
094 }
095 };
096
097 /**
098 * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
099 * {@link #EXPLICIT} nor {@link #REPLACED}).
100 */
101 abstract boolean wasEvicted();
102 }