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.ImmutableMap;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.concurrent.ExecutionException;
022
023/**
024 * A cache which forwards all its method calls to another cache. Subclasses should override one or
025 * more methods to modify the behavior of the backing cache as desired per the <a
026 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
027 *
028 * <p>Note that {@link #get}, {@link #getUnchecked}, and {@link #apply} all expose the same
029 * underlying functionality, so should probably be overridden as a group.
030 *
031 * @author Charles Fry
032 * @since 11.0
033 */
034@GwtIncompatible
035@ElementTypesAreNonnullByDefault
036public abstract class ForwardingLoadingCache<K, V> extends ForwardingCache<K, V>
037    implements LoadingCache<K, V> {
038
039  /** Constructor for use by subclasses. */
040  protected ForwardingLoadingCache() {}
041
042  @Override
043  protected abstract LoadingCache<K, V> delegate();
044
045  @CanIgnoreReturnValue // TODO(b/27479612): consider removing this
046  @Override
047  public V get(K key) throws ExecutionException {
048    return delegate().get(key);
049  }
050
051  @CanIgnoreReturnValue // TODO(b/27479612): consider removing this
052  @Override
053  public V getUnchecked(K key) {
054    return delegate().getUnchecked(key);
055  }
056
057  @CanIgnoreReturnValue // TODO(b/27479612): consider removing this
058  @Override
059  public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
060    return delegate().getAll(keys);
061  }
062
063  @Override
064  public V apply(K key) {
065    return delegate().apply(key);
066  }
067
068  @Override
069  public void refresh(K key) {
070    delegate().refresh(key);
071  }
072
073  /**
074   * A simplified version of {@link ForwardingLoadingCache} where subclasses can pass in an already
075   * constructed {@link LoadingCache} as the delegate.
076   *
077   * @since 10.0
078   */
079  public abstract static class SimpleForwardingLoadingCache<K, V>
080      extends ForwardingLoadingCache<K, V> {
081    private final LoadingCache<K, V> delegate;
082
083    protected SimpleForwardingLoadingCache(LoadingCache<K, V> delegate) {
084      this.delegate = Preconditions.checkNotNull(delegate);
085    }
086
087    @Override
088    protected final LoadingCache<K, V> delegate() {
089      return delegate;
090    }
091  }
092}