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;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.Map;

Computes or retrieves values, based on a key, for use in populating a com.google.common.cache.LoadingCache.

Most implementations will only need to implement load(java.lang.Object). Other methods may be overridden as desired.

Usage example:

   CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() {
     public Graph load(Key key) throws AnyException {
       return createExpensiveGraph(key);
     
   };
   LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader);}

Author(s):
Charles Fry
Since:
10.0
 
 @GwtCompatible(emulated = true)
 public abstract class CacheLoader<K, V> {
  
Constructor for use by subclasses.
 
   protected CacheLoader() {}

  
Computes or retrieves the value corresponding to key.

Parameters:
key the non-null key whose value should be loaded
Returns:
the value associated with key; must not be null
Throws:
java.lang.Exception if unable to load the result
java.lang.InterruptedException if this method is interrupted. InterruptedException is treated like any other Exception in all respects except that, when it is caught, the thread's interrupt status is set
 
   public abstract V load(K keythrows Exception;

  
Computes or retrieves the values corresponding to keys. This method is called by com.google.common.cache.LoadingCache.getAll(java.lang.Iterable).

If the returned map doesn't contain all requested keys then the entries it does contain will be cached, but getAll will throw an exception. If the returned map contains extra keys not present in keys then all returned entries will be cached, but only the entries for keys will be returned from getAll.

This method should be overriden when bulk retrieval is significantly more efficient than many individual lookups. Note that com.google.common.cache.LoadingCache.getAll(java.lang.Iterable) will defer to individual calls to com.google.common.cache.LoadingCache.get(java.lang.Object) if this method is not overriden.

Parameters:
keys the unique, non-null keys whose values should be loaded
Returns:
a map from each key in keys to the value associated with that key; may not contain null values
Throws:
java.lang.Exception if unable to load the result
java.lang.InterruptedException if this method is interrupted. InterruptedException is treated like any other Exception in all respects except that, when it is caught, the thread's interrupt status is set
Since:
11.0
 
   public Map<K, V> loadAll(Iterable<? extends K> keysthrows Exception {
     // This will be caught by getAll(), causing it to fall back to multiple calls to
     // LoadingCache.get
   }

  
Returns a cache loader based on an existing function instance. Note that there's no need to create a new function just to pass it in here; just subclass CacheLoader and implement load instead.

Parameters:
function the function to be used for loading values; must never return null
Returns:
a cache loader that loads values by passing each key to function
  @Beta
  public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
    return new FunctionToCacheLoader<K, V>(function);
  }
  private static final class FunctionToCacheLoader<K, V>
      extends CacheLoader<K, V> implements Serializable {
    private final Function<K, V> computingFunction;
    public FunctionToCacheLoader(Function<K, V> computingFunction) {
      this. = checkNotNull(computingFunction);
    }
    @Override
    public V load(K key) {
      return .apply(checkNotNull(key));
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a cache loader based on an existing supplier instance. Note that there's no need to create a new supplier just to pass it in here; just subclass CacheLoader and implement load instead.

Parameters:
supplier the supplier to be used for loading values; must never return null
Returns:
a cache loader that loads values by calling com.google.common.base.Supplier.get(), irrespective of the key
  @Beta
  public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) {
    return new SupplierToCacheLoader<V>(supplier);
  }
  private static final class SupplierToCacheLoader<V>
      extends CacheLoader<Object, V> implements Serializable {
    private final Supplier<V> computingSupplier;
    public SupplierToCacheLoader(Supplier<V> computingSupplier) {
      this. = checkNotNull(computingSupplier);
    }
    @Override
    public V load(Object key) {
      checkNotNull(key);
      return .get();
    }
    private static final long serialVersionUID = 0;
  }
Thrown to indicate that an invalid response was returned from a call to CacheLoader.

Since:
11.0
  public static final class InvalidCacheLoadException extends RuntimeException {
    public InvalidCacheLoadException(String message) {
      super(message);
    }
  }
New to GrepCode? Check out our FAQ X