001 /*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.cache;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.base.Preconditions;
021 import com.google.common.collect.ForwardingObject;
022 import com.google.common.collect.ImmutableMap;
023
024 import java.util.Map;
025 import java.util.concurrent.Callable;
026 import java.util.concurrent.ConcurrentMap;
027 import java.util.concurrent.ExecutionException;
028
029 import javax.annotation.Nullable;
030
031 /**
032 * A cache which forwards all its method calls to another cache. Subclasses should override one or
033 * more methods to modify the behavior of the backing cache as desired per the
034 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
035 *
036 * @author Charles Fry
037 * @since 10.0
038 */
039 @Beta
040 public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> {
041
042 /** Constructor for use by subclasses. */
043 protected ForwardingCache() {}
044
045
046 @Override
047 protected abstract Cache<K, V> delegate();
048
049 /**
050 * @since 11.0
051 */
052 @Nullable
053 public V getIfPresent(Object key) {
054 return delegate().getIfPresent(key);
055 }
056
057 /**
058 * @since 11.0
059 */
060 public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
061 return delegate().get(key, valueLoader);
062 }
063
064 /**
065 * @since 11.0
066 */
067 public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
068 return delegate().getAllPresent(keys);
069 }
070
071 /**
072 * @since 11.0
073 */
074 public void put(K key, V value) {
075 delegate().put(key, value);
076 }
077
078 /**
079 * @since 12.0
080 */
081 public void putAll(Map<? extends K,? extends V> m) {
082 delegate().putAll(m);
083 }
084
085 public void invalidate(Object key) {
086 delegate().invalidate(key);
087 }
088
089 /**
090 * @since 11.0
091 */
092 public void invalidateAll(Iterable<?> keys) {
093 delegate().invalidateAll(keys);
094 }
095
096 public void invalidateAll() {
097 delegate().invalidateAll();
098 }
099
100 public long size() {
101 return delegate().size();
102 }
103
104 public CacheStats stats() {
105 return delegate().stats();
106 }
107
108 public ConcurrentMap<K, V> asMap() {
109 return delegate().asMap();
110 }
111
112 public void cleanUp() {
113 delegate().cleanUp();
114 }
115
116 /**
117 * A simplified version of {@link ForwardingCache} where subclasses can pass in an already
118 * constructed {@link Cache} as the delegete.
119 *
120 * @since 10.0
121 */
122 @Beta
123 public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> {
124 private final Cache<K, V> delegate;
125
126 protected SimpleForwardingCache(Cache<K, V> delegate) {
127 this.delegate = Preconditions.checkNotNull(delegate);
128 }
129
130
131 @Override
132 protected final Cache<K, V> delegate() {
133 return delegate;
134 }
135 }
136 }