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.annotations.J2ktIncompatible;
019import com.google.common.collect.ImmutableMap;
020import com.google.common.util.concurrent.UncheckedExecutionException;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.LinkedHashMap;
023import java.util.Map;
024import java.util.concurrent.ExecutionException;
025
026/**
027 * This class provides a skeletal implementation of the {@code LoadingCache} interface to minimize
028 * the effort required to implement this interface.
029 *
030 * <p>To implement a loading 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} and {@link #getAll} are implemented in terms of {@code get}; {@link #apply} is
033 * implemented in terms of {@code getUnchecked}. The method {@link #refresh} throws an {@link
034 * UnsupportedOperationException}; for the methods inherited from {@link AbstractCache}, see that
035 * class's documentation.
036 *
037 * @author Charles Fry
038 * @since 11.0
039 */
040@GwtIncompatible
041@J2ktIncompatible // Compiles fine, but hidden for safety because the tests have not been ported
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 = new LinkedHashMap<>();
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}