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