Example usage for com.google.common.cache LoadingCache getUnchecked

List of usage examples for com.google.common.cache LoadingCache getUnchecked

Introduction

In this page you can find the example usage for com.google.common.cache LoadingCache getUnchecked.

Prototype

V getUnchecked(K key);

Source Link

Document

Returns the value associated with key in this cache, first loading that value if necessary.

Usage

From source file:rickbw.incubator.cache.TimedEvictionCacheBuilderExample.java

public static void main(final String... args) {
    final LoadingCache<String, Object> cache = TimedEvictionCacheBuilder.forKeysOfType(String.class)
            // After being written, ...
            .evictAfterWrite()/*from  w ww  .  j  av a  2 s.  c  om*/
            // ...20% of values will expire after 17 minutes:
            .period(17, TimeUnit.MINUTES, 20)
            // ...30% will expire after 31 minutes:
            .period(31, TimeUnit.MINUTES, 30)
            // ...and the remaining 50% will expire after 1 hour:
            .period(1, TimeUnit.HOURS, 50).build(new CacheLoader<String, Object>() {
                @Override
                public Object load(final String key) {
                    // Imagine something expensive here...
                    return key.trim().toUpperCase();
                }
            });

    final Object cached = cache.getUnchecked("Hello, World");
    assert cached != null;
}

From source file:org.opendaylight.yangtools.binding.data.codec.impl.SchemaRootCodecContext.java

private static <K, V> V getOrRethrow(final LoadingCache<K, V> cache, final K key) {
    try {//from w  w  w  .java 2  s  . c o m
        return cache.getUnchecked(key);
    } catch (final UncheckedExecutionException e) {
        final Throwable cause = e.getCause();
        if (cause != null) {
            Throwables.propagateIfPossible(cause);
        }
        throw e;
    }
}

From source file:org.apache.aurora.scheduler.updater.SlaKillController.java

private static void incrementJobStatCounter(LoadingCache<String, AtomicLong> counter, String prefix,
        IJobKey jobKey) {//w  w  w  .ja v a  2s .c  om

    counter.getUnchecked(prefix + JobKeys.canonicalString(jobKey)).incrementAndGet();
}

From source file:org.apache.hadoop.io.compress.CodecPool.java

@SuppressWarnings("unchecked")
private static <T> int getLeaseCount(LoadingCache<Class<T>, AtomicInteger> usageCounts,
        Class<? extends T> codecClass) {
    return usageCounts.getUnchecked((Class<T>) codecClass).get();
}

From source file:com.uber.tchannel.tracing.PrefixedHeadersCarrier.java

private static Function<String, String> cachingTransformer(Function<String, String> transformer) {
    final LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE)
            .build(CacheLoader.from(transformer));
    return new Function<String, String>() {
        @Override//from ww  w  . j a va 2s . c o m
        public String apply(String key) {
            return cache.getUnchecked(key);
        }
    };
}

From source file:org.apache.hadoop.io.compress.CodecPool.java

private static <T> void updateLeaseCount(LoadingCache<Class<T>, AtomicInteger> usageCounts, T codec,
        int delta) {
    if (codec != null) {
        Class<T> codecClass = ReflectionUtils.getClass(codec);
        usageCounts.getUnchecked(codecClass).addAndGet(delta);
    }// w  ww  . j a v a 2 s  .  co  m
}

From source file:org.apache.aurora.GuiceUtils.java

/**
 * Creates a matcher that will match methods of an interface, optionally excluding inherited
 * methods.//  w w w .j  a v a 2  s .  c  o m
 *
 * @param matchInterface The interface to match.
 * @param declaredMethodsOnly if {@code true} only methods directly declared in the interface
 *                            will be matched, otherwise all methods on the interface are matched.
 * @return A new matcher instance.
 */
public static Matcher<Method> interfaceMatcher(Class<?> matchInterface, boolean declaredMethodsOnly) {

    Method[] methods = declaredMethodsOnly ? matchInterface.getDeclaredMethods() : matchInterface.getMethods();
    final Set<Pair<String, Class<?>[]>> interfaceMethods = ImmutableSet
            .copyOf(Iterables.transform(ImmutableList.copyOf(methods), CANONICALIZE));
    final LoadingCache<Method, Pair<String, Class<?>[]>> cache = CacheBuilder.newBuilder()
            .build(CacheLoader.from(CANONICALIZE));

    return new AbstractMatcher<Method>() {
        @Override
        public boolean matches(Method method) {
            return interfaceMethods.contains(cache.getUnchecked(method));
        }
    };
}

