Java Code Examples for com.google.common.cache.LoadingCache#asMap()

The following examples show how to use com.google.common.cache.LoadingCache#asMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceCounter.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Computes arbitrary resource aggregates based on a query, a filter, and a grouping function.
 *
 * @param query Query to select tasks for aggregation.
 * @param filter Filter to apply on query result tasks.
 * @param keyFunction Function to define aggregation groupings.
 * @param <K> Key type.
 * @return A map from the keys to their aggregates based on the tasks fetched.
 * @throws StorageException if there was a problem fetching tasks from storage.
 */
public <K> Map<K, Metric> computeAggregates(
    Query.Builder query,
    Predicate<ITaskConfig> filter,
    Function<ITaskConfig, K> keyFunction) throws StorageException {

  LoadingCache<K, Metric> metrics = CacheBuilder.newBuilder()
      .build(new CacheLoader<K, Metric>() {
        @Override
        public Metric load(K key) {
          return new Metric();
        }
      });
  for (ITaskConfig task : Iterables.filter(getTasks(query), filter)) {
    metrics.getUnchecked(keyFunction.apply(task)).accumulate(task);
  }
  return metrics.asMap();
}
 
Example 2
Source File: MigrateMap.java    From canal with Apache License 2.0 4 votes vote down vote up
MigrateConcurrentMap(LoadingCache<K, V> computingCache){
    this.computingCache = computingCache;
    this.cacheView = computingCache.asMap();
}