    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
    017    package com.google.common.cache;
    018
    019    import com.google.common.annotations.Beta;
    020    import com.google.common.base.Preconditions;
    021    import com.google.common.collect.ForwardingObject;
    022
    023    import javax.annotation.Nullable;
    024
    025    import java.util.concurrent.ConcurrentMap;
    026    import java.util.concurrent.ExecutionException;
    027
    028    /**
    029     * A cache which forwards all its method calls to another cache. Subclasses should override one or
    030     * more methods to modify the behavior of the backing cache as desired per the
    031     * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
    032     *
    033     * <p>Note that {@link #get}, {@link #getUnchecked}, and {@link #apply} all expose the same
    034     * underlying functionality, so should probably be overridden as a group.
    035     *
    036     * @author Charles Fry
    037     * @since 10.0
    038     */
    039    @Beta
    040    public abstract class ForwardingCache<K, V> extends ForwardingObject implements Cache<K, V> {
    041
    042      /** Constructor for use by subclasses. */
    043      protected ForwardingCache() {}
    044
    045      @Override
    046      protected abstract Cache<K, V> delegate();
    047
    048      @Override
    049      @Nullable
    050      public V get(@Nullable K key) throws ExecutionException {
    051        return delegate().get(key);
    052      }
    053
    054      @Override
    055      @Nullable
    056      public V getUnchecked(@Nullable K key) {
    057        return delegate().getUnchecked(key);
    058      }
    059
    060      @Deprecated
    061      @Override
    062      @Nullable
    063      public V apply(@Nullable K key) {
    064        return delegate().apply(key);
    065      }
    066
    067      @Override
    068      public void invalidate(@Nullable Object key) {
    069        delegate().invalidate(key);
    070      }
    071
    072      @Override
    073      public void invalidateAll() {
    074        delegate().invalidateAll();
    075      }
    076
    077      @Override
    078      public long size() {
    079        return delegate().size();
    080      }
    081
    082      @Override
    083      public CacheStats stats() {
    084        return delegate().stats();
    085      }
    086
    087      @Override
    088      public ConcurrentMap<K, V> asMap() {
    089        return delegate().asMap();
    090      }
    091
    092      @Override
    093      public void cleanUp() {
    094        delegate().cleanUp();
    095      }
    096
    097      /**
    098       * A simplified version of {@link ForwardingCache} where subclasses can pass in an already
    099       * constructed {@link Cache} as the delegete.
    100       *
    101       * @since 10.0
    102       */
    103      @Beta
    104      public abstract static class SimpleForwardingCache<K, V> extends ForwardingCache<K, V> {
    105        private final Cache<K, V> delegate;
    106
    107        protected SimpleForwardingCache(Cache<K, V> delegate) {
    108          this.delegate = Preconditions.checkNotNull(delegate);
    109        }
    110
    111        @Override
    112        protected final Cache<K, V> delegate() {
    113          return delegate;
    114        }
    115      }
    116    }