001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.cache;
018
019import com.google.common.base.Preconditions;
020import com.google.common.collect.ForwardingObject;
021import com.google.common.collect.ImmutableMap;
022
023import java.util.Map;
024import java.util.concurrent.Callable;
025import java.util.concurrent.ConcurrentMap;
026import java.util.concurrent.ExecutionException;
027
028import javax.annotation.Nullable;
029
030/**
031 * A cache which forwards all its method calls to another cache. Subclasses should override one or
032 * more methods to modify the behavior of the backing cache as desired per the
033 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
034 *
035 * @author Charles Fry
036 * @since 10.0
037 */
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  @Nullable
051  public V getIfPresent(Object key) {
052    return delegate().getIfPresent(key);
053  }
054
055  /**
056   * @since 11.0
057   */
058  @Override
059  public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
060    return delegate().get(key, valueLoader);
061  }
062
063  /**
064   * @since 11.0
065   */
066  @Override
067  public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
068    return delegate().getAllPresent(keys);
069  }
070
071  /**
072   * @since 11.0
073   */
074  @Override
075  public void put(K key, V value) {
076    delegate().put(key, value);
077  }
078
079  /**
080   * @since 12.0
081   */
082  @Override
083  public void putAll(Map<? extends K,? extends V> m) {
084    delegate().putAll(m);
085  }
086
087  @Override
088  public void invalidate(Object key) {
089    delegate().invalidate(key);
090  }
091
092  /**
093   * @since 11.0
094   */
095  @Override
096  public void invalidateAll(Iterable<?> keys) {
097    delegate().invalidateAll(keys);
098  }
099
100  @Override
101  public void invalidateAll() {
102    delegate().invalidateAll();
103  }
104
105  @Override
106  public long size() {
107    return delegate().size();
108  }
109
110  @Override
111  public CacheStats stats() {
112    return delegate().stats();
113  }
114
115  @Override
116  public ConcurrentMap<K, V> asMap() {
117    return delegate().asMap();
118  }
119
120  @Override
121  public void cleanUp() {
122    delegate().cleanUp();
123  }
124
125  /**
126   * A simplified version of {@link ForwardingCache} where subclasses can pass in an already
127   * constructed {@link Cache} as the delegate.
128   *
129   * @since 10.0
130   */
131  public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> {
132    private final Cache<K, V> delegate;
133
134    protected SimpleForwardingCache(Cache<K, V> delegate) {
135      this.delegate = Preconditions.checkNotNull(delegate);
136    }
137
138    @Override
139    protected final Cache<K, V> delegate() {
140      return delegate;
141    }
142  }
143}