• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GwtIncompatible;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.ForwardingObject;
20 import com.google.common.collect.ImmutableMap;
21 import java.util.Map;
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.ExecutionException;
25 import org.checkerframework.checker.nullness.qual.Nullable;
26 
27 /**
28  * A cache which forwards all its method calls to another cache. Subclasses should override one or
29  * more methods to modify the behavior of the backing cache as desired per the <a
30  * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
31  *
32  * @author Charles Fry
33  * @since 10.0
34  */
35 @GwtIncompatible
36 public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> {
37 
38   /** Constructor for use by subclasses. */
ForwardingCache()39   protected ForwardingCache() {}
40 
41   @Override
delegate()42   protected abstract Cache<K, V> delegate();
43 
44   /** @since 11.0 */
45   @Override
getIfPresent(Object key)46   public @Nullable V getIfPresent(Object key) {
47     return delegate().getIfPresent(key);
48   }
49 
50   /** @since 11.0 */
51   @Override
get(K key, Callable<? extends V> valueLoader)52   public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
53     return delegate().get(key, valueLoader);
54   }
55 
56   /** @since 11.0 */
57   @Override
getAllPresent(Iterable<?> keys)58   public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
59     return delegate().getAllPresent(keys);
60   }
61 
62   /** @since 11.0 */
63   @Override
put(K key, V value)64   public void put(K key, V value) {
65     delegate().put(key, value);
66   }
67 
68   /** @since 12.0 */
69   @Override
putAll(Map<? extends K, ? extends V> m)70   public void putAll(Map<? extends K, ? extends V> m) {
71     delegate().putAll(m);
72   }
73 
74   @Override
invalidate(Object key)75   public void invalidate(Object key) {
76     delegate().invalidate(key);
77   }
78 
79   /** @since 11.0 */
80   @Override
invalidateAll(Iterable<?> keys)81   public void invalidateAll(Iterable<?> keys) {
82     delegate().invalidateAll(keys);
83   }
84 
85   @Override
invalidateAll()86   public void invalidateAll() {
87     delegate().invalidateAll();
88   }
89 
90   @Override
size()91   public long size() {
92     return delegate().size();
93   }
94 
95   @Override
stats()96   public CacheStats stats() {
97     return delegate().stats();
98   }
99 
100   @Override
asMap()101   public ConcurrentMap<K, V> asMap() {
102     return delegate().asMap();
103   }
104 
105   @Override
cleanUp()106   public void cleanUp() {
107     delegate().cleanUp();
108   }
109 
110   /**
111    * A simplified version of {@link ForwardingCache} where subclasses can pass in an already
112    * constructed {@link Cache} as the delegate.
113    *
114    * @since 10.0
115    */
116   public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> {
117     private final Cache<K, V> delegate;
118 
SimpleForwardingCache(Cache<K, V> delegate)119     protected SimpleForwardingCache(Cache<K, V> delegate) {
120       this.delegate = Preconditions.checkNotNull(delegate);
121     }
122 
123     @Override
delegate()124     protected final Cache<K, V> delegate() {
125       return delegate;
126     }
127   }
128 }
129