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