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