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.ImmutableMap; 020import java.util.concurrent.ExecutionException; 021 022/** 023 * A cache which forwards all its method calls to another cache. Subclasses should override one or 024 * more methods to modify the behavior of the backing cache as desired per the 025 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 026 * 027 * <p>Note that {@link #get}, {@link #getUnchecked}, and {@link #apply} all expose the same 028 * underlying functionality, so should probably be overridden as a group. 029 * 030 * @author Charles Fry 031 * @since 11.0 032 */ 033@GwtIncompatible 034public abstract class ForwardingLoadingCache<K, V> extends ForwardingCache<K, V> 035 implements LoadingCache<K, V> { 036 037 /** Constructor for use by subclasses. */ 038 protected ForwardingLoadingCache() {} 039 040 @Override 041 protected abstract LoadingCache<K, V> delegate(); 042 043 @Override 044 public V get(K key) throws ExecutionException { 045 return delegate().get(key); 046 } 047 048 @Override 049 public V getUnchecked(K key) { 050 return delegate().getUnchecked(key); 051 } 052 053 @Override 054 public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException { 055 return delegate().getAll(keys); 056 } 057 058 @Override 059 public V apply(K key) { 060 return delegate().apply(key); 061 } 062 063 @Override 064 public void refresh(K key) { 065 delegate().refresh(key); 066 } 067 068 /** 069 * A simplified version of {@link ForwardingLoadingCache} where subclasses can pass in an already 070 * constructed {@link LoadingCache} as the delegate. 071 * 072 * @since 10.0 073 */ 074 public abstract static class SimpleForwardingLoadingCache<K, V> 075 extends ForwardingLoadingCache<K, V> { 076 private final LoadingCache<K, V> delegate; 077 078 protected SimpleForwardingLoadingCache(LoadingCache<K, V> delegate) { 079 this.delegate = Preconditions.checkNotNull(delegate); 080 } 081 082 @Override 083 protected final LoadingCache<K, V> delegate() { 084 return delegate; 085 } 086 } 087}