1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.cache; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import com.google.common.base.Preconditions; 19 import com.google.common.collect.ImmutableMap; 20 import java.util.concurrent.ExecutionException; 21 22 /** 23 * A cache which forwards all its method calls to another cache. Subclasses should override one or 24 * more methods to modify the behavior of the backing cache as desired per the <a 25 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 26 * 27 * <p>Note that {@link #get}, {@link #getUnchecked}, and {@link #apply} all expose the same 28 * underlying functionality, so should probably be overridden as a group. 29 * 30 * @author Charles Fry 31 * @since 11.0 32 */ 33 @GwtIncompatible 34 public abstract class ForwardingLoadingCache<K, V> extends ForwardingCache<K, V> 35 implements LoadingCache<K, V> { 36 37 /** Constructor for use by subclasses. */ ForwardingLoadingCache()38 protected ForwardingLoadingCache() {} 39 40 @Override delegate()41 protected abstract LoadingCache<K, V> delegate(); 42 43 @Override get(K key)44 public V get(K key) throws ExecutionException { 45 return delegate().get(key); 46 } 47 48 @Override getUnchecked(K key)49 public V getUnchecked(K key) { 50 return delegate().getUnchecked(key); 51 } 52 53 @Override getAll(Iterable<? extends K> keys)54 public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException { 55 return delegate().getAll(keys); 56 } 57 58 @Override apply(K key)59 public V apply(K key) { 60 return delegate().apply(key); 61 } 62 63 @Override refresh(K key)64 public void refresh(K key) { 65 delegate().refresh(key); 66 } 67 68 /** 69 * A simplified version of {@link ForwardingLoadingCache} where subclasses can pass in an already 70 * constructed {@link LoadingCache} as the delegate. 71 * 72 * @since 10.0 73 */ 74 public abstract static class SimpleForwardingLoadingCache<K, V> 75 extends ForwardingLoadingCache<K, V> { 76 private final LoadingCache<K, V> delegate; 77 SimpleForwardingLoadingCache(LoadingCache<K, V> delegate)78 protected SimpleForwardingLoadingCache(LoadingCache<K, V> delegate) { 79 this.delegate = Preconditions.checkNotNull(delegate); 80 } 81 82 @Override delegate()83 protected final LoadingCache<K, V> delegate() { 84 return delegate; 85 } 86 } 87 } 88