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.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 javax.annotation.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
30   * <a 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. */
39    protected ForwardingCache() {}
40  
41    @Override
42    protected abstract Cache<K, V> delegate();
43  
44    /**
45     * @since 11.0
46     */
47    @Override
48    @Nullable
49    public V getIfPresent(Object key) {
50      return delegate().getIfPresent(key);
51    }
52  
53    /**
54     * @since 11.0
55     */
56    @Override
57    public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
58      return delegate().get(key, valueLoader);
59    }
60  
61    /**
62     * @since 11.0
63     */
64    @Override
65    public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
66      return delegate().getAllPresent(keys);
67    }
68  
69    /**
70     * @since 11.0
71     */
72    @Override
73    public void put(K key, V value) {
74      delegate().put(key, value);
75    }
76  
77    /**
78     * @since 12.0
79     */
80    @Override
81    public void putAll(Map<? extends K, ? extends V> m) {
82      delegate().putAll(m);
83    }
84  
85    @Override
86    public void invalidate(Object key) {
87      delegate().invalidate(key);
88    }
89  
90    /**
91     * @since 11.0
92     */
93    @Override
94    public void invalidateAll(Iterable<?> keys) {
95      delegate().invalidateAll(keys);
96    }
97  
98    @Override
99    public void invalidateAll() {
100     delegate().invalidateAll();
101   }
102 
103   @Override
104   public long size() {
105     return delegate().size();
106   }
107 
108   @Override
109   public CacheStats stats() {
110     return delegate().stats();
111   }
112 
113   @Override
114   public ConcurrentMap<K, V> asMap() {
115     return delegate().asMap();
116   }
117 
118   @Override
119   public void cleanUp() {
120     delegate().cleanUp();
121   }
122 
123   /**
124    * A simplified version of {@link ForwardingCache} where subclasses can pass in an already
125    * constructed {@link Cache} as the delegate.
126    *
127    * @since 10.0
128    */
129   public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> {
130     private final Cache<K, V> delegate;
131 
132     protected SimpleForwardingCache(Cache<K, V> delegate) {
133       this.delegate = Preconditions.checkNotNull(delegate);
134     }
135 
136     @Override
137     protected final Cache<K, V> delegate() {
138       return delegate;
139     }
140   }
141 }