View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.cache;
16  
17  import com.google.common.annotations.GwtCompatible;
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.concurrent.ConcurrentMap;
21  
22  /**
23   * The reason why a cached entry was removed.
24   *
25   * @author Charles Fry
26   * @since 10.0
27   */
28  @GwtCompatible
29  public enum RemovalCause {
30    /**
31     * The entry was manually removed by the user. This can result from the user invoking
32     * {@link Cache#invalidate}, {@link Cache#invalidateAll(Iterable)}, {@link Cache#invalidateAll()},
33     * {@link Map#remove}, {@link ConcurrentMap#remove}, or {@link Iterator#remove}.
34     */
35    EXPLICIT {
36      @Override
37      boolean wasEvicted() {
38        return false;
39      }
40    },
41  
42    /**
43     * The entry itself was not actually removed, but its value was replaced by the user. This can
44     * result from the user invoking {@link Cache#put}, {@link LoadingCache#refresh}, {@link Map#put},
45     * {@link Map#putAll}, {@link ConcurrentMap#replace(Object, Object)}, or
46     * {@link ConcurrentMap#replace(Object, Object, Object)}.
47     */
48    REPLACED {
49      @Override
50      boolean wasEvicted() {
51        return false;
52      }
53    },
54  
55    /**
56     * The entry was removed automatically because its key or value was garbage-collected. This can
57     * occur when using {@link CacheBuilder#weakKeys}, {@link CacheBuilder#weakValues}, or
58     * {@link CacheBuilder#softValues}.
59     */
60    COLLECTED {
61      @Override
62      boolean wasEvicted() {
63        return true;
64      }
65    },
66  
67    /**
68     * The entry's expiration timestamp has passed. This can occur when using
69     * {@link CacheBuilder#expireAfterWrite} or {@link CacheBuilder#expireAfterAccess}.
70     */
71    EXPIRED {
72      @Override
73      boolean wasEvicted() {
74        return true;
75      }
76    },
77  
78    /**
79     * The entry was evicted due to size constraints. This can occur when using
80     * {@link CacheBuilder#maximumSize} or {@link CacheBuilder#maximumWeight}.
81     */
82    SIZE {
83      @Override
84      boolean wasEvicted() {
85        return true;
86      }
87    };
88  
89    /**
90     * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
91     * {@link #EXPLICIT} nor {@link #REPLACED}).
92     */
93    abstract boolean wasEvicted();
94  }