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.concurrent.Callable; 025 import java.util.concurrent.ConcurrentMap; 026 import java.util.concurrent.ExecutionException; 027 028 import javax.annotation.Nullable; 029 030 /** 031 * A cache which forwards all its method calls to another cache. Subclasses should override one or 032 * more methods to modify the behavior of the backing cache as desired per the 033 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 034 * 035 * @author Charles Fry 036 * @since 10.0 037 */ 038 @Beta 039 public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> { 040 041 /** Constructor for use by subclasses. */ 042 protected ForwardingCache() {} 043 044 @Override 045 protected abstract Cache<K, V> delegate(); 046 047 /** 048 * @since 11.0 049 */ 050 @Override 051 @Nullable 052 public V getIfPresent(K key) { 053 return delegate().getIfPresent(key); 054 } 055 056 /** 057 * @since 11.0 058 */ 059 @Override 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 @Override 068 public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) { 069 return delegate().getAllPresent(keys); 070 } 071 072 /** 073 * @since 11.0 074 */ 075 @Override 076 public void put(K key, V value) { 077 delegate().put(key, value); 078 } 079 080 @Override 081 public void invalidate(Object key) { 082 delegate().invalidate(key); 083 } 084 085 /** 086 * @since 11.0 087 */ 088 @Override 089 public void invalidateAll(Iterable<?> keys) { 090 delegate().invalidateAll(keys); 091 } 092 093 @Override 094 public void invalidateAll() { 095 delegate().invalidateAll(); 096 } 097 098 @Override 099 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 }