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