Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (C) 2011 The Guava Authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.common.cache;
This class provides a skeletal implementation of the Cache interface to minimize the effort required to implement this interface.

To implement a cache, the programmer needs only to extend this class and provide an implementation for the LoadingCache.get(java.lang.Object) and Cache.getIfPresent(java.lang.Object) methods. getUnchecked(java.lang.Object), AbstractCache.get(java.lang.Object,java.util.concurrent.Callable), and getAll(java.lang.Iterable) are implemented in terms of get; AbstractCache.getAllPresent(java.lang.Iterable) is implemented in terms of getIfPresent; AbstractCache.putAll(java.util.Map) is implemented in terms of AbstractCache.put(java.lang.Object,java.lang.Object), AbstractCache.invalidateAll(java.lang.Iterable) is implemented in terms of AbstractCache.invalidate(java.lang.Object). The method AbstractCache.cleanUp() is a no-op. All other methods throw an java.lang.UnsupportedOperationException.

Author(s):
Charles Fry
Since:
11.0
public abstract class AbstractLoadingCache<K, V>
    extends AbstractCache<K, V> implements LoadingCache<K, V> {

  
Constructor for use by subclasses.
  protected AbstractLoadingCache() {}
  public V getUnchecked(K key) {
    try {
      return get(key);
    } catch (ExecutionException e) {
      throw new UncheckedExecutionException(e.getCause());
    }
  }
  public ImmutableMap<K, V> getAll(Iterable<? extends K> keysthrows ExecutionException {
    Map<K, V> result = Maps.newLinkedHashMap();
    for (K key : keys) {
      if (!result.containsKey(key)) {
        result.put(keyget(key));
      }
    }
    return ImmutableMap.copyOf(result);
  }
  public final V apply(K key) {
    return getUnchecked(key);
  }
  public void refresh(K key) {
    throw new UnsupportedOperationException();
  }
New to GrepCode? Check out our FAQ X