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.collect.ImmutableMap;
021    import com.google.common.collect.Maps;
022    import com.google.common.util.concurrent.UncheckedExecutionException;
023    
024    import java.util.Map;
025    import java.util.concurrent.Callable;
026    import java.util.concurrent.ExecutionException;
027    
028    /**
029     * This class provides a skeletal implementation of the {@code Cache} interface to minimize the
030     * effort required to implement this interface.
031     *
032     * <p>To implement a cache, the programmer needs only to extend this class and provide an
033     * implementation for the {@link #get} and {@link #getIfPresent} methods. {@link #getUnchecked},
034     * {@link #get(K, Callable)}, and {@link #getAll} are implemented in terms of {@code get};
035     * {@link #getAllPresent} is implemented in terms of {@code get}; {@link #invalidateAll(Iterable)}
036     * is implemented in terms of {@link #invalidate}. The method {@link #cleanUp} is a no-op. All other
037     * methods throw an {@link UnsupportedOperationException}.
038     *
039     * @author Charles Fry
040     * @since 11.0
041     */
042    @Beta
043    public abstract class AbstractLoadingCache<K, V>
044        extends AbstractCache<K, V> implements LoadingCache<K, V> {
045    
046      /** Constructor for use by subclasses. */
047      protected AbstractLoadingCache() {}
048    
049      @Override
050      public V getUnchecked(K key) {
051        try {
052          return get(key);
053        } catch (ExecutionException e) {
054          throw new UncheckedExecutionException(e.getCause());
055        }
056      }
057    
058      @Override
059      public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
060        Map<K, V> result = Maps.newLinkedHashMap();
061        for (K key : keys) {
062          if (!result.containsKey(key)) {
063            result.put(key, get(key));
064          }
065        }
066        return ImmutableMap.copyOf(result);
067      }
068    
069      @Override
070      public final V apply(K key) {
071        return getUnchecked(key);
072      }
073    
074      @Override
075      public void refresh(K key) {
076        throw new UnsupportedOperationException();
077      }
078    }