AbstractChainingFuture(ListenableFuture<? extends I> inputFuture, F function) { this.inputFuture = Preconditions.checkNotNull(inputFuture); this.function = Preconditions.checkNotNull(function); }
/** * Specifies the maximum number of entries the cache may contain. Note that the cache <b>may evict * an entry before this limit is exceeded</b>. As the cache size grows close to the maximum, the * cache evicts entries that are less likely to be used again. For example, the cache may evict an * entry because it hasn't been used recently or very often. * * <p>When {@code size} is zero, elements will be evicted immediately after being loaded into the * cache. This can be useful in testing, or to disable caching temporarily without a code change. * * <p>This feature cannot be used in conjunction with {@link #maximumWeight}. * * @param size the maximum size of the cache * @throws IllegalArgumentException if {@code size} is negative * @throws IllegalStateException if a maximum size or weight was already set */ @Nonnull public CacheBuilder<K, V> maximumSize(long size) { Preconditions.checkState(this.maximumSize == UNSET_INT, "maximum size was already set to %s", this.maximumSize); Preconditions.checkState(this.maximumWeight == UNSET_INT, "maximum weight was already set to %s", this.maximumWeight); Preconditions.checkState(this.weigher == null, "maximum size can not be combined with weigher"); Preconditions.checkArgument(size >= 0, "maximum size must not be negative"); this.maximumSize = size; return this; }
@Nonnull CacheBuilder<K, V> setKeyStrength(@Nonnull LocalCache.Strength strength) { Preconditions.checkState(keyStrength == null, "Key strength was already set to %s", keyStrength); keyStrength = Preconditions.checkNotNull(strength); return this; }
private void checkNonLoadingCache() { Preconditions.checkState(refreshNanos == UNSET_INT, "refreshAfterWrite requires a LoadingCache"); }
@Nonnull CacheBuilder<K, V> setValueStrength(@Nonnull LocalCache.Strength strength) { Preconditions.checkState(valueStrength == null, "Value strength was already set to %s", valueStrength); valueStrength = Preconditions.checkNotNull(strength); return this; }
private void checkWeightWithWeigher() { if (weigher == null) { Preconditions.checkState(maximumWeight == UNSET_INT, "maximumWeight requires weigher"); } else { if (strictParsing) { Preconditions.checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight"); } else { if (maximumWeight == UNSET_INT) { logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight"); } } } }
Failure(Throwable exception) { this.exception = Preconditions.checkNotNull(exception); } }
/** * Sets a custom {@code Equivalence} strategy for comparing keys. * * <p>By default, the cache uses {@link Equivalence#identity} to determine key equality when * @link #weakKeys} is specified, and {@link Equivalence#equals()} otherwise. */ @Nonnull CacheBuilder<K, V> keyEquivalence(@Nonnull Equivalence<Object> equivalence) { Preconditions.checkState(keyEquivalence == null, "key equivalence was already set to %s", keyEquivalence); keyEquivalence = Preconditions.checkNotNull(equivalence); return this; }
Preconditions.checkState(this.maximumWeight == UNSET_INT, "maximum weight was already set to %s", this.maximumWeight); Preconditions.checkState(this.maximumSize == UNSET_INT, "maximum size was already set to %s", this.maximumSize); this.maximumWeight = weight; Preconditions.checkArgument(weight >= 0, "maximum weight must not be negative"); return this;
/** * Starts the stopwatch. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already running. */ @Nonnull public Stopwatch start() { checkState(!isRunning, "This stopwatch is already running."); isRunning = true; startTick = ticker.read(); return this; }
public static <T> T firstNonNull(T first, T second) { return first != null ? first : Preconditions.checkNotNull(second); }
/** * Sets a custom {@code Equivalence} strategy for comparing values. * * <p>By default, the cache uses {@link Equivalence#identity} to determine value equality when * @link #weakValues} or @link #softValues} is specified, and {@link Equivalence#equals()} * otherwise. */ @Nonnull CacheBuilder<K, V> valueEquivalence(@Nonnull Equivalence<Object> equivalence) { Preconditions.checkState(valueEquivalence == null, "value equivalence was already set to %s", valueEquivalence); this.valueEquivalence = Preconditions.checkNotNull(equivalence); return this; }
Preconditions.checkState(this.concurrencyLevel == UNSET_INT, "concurrency level was already set to %s", this.concurrencyLevel); Preconditions.checkArgument(concurrencyLevel > 0); this.concurrencyLevel = concurrencyLevel; return this;
/** * Stops the stopwatch. Future reads will return the fixed duration that had * elapsed up to this point. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already stopped. */ @Nonnull public Stopwatch stop() { long tick = ticker.read(); checkState(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; }
@Inject public GsonSourceParser(Gson gson, Type type) { checkNotNull(gson, "Gson can't be null"); checkNotNull(type, "Type can't be null"); this.gson = gson; this.type = type; }
/** * Specifies a nanosecond-precision time source for this cache. By default, * {@link System#nanoTime} is used. * * <p>The primary intent of this method is to facilitate testing of caches with a fake or mock * time source. * * @throws IllegalStateException if a ticker was already set */ @Nonnull public CacheBuilder<K, V> ticker(@Nonnull Ticker ticker) { Preconditions.checkState(this.ticker == null); this.ticker = Preconditions.checkNotNull(ticker); return this; }
/** * Specifies that each entry should be automatically removed from the cache once a fixed duration * has elapsed after the entry's creation, the most recent replacement of its value, or its last * access. Access time is reset by all cache read and write operations (including * {@code Cache.asMap().get(Object)} and {@code Cache.asMap().put(K, V)}), but not by operations * on the collection-views of @link Cache#asMap}. * * <p>When {@code duration} is zero, this method hands off to * {@link #maximumSize(long) maximumSize}{@code (0)}, ignoring any otherwise-specificed maximum * size or weight. This can be useful in testing, or to disable caching temporarily without a code * change. * * <p>Expired entries may be counted in @link Cache#size}, but will never be visible to read or * write operations. Expired entries are cleaned up as part of the routine maintenance described * in the class javadoc. * * @param duration the length of time after an entry is last accessed that it should be * automatically removed * @param unit the unit that {@code duration} is expressed in * @throws IllegalArgumentException if {@code duration} is negative * @throws IllegalStateException if the time to idle or time to live was already set */ @Nonnull public CacheBuilder<K, V> expireAfterAccess(long duration, @Nonnull TimeUnit unit) { Preconditions.checkState(expireAfterAccessNanos == UNSET_INT, "expireAfterAccess was already set to %s ns", expireAfterAccessNanos); Preconditions.checkArgument(duration >= 0, "duration cannot be negative: %s %s", duration, unit); this.expireAfterAccessNanos = unit.toNanos(duration); return this; }
@Nullable V waitForLoadingValue(@Nonnull ReferenceEntry<K, V> e, K key, @Nonnull ValueReference<K, V> valueReference) throws ExecutionException { if (!valueReference.isLoading()) { throw new AssertionError(); } Preconditions.checkState(!Thread.holdsLock(e), "Recursive load of: %s", key); // don't consider expiration as we're concurrent with loading try { V value = valueReference.waitForValue(); if (value == null) { throw new InvalidCacheLoadException("CacheLoader returned null for key " + key + "."); } // re-read ticker now that loading has completed long now = map.ticker.read(); recordRead(e, now); return value; } finally { } }
@Inject public GsonReaderParser(Gson gson, Type type) { checkNotNull(gson, "Gson can't be null"); checkNotNull(type, "Type can't be null"); this.gson = gson; this.type = type; }
public <K1 extends K, V1 extends V> CacheBuilder<K1, V1> removalListener( @Nonnull RemovalListener<? super K1, ? super V1> listener) { Preconditions.checkState(this.removalListener == null); me.removalListener = Preconditions.checkNotNull(listener); return me;