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 @Override 046 protected abstract Cache<K, V> delegate(); 047 048 /** 049 * @since 11.0 050 */ 051 @Override 052 @Nullable 053 public V getIfPresent(Object key) { 054 return delegate().getIfPresent(key); 055 } 056 057 /** 058 * @since 11.0 059 */ 060 @Override 061 public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException { 062 return delegate().get(key, valueLoader); 063 } 064 065 /** 066 * @since 11.0 067 */ 068 @Override 069 public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) { 070 return delegate().getAllPresent(keys); 071 } 072 073 /** 074 * @since 11.0 075 */ 076 @Override 077 public void put(K key, V value) { 078 delegate().put(key, value); 079 } 080 081 /** 082 * @since 12.0 083 */ 084 @Override 085 public void putAll(Map<? extends K,? extends V> m) { 086 delegate().putAll(m); 087 } 088 089 @Override 090 public void invalidate(Object key) { 091 delegate().invalidate(key); 092 } 093 094 /** 095 * @since 11.0 096 */ 097 @Override 098 public void invalidateAll(Iterable<?> keys) { 099 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 }