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.collect.ImmutableMap;
019import com.google.common.collect.Maps;
020import com.google.common.util.concurrent.UncheckedExecutionException;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.Map;
023import java.util.concurrent.Callable;
024import java.util.concurrent.ExecutionException;
025
026/**
027 * This class provides a skeletal implementation of the {@code Cache} interface to minimize the
028 * effort required to implement this interface.
029 *
030 * <p>To implement a cache, the programmer needs only to extend this class and provide an
031 * implementation for the {@link #get(Object)} and {@link #getIfPresent} methods. {@link
032 * #getUnchecked}, {@link #get(Object, Callable)}, and {@link #getAll} are implemented in terms of
033 * {@code get}; {@link #getAllPresent} is implemented in terms of {@code getIfPresent}; {@link
034 * #putAll} is implemented in terms of {@link #put}, {@link #invalidateAll(Iterable)} is implemented
035 * in terms of {@link #invalidate}. The method {@link #cleanUp} is a no-op. All other methods throw
036 * an {@link UnsupportedOperationException}.
037 *
038 * @author Charles Fry
039 * @since 11.0
040 */
041@GwtIncompatible
042public abstract class AbstractLoadingCache<K, V> extends AbstractCache<K, V>
043    implements LoadingCache<K, V> {
044
045  /** Constructor for use by subclasses. */
046  protected AbstractLoadingCache() {}
047
048  @CanIgnoreReturnValue // TODO(b/27479612): consider removing this?
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}