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.jspecify.annotations.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 /** 045 * @since 11.0 046 */ 047 @Override 048 public @Nullable V getIfPresent(Object key) { 049 return delegate().getIfPresent(key); 050 } 051 052 /** 053 * @since 11.0 054 */ 055 @Override 056 public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException { 057 return delegate().get(key, valueLoader); 058 } 059 060 /** 061 * @since 11.0 062 */ 063 @Override 064 /* 065 * <? extends Object> is mostly the same as <?> to plain Java. But to nullness checkers, they 066 * differ: <? extends Object> means "non-null types," while <?> means "all types." 067 */ 068 public ImmutableMap<K, V> getAllPresent(Iterable<? extends Object> keys) { 069 return delegate().getAllPresent(keys); 070 } 071 072 /** 073 * @since 11.0 074 */ 075 @Override 076 public void put(K key, V value) { 077 delegate().put(key, value); 078 } 079 080 /** 081 * @since 12.0 082 */ 083 @Override 084 public void putAll(Map<? extends K, ? extends V> m) { 085 delegate().putAll(m); 086 } 087 088 @Override 089 public void invalidate(Object key) { 090 delegate().invalidate(key); 091 } 092 093 /** 094 * @since 11.0 095 */ 096 @Override 097 // For discussion of <? extends Object>, see getAllPresent. 098 public void invalidateAll(Iterable<? extends Object> keys) { 099 delegate().invalidateAll(keys); 100 } 101 102 @Override 103 public void invalidateAll() { 104 delegate().invalidateAll(); 105 } 106 107 @Override 108 public long size() { 109 return delegate().size(); 110 } 111 112 @Override 113 public CacheStats stats() { 114 return delegate().stats(); 115 } 116 117 @Override 118 public ConcurrentMap<K, V> asMap() { 119 return delegate().asMap(); 120 } 121 122 @Override 123 public void cleanUp() { 124 delegate().cleanUp(); 125 } 126 127 /** 128 * A simplified version of {@link ForwardingCache} where subclasses can pass in an already 129 * constructed {@link Cache} as the delegate. 130 * 131 * @since 10.0 132 */ 133 public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> { 134 private final Cache<K, V> delegate; 135 136 protected SimpleForwardingCache(Cache<K, V> delegate) { 137 this.delegate = Preconditions.checkNotNull(delegate); 138 } 139 140 @Override 141 protected final Cache<K, V> delegate() { 142 return delegate; 143 } 144 } 145}