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