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