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.ForwardingObject; 020import com.google.common.collect.ImmutableMap; 021import java.util.Map; 022import java.util.concurrent.Callable; 023import java.util.concurrent.ConcurrentMap; 024import java.util.concurrent.ExecutionException; 025import org.checkerframework.checker.nullness.qual.Nullable; 026 027/** 028 * A cache which forwards all its method calls to another cache. Subclasses should override one or 029 * more methods to modify the behavior of the backing cache as desired per the <a 030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 031 * 032 * @author Charles Fry 033 * @since 10.0 034 */ 035@GwtIncompatible 036public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> { 037 038 /** Constructor for use by subclasses. */ 039 protected ForwardingCache() {} 040 041 @Override 042 protected abstract Cache<K, V> delegate(); 043 044 /** @since 11.0 */ 045 @Override 046 public @Nullable V getIfPresent(Object key) { 047 return delegate().getIfPresent(key); 048 } 049 050 /** @since 11.0 */ 051 @Override 052 public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException { 053 return delegate().get(key, valueLoader); 054 } 055 056 /** @since 11.0 */ 057 @Override 058 public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) { 059 return delegate().getAllPresent(keys); 060 } 061 062 /** @since 11.0 */ 063 @Override 064 public void put(K key, V value) { 065 delegate().put(key, value); 066 } 067 068 /** @since 12.0 */ 069 @Override 070 public void putAll(Map<? extends K, ? extends V> m) { 071 delegate().putAll(m); 072 } 073 074 @Override 075 public void invalidate(Object key) { 076 delegate().invalidate(key); 077 } 078 079 /** @since 11.0 */ 080 @Override 081 public void invalidateAll(Iterable<?> keys) { 082 delegate().invalidateAll(keys); 083 } 084 085 @Override 086 public void invalidateAll() { 087 delegate().invalidateAll(); 088 } 089 090 @Override 091 public long size() { 092 return delegate().size(); 093 } 094 095 @Override 096 public CacheStats stats() { 097 return delegate().stats(); 098 } 099 100 @Override 101 public ConcurrentMap<K, V> asMap() { 102 return delegate().asMap(); 103 } 104 105 @Override 106 public void cleanUp() { 107 delegate().cleanUp(); 108 } 109 110 /** 111 * A simplified version of {@link ForwardingCache} where subclasses can pass in an already 112 * constructed {@link Cache} as the delegate. 113 * 114 * @since 10.0 115 */ 116 public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> { 117 private final Cache<K, V> delegate; 118 119 protected SimpleForwardingCache(Cache<K, V> delegate) { 120 this.delegate = Preconditions.checkNotNull(delegate); 121 } 122 123 @Override 124 protected final Cache<K, V> delegate() { 125 return delegate; 126 } 127 } 128}