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