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