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