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