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.ImmutableList; 023 024 import javax.annotation.Nullable; 025 026 import java.util.Map; 027 import java.util.concurrent.ConcurrentMap; 028 import java.util.concurrent.ExecutionException; 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 * <p>Note that {@link #get}, {@link #getUnchecked}, and {@link #apply} all expose the same 036 * underlying functionality, so should probably be overridden as a group. 037 * 038 * @author Charles Fry 039 * @since 10.0 040 */ 041 @Beta 042 public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> { 043 044 /** Constructor for use by subclasses. */ 045 protected ForwardingCache() {} 046 047 @Override 048 protected abstract Cache<K, V> delegate(); 049 050 @Override 051 @Nullable 052 public V get(@Nullable K key) throws ExecutionException { 053 return delegate().get(key); 054 } 055 056 @Override 057 @Nullable 058 public V getUnchecked(@Nullable K key) { 059 return delegate().getUnchecked(key); 060 } 061 062 @Deprecated 063 @Override 064 @Nullable 065 public V apply(@Nullable K key) { 066 return delegate().apply(key); 067 } 068 069 @Override 070 public void invalidate(@Nullable Object key) { 071 delegate().invalidate(key); 072 } 073 074 @Override 075 public void invalidateAll() { 076 delegate().invalidateAll(); 077 } 078 079 @Override 080 public int size() { 081 return delegate().size(); 082 } 083 084 @Override 085 public CacheStats stats() { 086 return delegate().stats(); 087 } 088 089 @Override 090 public ImmutableList<Map.Entry<K, V>> activeEntries(int limit) { 091 return delegate().activeEntries(limit); 092 } 093 094 @Override 095 public ConcurrentMap<K, V> asMap() { 096 return delegate().asMap(); 097 } 098 099 @Override 100 public void cleanUp() { 101 delegate().cleanUp(); 102 } 103 104 /** 105 * A simplified version of {@link ForwardingCache} where subclasses can pass in an already 106 * constructed {@link Cache} as the delegete. 107 * 108 * @since 10.0 109 */ 110 @Beta 111 public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> { 112 private final Cache<K, V> delegate; 113 114 protected SimpleForwardingCache(Cache<K, V> delegate) { 115 this.delegate = Preconditions.checkNotNull(delegate); 116 } 117 118 @Override 119 protected final Cache<K, V> delegate() { 120 return delegate; 121 } 122 } 123 }