From source file:com.twitter.aurora.GuiceUtils.java

/**
 * Creates a matcher that will match methods of an interface, optionally excluding inherited
 * methods./*w  w w.jav a  2  s . c om*/
 *
 * @param matchInterface The interface to match.
 * @param declaredMethodsOnly if {@code true} only methods directly declared in the interface
 *                            will be matched, otherwise all methods on the interface are matched.
 * @return A new matcher instance.
 */
public static Matcher<Method> interfaceMatcher(Class<?> matchInterface, boolean declaredMethodsOnly) {

    Method[] methods = declaredMethodsOnly ? matchInterface.getDeclaredMethods() : matchInterface.getMethods();
    final Set<Pair<String, Class[]>> interfaceMethods = ImmutableSet
            .copyOf(Iterables.transform(ImmutableList.copyOf(methods), CANONICALIZE));
    final LoadingCache<Method, Pair<String, Class[]>> cache = CacheBuilder.newBuilder()
            .build(CacheLoader.from(CANONICALIZE));

    return new AbstractMatcher<Method>() {
        @Override
        public boolean matches(Method method) {
            return interfaceMethods.contains(cache.getUnchecked(method));
        }
    };
}

From source file:org.apache.beam.runners.core.metrics.MetricsTranslation.java

public static Map<String, Collection<BeamFnApi.Metrics.User>> metricUpdatesToProto(
        MetricUpdates metricUpdates) {/*from  ww w.  j av a  2s.c  o  m*/
    LoadingCache<String, Collection<BeamFnApi.Metrics.User>> fnMetrics = CacheBuilder.newBuilder()
            .build(new CacheLoader<String, Collection<BeamFnApi.Metrics.User>>() {
                @Override
                public Collection<BeamFnApi.Metrics.User> load(String ptransformName) {
                    return new ArrayList<>();
                }
            });

    for (MetricUpdates.MetricUpdate<Long> counterUpdate : metricUpdates.counterUpdates()) {
        fnMetrics.getUnchecked(counterUpdate.getKey().stepName()).add(BeamFnApi.Metrics.User.newBuilder()
                .setMetricName(metricNameToProto(counterUpdate.getKey().metricName()))
                .setCounterData(
                        BeamFnApi.Metrics.User.CounterData.newBuilder().setValue(counterUpdate.getUpdate()))
                .build());
    }

    for (MetricUpdates.MetricUpdate<GaugeData> gaugeUpdate : metricUpdates.gaugeUpdates()) {
        fnMetrics.getUnchecked(gaugeUpdate.getKey().stepName()).add(BeamFnApi.Metrics.User.newBuilder()
                .setMetricName(metricNameToProto(gaugeUpdate.getKey().metricName()))
                .setGaugeData(
                        BeamFnApi.Metrics.User.GaugeData.newBuilder().setValue(gaugeUpdate.getUpdate().value()))
                .build());
    }

    for (MetricUpdates.MetricUpdate<DistributionData> distributionUpdate : metricUpdates
            .distributionUpdates()) {
        fnMetrics.getUnchecked(distributionUpdate.getKey().stepName())
                .add(BeamFnApi.Metrics.User.newBuilder()
                        .setMetricName(metricNameToProto(distributionUpdate.getKey().metricName()))
                        .setDistributionData(BeamFnApi.Metrics.User.DistributionData.newBuilder()
                                .setCount(distributionUpdate.getUpdate().count())
                                .setMax(distributionUpdate.getUpdate().max())
                                .setMin(distributionUpdate.getUpdate().min())
                                .setSum(distributionUpdate.getUpdate().sum()))
                        .build());
    }

    return fnMetrics.asMap();
}

From source file:io.prestosql.plugin.hive.metastore.CachingHiveMetastore.java

private static <K, V> V get(LoadingCache<K, V> cache, K key) {
    try {//from  w  w w . ja v  a  2 s . co  m
        return cache.getUnchecked(key);
    } catch (UncheckedExecutionException e) {
        throwIfInstanceOf(e.getCause(), PrestoException.class);
        throw e;
    }
}