001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.cache;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.google.common.base.Preconditions.checkState;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.base.Ascii;
024import com.google.common.base.Equivalence;
025import com.google.common.base.MoreObjects;
026import com.google.common.base.Supplier;
027import com.google.common.base.Suppliers;
028import com.google.common.base.Ticker;
029import com.google.common.cache.AbstractCache.SimpleStatsCounter;
030import com.google.common.cache.AbstractCache.StatsCounter;
031import com.google.common.cache.LocalCache.Strength;
032import com.google.errorprone.annotations.CheckReturnValue;
033import com.google.j2objc.annotations.J2ObjCIncompatible;
034import java.util.ConcurrentModificationException;
035import java.util.IdentityHashMap;
036import java.util.Map;
037import java.util.concurrent.ConcurrentHashMap;
038import java.util.concurrent.TimeUnit;
039import java.util.logging.Level;
040import java.util.logging.Logger;
041import org.checkerframework.checker.nullness.qual.Nullable;
042
043/**
044 * A builder of {@link LoadingCache} and {@link Cache} instances having any combination of the
045 * following features:
046 *
047 * <ul>
048 *   <li>automatic loading of entries into the cache
049 *   <li>least-recently-used eviction when a maximum size is exceeded
050 *   <li>time-based expiration of entries, measured since last access or last write
051 *   <li>keys automatically wrapped in {@code WeakReference}
052 *   <li>values automatically wrapped in {@code WeakReference} or {@code SoftReference}
053 *   <li>notification of evicted (or otherwise removed) entries
054 *   <li>accumulation of cache access statistics
055 * </ul>
056 *
057 *
058 * <p>These features are all optional; caches can be created using all or none of them. By default
059 * cache instances created by {@code CacheBuilder} will not perform any type of eviction.
060 *
061 * <p>Usage example:
062 *
063 * <pre>{@code
064 * LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()
065 *     .maximumSize(10000)
066 *     .expireAfterWrite(Duration.ofMinutes(10))
067 *     .removalListener(MY_LISTENER)
068 *     .build(
069 *         new CacheLoader<Key, Graph>() {
070 *           public Graph load(Key key) throws AnyException {
071 *             return createExpensiveGraph(key);
072 *           }
073 *         });
074 * }</pre>
075 *
076 * <p>Or equivalently,
077 *
078 * <pre>{@code
079 * // In real life this would come from a command-line flag or config file
080 * String spec = "maximumSize=10000,expireAfterWrite=10m";
081 *
082 * LoadingCache<Key, Graph> graphs = CacheBuilder.from(spec)
083 *     .removalListener(MY_LISTENER)
084 *     .build(
085 *         new CacheLoader<Key, Graph>() {
086 *           public Graph load(Key key) throws AnyException {
087 *             return createExpensiveGraph(key);
088 *           }
089 *         });
090 * }</pre>
091 *
092 * <p>The returned cache is implemented as a hash table with similar performance characteristics to
093 * {@link ConcurrentHashMap}. It implements all optional operations of the {@link LoadingCache} and
094 * {@link Cache} interfaces. The {@code asMap} view (and its collection views) have <i>weakly
095 * consistent iterators</i>. This means that they are safe for concurrent use, but if other threads
096 * modify the cache after the iterator is created, it is undefined which of these changes, if any,
097 * are reflected in that iterator. These iterators never throw {@link
098 * ConcurrentModificationException}.
099 *
100 * <p><b>Note:</b> by default, the returned cache uses equality comparisons (the {@link
101 * Object#equals equals} method) to determine equality for keys or values. However, if {@link
102 * #weakKeys} was specified, the cache uses identity ({@code ==}) comparisons instead for keys.
103 * Likewise, if {@link #weakValues} or {@link #softValues} was specified, the cache uses identity
104 * comparisons for values.
105 *
106 * <p>Entries are automatically evicted from the cache when any of {@linkplain #maximumSize(long)
107 * maximumSize}, {@linkplain #maximumWeight(long) maximumWeight}, {@linkplain #expireAfterWrite
108 * expireAfterWrite}, {@linkplain #expireAfterAccess expireAfterAccess}, {@linkplain #weakKeys
109 * weakKeys}, {@linkplain #weakValues weakValues}, or {@linkplain #softValues softValues} are
110 * requested.
111 *
112 * <p>If {@linkplain #maximumSize(long) maximumSize} or {@linkplain #maximumWeight(long)
113 * maximumWeight} is requested entries may be evicted on each cache modification.
114 *
115 * <p>If {@linkplain #expireAfterWrite expireAfterWrite} or {@linkplain #expireAfterAccess
116 * expireAfterAccess} is requested entries may be evicted on each cache modification, on occasional
117 * cache accesses, or on calls to {@link Cache#cleanUp}. Expired entries may be counted by {@link
118 * Cache#size}, but will never be visible to read or write operations.
119 *
120 * <p>If {@linkplain #weakKeys weakKeys}, {@linkplain #weakValues weakValues}, or {@linkplain
121 * #softValues softValues} are requested, it is possible for a key or value present in the cache to
122 * be reclaimed by the garbage collector. Entries with reclaimed keys or values may be removed from
123 * the cache on each cache modification, on occasional cache accesses, or on calls to {@link
124 * Cache#cleanUp}; such entries may be counted in {@link Cache#size}, but will never be visible to
125 * read or write operations.
126 *
127 * <p>Certain cache configurations will result in the accrual of periodic maintenance tasks which
128 * will be performed during write operations, or during occasional read operations in the absence of
129 * writes. The {@link Cache#cleanUp} method of the returned cache will also perform maintenance, but
130 * calling it should not be necessary with a high throughput cache. Only caches built with
131 * {@linkplain #removalListener removalListener}, {@linkplain #expireAfterWrite expireAfterWrite},
132 * {@linkplain #expireAfterAccess expireAfterAccess}, {@linkplain #weakKeys weakKeys}, {@linkplain
133 * #weakValues weakValues}, or {@linkplain #softValues softValues} perform periodic maintenance.
134 *
135 * <p>The caches produced by {@code CacheBuilder} are serializable, and the deserialized caches
136 * retain all the configuration properties of the original cache. Note that the serialized form does
137 * <i>not</i> include cache contents, but only configuration.
138 *
139 * <p>See the Guava User Guide article on <a
140 * href="https://github.com/google/guava/wiki/CachesExplained">caching</a> for a higher-level
141 * explanation.
142 *
143 * @param <K> the most general key type this builder will be able to create caches for. This is
144 *     normally {@code Object} unless it is constrained by using a method like {@code
145 *     #removalListener}
146 * @param <V> the most general value type this builder will be able to create caches for. This is
147 *     normally {@code Object} unless it is constrained by using a method like {@code
148 *     #removalListener}
149 * @author Charles Fry
150 * @author Kevin Bourrillion
151 * @since 10.0
152 */
153@GwtCompatible(emulated = true)
154public final class CacheBuilder<K, V> {
155  private static final int DEFAULT_INITIAL_CAPACITY = 16;
156  private static final int DEFAULT_CONCURRENCY_LEVEL = 4;
157
158  @SuppressWarnings("GoodTime") // should be a java.time.Duration
159  private static final int DEFAULT_EXPIRATION_NANOS = 0;
160
161  @SuppressWarnings("GoodTime") // should be a java.time.Duration
162  private static final int DEFAULT_REFRESH_NANOS = 0;
163
164  static final Supplier<? extends StatsCounter> NULL_STATS_COUNTER =
165      Suppliers.ofInstance(
166          new StatsCounter() {
167            @Override
168            public void recordHits(int count) {}
169
170            @Override
171            public void recordMisses(int count) {}
172
173            @SuppressWarnings("GoodTime") // b/122668874
174            @Override
175            public void recordLoadSuccess(long loadTime) {}
176
177            @SuppressWarnings("GoodTime") // b/122668874
178            @Override
179            public void recordLoadException(long loadTime) {}
180
181            @Override
182            public void recordEviction() {}
183
184            @Override
185            public CacheStats snapshot() {
186              return EMPTY_STATS;
187            }
188          });
189  static final CacheStats EMPTY_STATS = new CacheStats(0, 0, 0, 0, 0, 0);
190
191  static final Supplier<StatsCounter> CACHE_STATS_COUNTER =
192      new Supplier<StatsCounter>() {
193        @Override
194        public StatsCounter get() {
195          return new SimpleStatsCounter();
196        }
197      };
198
199  enum NullListener implements RemovalListener<Object, Object> {
200    INSTANCE;
201
202    @Override
203    public void onRemoval(RemovalNotification<Object, Object> notification) {}
204  }
205
206  enum OneWeigher implements Weigher<Object, Object> {
207    INSTANCE;
208
209    @Override
210    public int weigh(Object key, Object value) {
211      return 1;
212    }
213  }
214
215  static final Ticker NULL_TICKER =
216      new Ticker() {
217        @Override
218        public long read() {
219          return 0;
220        }
221      };
222
223  private static final Logger logger = Logger.getLogger(CacheBuilder.class.getName());
224
225  static final int UNSET_INT = -1;
226
227  boolean strictParsing = true;
228
229  int initialCapacity = UNSET_INT;
230  int concurrencyLevel = UNSET_INT;
231  long maximumSize = UNSET_INT;
232  long maximumWeight = UNSET_INT;
233  @Nullable Weigher<? super K, ? super V> weigher;
234
235  @Nullable Strength keyStrength;
236  @Nullable Strength valueStrength;
237
238  @SuppressWarnings("GoodTime") // should be a java.time.Duration
239  long expireAfterWriteNanos = UNSET_INT;
240
241  @SuppressWarnings("GoodTime") // should be a java.time.Duration
242  long expireAfterAccessNanos = UNSET_INT;
243
244  @SuppressWarnings("GoodTime") // should be a java.time.Duration
245  long refreshNanos = UNSET_INT;
246
247  @Nullable Equivalence<Object> keyEquivalence;
248  @Nullable Equivalence<Object> valueEquivalence;
249
250  @Nullable RemovalListener<? super K, ? super V> removalListener;
251  @Nullable Ticker ticker;
252
253  Supplier<? extends StatsCounter> statsCounterSupplier = NULL_STATS_COUNTER;
254
255  private CacheBuilder() {}
256
257  /**
258   * Constructs a new {@code CacheBuilder} instance with default settings, including strong keys,
259   * strong values, and no automatic eviction of any kind.
260   *
261   * <p>Note that while this return type is {@code CacheBuilder<Object, Object>}, type parameters on
262   * the {@link #build} methods allow you to create a cache of any key and value type desired.
263   */
264  public static CacheBuilder<Object, Object> newBuilder() {
265    return new CacheBuilder<>();
266  }
267
268  /**
269   * Constructs a new {@code CacheBuilder} instance with the settings specified in {@code spec}.
270   *
271   * @since 12.0
272   */
273  @GwtIncompatible // To be supported
274  public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
275    return spec.toCacheBuilder().lenientParsing();
276  }
277
278  /**
279   * Constructs a new {@code CacheBuilder} instance with the settings specified in {@code spec}.
280   * This is especially useful for command-line configuration of a {@code CacheBuilder}.
281   *
282   * @param spec a String in the format specified by {@link CacheBuilderSpec}
283   * @since 12.0
284   */
285  @GwtIncompatible // To be supported
286  public static CacheBuilder<Object, Object> from(String spec) {
287    return from(CacheBuilderSpec.parse(spec));
288  }
289
290  /**
291   * Enables lenient parsing. Useful for tests and spec parsing.
292   *
293   * @return this {@code CacheBuilder} instance (for chaining)
294   */
295  @GwtIncompatible // To be supported
296  CacheBuilder<K, V> lenientParsing() {
297    strictParsing = false;
298    return this;
299  }
300
301  /**
302   * Sets a custom {@code Equivalence} strategy for comparing keys.
303   *
304   * <p>By default, the cache uses {@link Equivalence#identity} to determine key equality when
305   * {@link #weakKeys} is specified, and {@link Equivalence#equals()} otherwise.
306   *
307   * @return this {@code CacheBuilder} instance (for chaining)
308   */
309  @GwtIncompatible // To be supported
310  CacheBuilder<K, V> keyEquivalence(Equivalence<Object> equivalence) {
311    checkState(keyEquivalence == null, "key equivalence was already set to %s", keyEquivalence);
312    keyEquivalence = checkNotNull(equivalence);
313    return this;
314  }
315
316  Equivalence<Object> getKeyEquivalence() {
317    return MoreObjects.firstNonNull(keyEquivalence, getKeyStrength().defaultEquivalence());
318  }
319
320  /**
321   * Sets a custom {@code Equivalence} strategy for comparing values.
322   *
323   * <p>By default, the cache uses {@link Equivalence#identity} to determine value equality when
324   * {@link #weakValues} or {@link #softValues} is specified, and {@link Equivalence#equals()}
325   * otherwise.
326   *
327   * @return this {@code CacheBuilder} instance (for chaining)
328   */
329  @GwtIncompatible // To be supported
330  CacheBuilder<K, V> valueEquivalence(Equivalence<Object> equivalence) {
331    checkState(
332        valueEquivalence == null, "value equivalence was already set to %s", valueEquivalence);
333    this.valueEquivalence = checkNotNull(equivalence);
334    return this;
335  }
336
337  Equivalence<Object> getValueEquivalence() {
338    return MoreObjects.firstNonNull(valueEquivalence, getValueStrength().defaultEquivalence());
339  }
340
341  /**
342   * Sets the minimum total size for the internal hash tables. For example, if the initial capacity
343   * is {@code 60}, and the concurrency level is {@code 8}, then eight segments are created, each
344   * having a hash table of size eight. Providing a large enough estimate at construction time
345   * avoids the need for expensive resizing operations later, but setting this value unnecessarily
346   * high wastes memory.
347   *
348   * @return this {@code CacheBuilder} instance (for chaining)
349   * @throws IllegalArgumentException if {@code initialCapacity} is negative
350   * @throws IllegalStateException if an initial capacity was already set
351   */
352  public CacheBuilder<K, V> initialCapacity(int initialCapacity) {
353    checkState(
354        this.initialCapacity == UNSET_INT,
355        "initial capacity was already set to %s",
356        this.initialCapacity);
357    checkArgument(initialCapacity >= 0);
358    this.initialCapacity = initialCapacity;
359    return this;
360  }
361
362  int getInitialCapacity() {
363    return (initialCapacity == UNSET_INT) ? DEFAULT_INITIAL_CAPACITY : initialCapacity;
364  }
365
366  /**
367   * Guides the allowed concurrency among update operations. Used as a hint for internal sizing. The
368   * table is internally partitioned to try to permit the indicated number of concurrent updates
369   * without contention. Because assignment of entries to these partitions is not necessarily
370   * uniform, the actual concurrency observed may vary. Ideally, you should choose a value to
371   * accommodate as many threads as will ever concurrently modify the table. Using a significantly
372   * higher value than you need can waste space and time, and a significantly lower value can lead
373   * to thread contention. But overestimates and underestimates within an order of magnitude do not
374   * usually have much noticeable impact. A value of one permits only one thread to modify the cache
375   * at a time, but since read operations and cache loading computations can proceed concurrently,
376   * this still yields higher concurrency than full synchronization.
377   *
378   * <p>Defaults to 4. <b>Note:</b>The default may change in the future. If you care about this
379   * value, you should always choose it explicitly.
380   *
381   * <p>The current implementation uses the concurrency level to create a fixed number of hashtable
382   * segments, each governed by its own write lock. The segment lock is taken once for each explicit
383   * write, and twice for each cache loading computation (once prior to loading the new value, and
384   * once after loading completes). Much internal cache management is performed at the segment
385   * granularity. For example, access queues and write queues are kept per segment when they are
386   * required by the selected eviction algorithm. As such, when writing unit tests it is not
387   * uncommon to specify {@code concurrencyLevel(1)} in order to achieve more deterministic eviction
388   * behavior.
389   *
390   * <p>Note that future implementations may abandon segment locking in favor of more advanced
391   * concurrency controls.
392   *
393   * @return this {@code CacheBuilder} instance (for chaining)
394   * @throws IllegalArgumentException if {@code concurrencyLevel} is nonpositive
395   * @throws IllegalStateException if a concurrency level was already set
396   */
397  public CacheBuilder<K, V> concurrencyLevel(int concurrencyLevel) {
398    checkState(
399        this.concurrencyLevel == UNSET_INT,
400        "concurrency level was already set to %s",
401        this.concurrencyLevel);
402    checkArgument(concurrencyLevel > 0);
403    this.concurrencyLevel = concurrencyLevel;
404    return this;
405  }
406
407  int getConcurrencyLevel() {
408    return (concurrencyLevel == UNSET_INT) ? DEFAULT_CONCURRENCY_LEVEL : concurrencyLevel;
409  }
410
411  /**
412   * Specifies the maximum number of entries the cache may contain.
413   *
414   * <p>Note that the cache <b>may evict an entry before this limit is exceeded</b>. For example, in
415   * the current implementation, when {@code concurrencyLevel} is greater than {@code 1}, each
416   * resulting segment inside the cache <i>independently</i> limits its own size to approximately
417   * {@code maximumSize / concurrencyLevel}.
418   *
419   * <p>When eviction is necessary, the cache evicts entries that are less likely to be used again.
420   * For example, the cache may evict an entry because it hasn't been used recently or very often.
421   *
422   * <p>If {@code maximumSize} is zero, elements will be evicted immediately after being loaded into
423   * cache. This can be useful in testing, or to disable caching temporarily.
424   *
425   * <p>This feature cannot be used in conjunction with {@link #maximumWeight}.
426   *
427   * @param maximumSize the maximum size of the cache
428   * @return this {@code CacheBuilder} instance (for chaining)
429   * @throws IllegalArgumentException if {@code maximumSize} is negative
430   * @throws IllegalStateException if a maximum size or weight was already set
431   */
432  public CacheBuilder<K, V> maximumSize(long maximumSize) {
433    checkState(
434        this.maximumSize == UNSET_INT, "maximum size was already set to %s", this.maximumSize);
435    checkState(
436        this.maximumWeight == UNSET_INT,
437        "maximum weight was already set to %s",
438        this.maximumWeight);
439    checkState(this.weigher == null, "maximum size can not be combined with weigher");
440    checkArgument(maximumSize >= 0, "maximum size must not be negative");
441    this.maximumSize = maximumSize;
442    return this;
443  }
444
445  /**
446   * Specifies the maximum weight of entries the cache may contain. Weight is determined using the
447   * {@link Weigher} specified with {@link #weigher}, and use of this method requires a
448   * corresponding call to {@link #weigher} prior to calling {@link #build}.
449   *
450   * <p>Note that the cache <b>may evict an entry before this limit is exceeded</b>. For example, in
451   * the current implementation, when {@code concurrencyLevel} is greater than {@code 1}, each
452   * resulting segment inside the cache <i>independently</i> limits its own weight to approximately
453   * {@code maximumWeight / concurrencyLevel}.
454   *
455   * <p>When eviction is necessary, the cache evicts entries that are less likely to be used again.
456   * For example, the cache may evict an entry because it hasn't been used recently or very often.
457   *
458   * <p>If {@code maximumWeight} is zero, elements will be evicted immediately after being loaded
459   * into cache. This can be useful in testing, or to disable caching temporarily.
460   *
461   * <p>Note that weight is only used to determine whether the cache is over capacity; it has no
462   * effect on selecting which entry should be evicted next.
463   *
464   * <p>This feature cannot be used in conjunction with {@link #maximumSize}.
465   *
466   * @param maximumWeight the maximum total weight of entries the cache may contain
467   * @return this {@code CacheBuilder} instance (for chaining)
468   * @throws IllegalArgumentException if {@code maximumWeight} is negative
469   * @throws IllegalStateException if a maximum weight or size was already set
470   * @since 11.0
471   */
472  @GwtIncompatible // To be supported
473  public CacheBuilder<K, V> maximumWeight(long maximumWeight) {
474    checkState(
475        this.maximumWeight == UNSET_INT,
476        "maximum weight was already set to %s",
477        this.maximumWeight);
478    checkState(
479        this.maximumSize == UNSET_INT, "maximum size was already set to %s", this.maximumSize);
480    this.maximumWeight = maximumWeight;
481    checkArgument(maximumWeight >= 0, "maximum weight must not be negative");
482    return this;
483  }
484
485  /**
486   * Specifies the weigher to use in determining the weight of entries. Entry weight is taken into
487   * consideration by {@link #maximumWeight(long)} when determining which entries to evict, and use
488   * of this method requires a corresponding call to {@link #maximumWeight(long)} prior to calling
489   * {@link #build}. Weights are measured and recorded when entries are inserted into the cache, and
490   * are thus effectively static during the lifetime of a cache entry.
491   *
492   * <p>When the weight of an entry is zero it will not be considered for size-based eviction
493   * (though it still may be evicted by other means).
494   *
495   * <p><b>Important note:</b> Instead of returning <em>this</em> as a {@code CacheBuilder}
496   * instance, this method returns {@code CacheBuilder<K1, V1>}. From this point on, either the
497   * original reference or the returned reference may be used to complete configuration and build
498   * the cache, but only the "generic" one is type-safe. That is, it will properly prevent you from
499   * building caches whose key or value types are incompatible with the types accepted by the
500   * weigher already provided; the {@code CacheBuilder} type cannot do this. For best results,
501   * simply use the standard method-chaining idiom, as illustrated in the documentation at top,
502   * configuring a {@code CacheBuilder} and building your {@link Cache} all in a single statement.
503   *
504   * <p><b>Warning:</b> if you ignore the above advice, and use this {@code CacheBuilder} to build a
505   * cache whose key or value type is incompatible with the weigher, you will likely experience a
506   * {@link ClassCastException} at some <i>undefined</i> point in the future.
507   *
508   * @param weigher the weigher to use in calculating the weight of cache entries
509   * @return this {@code CacheBuilder} instance (for chaining)
510   * @throws IllegalArgumentException if {@code size} is negative
511   * @throws IllegalStateException if a maximum size was already set
512   * @since 11.0
513   */
514  @GwtIncompatible // To be supported
515  public <K1 extends K, V1 extends V> CacheBuilder<K1, V1> weigher(
516      Weigher<? super K1, ? super V1> weigher) {
517    checkState(this.weigher == null);
518    if (strictParsing) {
519      checkState(
520          this.maximumSize == UNSET_INT,
521          "weigher can not be combined with maximum size",
522          this.maximumSize);
523    }
524
525    // safely limiting the kinds of caches this can produce
526    @SuppressWarnings("unchecked")
527    CacheBuilder<K1, V1> me = (CacheBuilder<K1, V1>) this;
528    me.weigher = checkNotNull(weigher);
529    return me;
530  }
531
532  long getMaximumWeight() {
533    if (expireAfterWriteNanos == 0 || expireAfterAccessNanos == 0) {
534      return 0;
535    }
536    return (weigher == null) ? maximumSize : maximumWeight;
537  }
538
539  // Make a safe contravariant cast now so we don't have to do it over and over.
540  @SuppressWarnings("unchecked")
541  <K1 extends K, V1 extends V> Weigher<K1, V1> getWeigher() {
542    return (Weigher<K1, V1>) MoreObjects.firstNonNull(weigher, OneWeigher.INSTANCE);
543  }
544
545  /**
546   * Specifies that each key (not value) stored in the cache should be wrapped in a {@link
547   * WeakReference} (by default, strong references are used).
548   *
549   * <p><b>Warning:</b> when this method is used, the resulting cache will use identity ({@code ==})
550   * comparison to determine equality of keys. Its {@link Cache#asMap} view will therefore
551   * technically violate the {@link Map} specification (in the same way that {@link IdentityHashMap}
552   * does).
553   *
554   * <p>Entries with keys that have been garbage collected may be counted in {@link Cache#size}, but
555   * will never be visible to read or write operations; such entries are cleaned up as part of the
556   * routine maintenance described in the class javadoc.
557   *
558   * @return this {@code CacheBuilder} instance (for chaining)
559   * @throws IllegalStateException if the key strength was already set
560   */
561  @GwtIncompatible // java.lang.ref.WeakReference
562  public CacheBuilder<K, V> weakKeys() {
563    return setKeyStrength(Strength.WEAK);
564  }
565
566  CacheBuilder<K, V> setKeyStrength(Strength strength) {
567    checkState(keyStrength == null, "Key strength was already set to %s", keyStrength);
568    keyStrength = checkNotNull(strength);
569    return this;
570  }
571
572  Strength getKeyStrength() {
573    return MoreObjects.firstNonNull(keyStrength, Strength.STRONG);
574  }
575
576  /**
577   * Specifies that each value (not key) stored in the cache should be wrapped in a {@link
578   * WeakReference} (by default, strong references are used).
579   *
580   * <p>Weak values will be garbage collected once they are weakly reachable. This makes them a poor
581   * candidate for caching; consider {@link #softValues} instead.
582   *
583   * <p><b>Note:</b> when this method is used, the resulting cache will use identity ({@code ==})
584   * comparison to determine equality of values.
585   *
586   * <p>Entries with values that have been garbage collected may be counted in {@link Cache#size},
587   * but will never be visible to read or write operations; such entries are cleaned up as part of
588   * the routine maintenance described in the class javadoc.
589   *
590   * @return this {@code CacheBuilder} instance (for chaining)
591   * @throws IllegalStateException if the value strength was already set
592   */
593  @GwtIncompatible // java.lang.ref.WeakReference
594  public CacheBuilder<K, V> weakValues() {
595    return setValueStrength(Strength.WEAK);
596  }
597
598  /**
599   * Specifies that each value (not key) stored in the cache should be wrapped in a {@link
600   * SoftReference} (by default, strong references are used). Softly-referenced objects will be
601   * garbage-collected in a <i>globally</i> least-recently-used manner, in response to memory
602   * demand.
603   *
604   * <p><b>Warning:</b> in most circumstances it is better to set a per-cache {@linkplain
605   * #maximumSize(long) maximum size} instead of using soft references. You should only use this
606   * method if you are well familiar with the practical consequences of soft references.
607   *
608   * <p><b>Note:</b> when this method is used, the resulting cache will use identity ({@code ==})
609   * comparison to determine equality of values.
610   *
611   * <p>Entries with values that have been garbage collected may be counted in {@link Cache#size},
612   * but will never be visible to read or write operations; such entries are cleaned up as part of
613   * the routine maintenance described in the class javadoc.
614   *
615   * @return this {@code CacheBuilder} instance (for chaining)
616   * @throws IllegalStateException if the value strength was already set
617   */
618  @GwtIncompatible // java.lang.ref.SoftReference
619  public CacheBuilder<K, V> softValues() {
620    return setValueStrength(Strength.SOFT);
621  }
622
623  CacheBuilder<K, V> setValueStrength(Strength strength) {
624    checkState(valueStrength == null, "Value strength was already set to %s", valueStrength);
625    valueStrength = checkNotNull(strength);
626    return this;
627  }
628
629  Strength getValueStrength() {
630    return MoreObjects.firstNonNull(valueStrength, Strength.STRONG);
631  }
632
633  /**
634   * Specifies that each entry should be automatically removed from the cache once a fixed duration
635   * has elapsed after the entry's creation, or the most recent replacement of its value.
636   *
637   * <p>When {@code duration} is zero, this method hands off to {@link #maximumSize(long)
638   * maximumSize}{@code (0)}, ignoring any otherwise-specified maximum size or weight. This can be
639   * useful in testing, or to disable caching temporarily without a code change.
640   *
641   * <p>Expired entries may be counted in {@link Cache#size}, but will never be visible to read or
642   * write operations. Expired entries are cleaned up as part of the routine maintenance described
643   * in the class javadoc.
644   *
645   * @param duration the length of time after an entry is created that it should be automatically
646   *     removed
647   * @return this {@code CacheBuilder} instance (for chaining)
648   * @throws IllegalArgumentException if {@code duration} is negative
649   * @throws IllegalStateException if the time to live or time to idle was already set
650   * @throws ArithmeticException for durations greater than +/- approximately 292 years
651   * @since 25.0
652   */
653  @J2ObjCIncompatible
654  @GwtIncompatible // java.time.Duration
655  @SuppressWarnings("GoodTime") // java.time.Duration decomposition
656  public CacheBuilder<K, V> expireAfterWrite(java.time.Duration duration) {
657    return expireAfterWrite(toNanosSaturated(duration), TimeUnit.NANOSECONDS);
658  }
659
660  /**
661   * Specifies that each entry should be automatically removed from the cache once a fixed duration
662   * has elapsed after the entry's creation, or the most recent replacement of its value.
663   *
664   * <p>When {@code duration} is zero, this method hands off to {@link #maximumSize(long)
665   * maximumSize}{@code (0)}, ignoring any otherwise-specified maximum size or weight. This can be
666   * useful in testing, or to disable caching temporarily without a code change.
667   *
668   * <p>Expired entries may be counted in {@link Cache#size}, but will never be visible to read or
669   * write operations. Expired entries are cleaned up as part of the routine maintenance described
670   * in the class javadoc.
671   *
672   * <p>If you can represent the duration as a {@link java.time.Duration} (which should be preferred
673   * when feasible), use {@link #expireAfterWrite(Duration)} instead.
674   *
675   * @param duration the length of time after an entry is created that it should be automatically
676   *     removed
677   * @param unit the unit that {@code duration} is expressed in
678   * @return this {@code CacheBuilder} instance (for chaining)
679   * @throws IllegalArgumentException if {@code duration} is negative
680   * @throws IllegalStateException if the time to live or time to idle was already set
681   */
682  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
683  public CacheBuilder<K, V> expireAfterWrite(long duration, TimeUnit unit) {
684    checkState(
685        expireAfterWriteNanos == UNSET_INT,
686        "expireAfterWrite was already set to %s ns",
687        expireAfterWriteNanos);
688    checkArgument(duration >= 0, "duration cannot be negative: %s %s", duration, unit);
689    this.expireAfterWriteNanos = unit.toNanos(duration);
690    return this;
691  }
692
693  @SuppressWarnings("GoodTime") // nanos internally, should be Duration
694  long getExpireAfterWriteNanos() {
695    return (expireAfterWriteNanos == UNSET_INT) ? DEFAULT_EXPIRATION_NANOS : expireAfterWriteNanos;
696  }
697
698  /**
699   * Specifies that each entry should be automatically removed from the cache once a fixed duration
700   * has elapsed after the entry's creation, the most recent replacement of its value, or its last
701   * access. Access time is reset by all cache read and write operations (including {@code
702   * Cache.asMap().get(Object)} and {@code Cache.asMap().put(K, V)}), but not by {@code
703   * containsKey(Object)}, nor by operations on the collection-views of {@link Cache#asMap}}. So,
704   * for example, iterating through {@code Cache.asMap().entrySet()} does not reset access time for
705   * the entries you retrieve.
706   *
707   * <p>When {@code duration} is zero, this method hands off to {@link #maximumSize(long)
708   * maximumSize}{@code (0)}, ignoring any otherwise-specified maximum size or weight. This can be
709   * useful in testing, or to disable caching temporarily without a code change.
710   *
711   * <p>Expired entries may be counted in {@link Cache#size}, but will never be visible to read or
712   * write operations. Expired entries are cleaned up as part of the routine maintenance described
713   * in the class javadoc.
714   *
715   * @param duration the length of time after an entry is last accessed that it should be
716   *     automatically removed
717   * @return this {@code CacheBuilder} instance (for chaining)
718   * @throws IllegalArgumentException if {@code duration} is negative
719   * @throws IllegalStateException if the time to idle or time to live was already set
720   * @throws ArithmeticException for durations greater than +/- approximately 292 years
721   * @since 25.0
722   */
723  @J2ObjCIncompatible
724  @GwtIncompatible // java.time.Duration
725  @SuppressWarnings("GoodTime") // java.time.Duration decomposition
726  public CacheBuilder<K, V> expireAfterAccess(java.time.Duration duration) {
727    return expireAfterAccess(toNanosSaturated(duration), TimeUnit.NANOSECONDS);
728  }
729
730  /**
731   * Specifies that each entry should be automatically removed from the cache once a fixed duration
732   * has elapsed after the entry's creation, the most recent replacement of its value, or its last
733   * access. Access time is reset by all cache read and write operations (including {@code
734   * Cache.asMap().get(Object)} and {@code Cache.asMap().put(K, V)}), but not by {@code
735   * containsKey(Object)}, nor by operations on the collection-views of {@link Cache#asMap}. So, for
736   * example, iterating through {@code Cache.asMap().entrySet()} does not reset access time for the
737   * entries you retrieve.
738   *
739   * <p>When {@code duration} is zero, this method hands off to {@link #maximumSize(long)
740   * maximumSize}{@code (0)}, ignoring any otherwise-specified maximum size or weight. This can be
741   * useful in testing, or to disable caching temporarily without a code change.
742   *
743   * <p>Expired entries may be counted in {@link Cache#size}, but will never be visible to read or
744   * write operations. Expired entries are cleaned up as part of the routine maintenance described
745   * in the class javadoc.
746   *
747   * <p>If you can represent the duration as a {@link java.time.Duration} (which should be preferred
748   * when feasible), use {@link #expireAfterAccess(Duration)} instead.
749   *
750   * @param duration the length of time after an entry is last accessed that it should be
751   *     automatically removed
752   * @param unit the unit that {@code duration} is expressed in
753   * @return this {@code CacheBuilder} instance (for chaining)
754   * @throws IllegalArgumentException if {@code duration} is negative
755   * @throws IllegalStateException if the time to idle or time to live was already set
756   */
757  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
758  public CacheBuilder<K, V> expireAfterAccess(long duration, TimeUnit unit) {
759    checkState(
760        expireAfterAccessNanos == UNSET_INT,
761        "expireAfterAccess was already set to %s ns",
762        expireAfterAccessNanos);
763    checkArgument(duration >= 0, "duration cannot be negative: %s %s", duration, unit);
764    this.expireAfterAccessNanos = unit.toNanos(duration);
765    return this;
766  }
767
768  @SuppressWarnings("GoodTime") // nanos internally, should be Duration
769  long getExpireAfterAccessNanos() {
770    return (expireAfterAccessNanos == UNSET_INT)
771        ? DEFAULT_EXPIRATION_NANOS
772        : expireAfterAccessNanos;
773  }
774
775  /**
776   * Specifies that active entries are eligible for automatic refresh once a fixed duration has
777   * elapsed after the entry's creation, or the most recent replacement of its value. The semantics
778   * of refreshes are specified in {@link LoadingCache#refresh}, and are performed by calling {@link
779   * CacheLoader#reload}.
780   *
781   * <p>As the default implementation of {@link CacheLoader#reload} is synchronous, it is
782   * recommended that users of this method override {@link CacheLoader#reload} with an asynchronous
783   * implementation; otherwise refreshes will be performed during unrelated cache read and write
784   * operations.
785   *
786   * <p>Currently automatic refreshes are performed when the first stale request for an entry
787   * occurs. The request triggering refresh will make a blocking call to {@link CacheLoader#reload}
788   * and immediately return the new value if the returned future is complete, and the old value
789   * otherwise.
790   *
791   * <p><b>Note:</b> <i>all exceptions thrown during refresh will be logged and then swallowed</i>.
792   *
793   * @param duration the length of time after an entry is created that it should be considered
794   *     stale, and thus eligible for refresh
795   * @return this {@code CacheBuilder} instance (for chaining)
796   * @throws IllegalArgumentException if {@code duration} is negative
797   * @throws IllegalStateException if the refresh interval was already set
798   * @throws ArithmeticException for durations greater than +/- approximately 292 years
799   * @since 25.0
800   */
801  @J2ObjCIncompatible
802  @GwtIncompatible // java.time.Duration
803  @SuppressWarnings("GoodTime") // java.time.Duration decomposition
804  public CacheBuilder<K, V> refreshAfterWrite(java.time.Duration duration) {
805    return refreshAfterWrite(toNanosSaturated(duration), TimeUnit.NANOSECONDS);
806  }
807
808  /**
809   * Specifies that active entries are eligible for automatic refresh once a fixed duration has
810   * elapsed after the entry's creation, or the most recent replacement of its value. The semantics
811   * of refreshes are specified in {@link LoadingCache#refresh}, and are performed by calling {@link
812   * CacheLoader#reload}.
813   *
814   * <p>As the default implementation of {@link CacheLoader#reload} is synchronous, it is
815   * recommended that users of this method override {@link CacheLoader#reload} with an asynchronous
816   * implementation; otherwise refreshes will be performed during unrelated cache read and write
817   * operations.
818   *
819   * <p>Currently automatic refreshes are performed when the first stale request for an entry
820   * occurs. The request triggering refresh will make a blocking call to {@link CacheLoader#reload}
821   * and immediately return the new value if the returned future is complete, and the old value
822   * otherwise.
823   *
824   * <p><b>Note:</b> <i>all exceptions thrown during refresh will be logged and then swallowed</i>.
825   *
826   * <p>If you can represent the duration as a {@link java.time.Duration} (which should be preferred
827   * when feasible), use {@link #refreshAfterWrite(Duration)} instead.
828   *
829   * @param duration the length of time after an entry is created that it should be considered
830   *     stale, and thus eligible for refresh
831   * @param unit the unit that {@code duration} is expressed in
832   * @return this {@code CacheBuilder} instance (for chaining)
833   * @throws IllegalArgumentException if {@code duration} is negative
834   * @throws IllegalStateException if the refresh interval was already set
835   * @since 11.0
836   */
837  @GwtIncompatible // To be supported (synchronously).
838  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
839  public CacheBuilder<K, V> refreshAfterWrite(long duration, TimeUnit unit) {
840    checkNotNull(unit);
841    checkState(refreshNanos == UNSET_INT, "refresh was already set to %s ns", refreshNanos);
842    checkArgument(duration > 0, "duration must be positive: %s %s", duration, unit);
843    this.refreshNanos = unit.toNanos(duration);
844    return this;
845  }
846
847  @SuppressWarnings("GoodTime") // nanos internally, should be Duration
848  long getRefreshNanos() {
849    return (refreshNanos == UNSET_INT) ? DEFAULT_REFRESH_NANOS : refreshNanos;
850  }
851
852  /**
853   * Specifies a nanosecond-precision time source for this cache. By default, {@link
854   * System#nanoTime} is used.
855   *
856   * <p>The primary intent of this method is to facilitate testing of caches with a fake or mock
857   * time source.
858   *
859   * @return this {@code CacheBuilder} instance (for chaining)
860   * @throws IllegalStateException if a ticker was already set
861   */
862  public CacheBuilder<K, V> ticker(Ticker ticker) {
863    checkState(this.ticker == null);
864    this.ticker = checkNotNull(ticker);
865    return this;
866  }
867
868  Ticker getTicker(boolean recordsTime) {
869    if (ticker != null) {
870      return ticker;
871    }
872    return recordsTime ? Ticker.systemTicker() : NULL_TICKER;
873  }
874
875  /**
876   * Specifies a listener instance that caches should notify each time an entry is removed for any
877   * {@linkplain RemovalCause reason}. Each cache created by this builder will invoke this listener
878   * as part of the routine maintenance described in the class documentation above.
879   *
880   * <p><b>Warning:</b> after invoking this method, do not continue to use <i>this</i> cache builder
881   * reference; instead use the reference this method <i>returns</i>. At runtime, these point to the
882   * same instance, but only the returned reference has the correct generic type information so as
883   * to ensure type safety. For best results, use the standard method-chaining idiom illustrated in
884   * the class documentation above, configuring a builder and building your cache in a single
885   * statement. Failure to heed this advice can result in a {@link ClassCastException} being thrown
886   * by a cache operation at some <i>undefined</i> point in the future.
887   *
888   * <p><b>Warning:</b> any exception thrown by {@code listener} will <i>not</i> be propagated to
889   * the {@code Cache} user, only logged via a {@link Logger}.
890   *
891   * @return the cache builder reference that should be used instead of {@code this} for any
892   *     remaining configuration and cache building
893   * @return this {@code CacheBuilder} instance (for chaining)
894   * @throws IllegalStateException if a removal listener was already set
895   */
896  @CheckReturnValue
897  public <K1 extends K, V1 extends V> CacheBuilder<K1, V1> removalListener(
898      RemovalListener<? super K1, ? super V1> listener) {
899    checkState(this.removalListener == null);
900
901    // safely limiting the kinds of caches this can produce
902    @SuppressWarnings("unchecked")
903    CacheBuilder<K1, V1> me = (CacheBuilder<K1, V1>) this;
904    me.removalListener = checkNotNull(listener);
905    return me;
906  }
907
908  // Make a safe contravariant cast now so we don't have to do it over and over.
909  @SuppressWarnings("unchecked")
910  <K1 extends K, V1 extends V> RemovalListener<K1, V1> getRemovalListener() {
911    return (RemovalListener<K1, V1>)
912        MoreObjects.firstNonNull(removalListener, NullListener.INSTANCE);
913  }
914
915  /**
916   * Enable the accumulation of {@link CacheStats} during the operation of the cache. Without this
917   * {@link Cache#stats} will return zero for all statistics. Note that recording stats requires
918   * bookkeeping to be performed with each operation, and thus imposes a performance penalty on
919   * cache operation.
920   *
921   * @return this {@code CacheBuilder} instance (for chaining)
922   * @since 12.0 (previously, stats collection was automatic)
923   */
924  public CacheBuilder<K, V> recordStats() {
925    statsCounterSupplier = CACHE_STATS_COUNTER;
926    return this;
927  }
928
929  boolean isRecordingStats() {
930    return statsCounterSupplier == CACHE_STATS_COUNTER;
931  }
932
933  Supplier<? extends StatsCounter> getStatsCounterSupplier() {
934    return statsCounterSupplier;
935  }
936
937  /**
938   * Builds a cache, which either returns an already-loaded value for a given key or atomically
939   * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently
940   * loading the value for this key, simply waits for that thread to finish and returns its loaded
941   * value. Note that multiple threads can concurrently load values for distinct keys.
942   *
943   * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be
944   * invoked again to create multiple independent caches.
945   *
946   * @param loader the cache loader used to obtain new values
947   * @return a cache having the requested features
948   */
949  public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
950      CacheLoader<? super K1, V1> loader) {
951    checkWeightWithWeigher();
952    return new LocalCache.LocalLoadingCache<>(this, loader);
953  }
954
955  /**
956   * Builds a cache which does not automatically load values when keys are requested.
957   *
958   * <p>Consider {@link #build(CacheLoader)} instead, if it is feasible to implement a {@code
959   * CacheLoader}.
960   *
961   * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be
962   * invoked again to create multiple independent caches.
963   *
964   * @return a cache having the requested features
965   * @since 11.0
966   */
967  public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
968    checkWeightWithWeigher();
969    checkNonLoadingCache();
970    return new LocalCache.LocalManualCache<>(this);
971  }
972
973  private void checkNonLoadingCache() {
974    checkState(refreshNanos == UNSET_INT, "refreshAfterWrite requires a LoadingCache");
975  }
976
977  private void checkWeightWithWeigher() {
978    if (weigher == null) {
979      checkState(maximumWeight == UNSET_INT, "maximumWeight requires weigher");
980    } else {
981      if (strictParsing) {
982        checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight");
983      } else {
984        if (maximumWeight == UNSET_INT) {
985          logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight");
986        }
987      }
988    }
989  }
990
991  /**
992   * Returns a string representation for this CacheBuilder instance. The exact form of the returned
993   * string is not specified.
994   */
995  @Override
996  public String toString() {
997    MoreObjects.ToStringHelper s = MoreObjects.toStringHelper(this);
998    if (initialCapacity != UNSET_INT) {
999      s.add("initialCapacity", initialCapacity);
1000    }
1001    if (concurrencyLevel != UNSET_INT) {
1002      s.add("concurrencyLevel", concurrencyLevel);
1003    }
1004    if (maximumSize != UNSET_INT) {
1005      s.add("maximumSize", maximumSize);
1006    }
1007    if (maximumWeight != UNSET_INT) {
1008      s.add("maximumWeight", maximumWeight);
1009    }
1010    if (expireAfterWriteNanos != UNSET_INT) {
1011      s.add("expireAfterWrite", expireAfterWriteNanos + "ns");
1012    }
1013    if (expireAfterAccessNanos != UNSET_INT) {
1014      s.add("expireAfterAccess", expireAfterAccessNanos + "ns");
1015    }
1016    if (keyStrength != null) {
1017      s.add("keyStrength", Ascii.toLowerCase(keyStrength.toString()));
1018    }
1019    if (valueStrength != null) {
1020      s.add("valueStrength", Ascii.toLowerCase(valueStrength.toString()));
1021    }
1022    if (keyEquivalence != null) {
1023      s.addValue("keyEquivalence");
1024    }
1025    if (valueEquivalence != null) {
1026      s.addValue("valueEquivalence");
1027    }
1028    if (removalListener != null) {
1029      s.addValue("removalListener");
1030    }
1031    return s.toString();
1032  }
1033
1034  /**
1035   * Returns the number of nanoseconds of the given duration without throwing or overflowing.
1036   *
1037   * <p>Instead of throwing {@link ArithmeticException}, this method silently saturates to either
1038   * {@link Long#MAX_VALUE} or {@link Long#MIN_VALUE}. This behavior can be useful when decomposing
1039   * a duration in order to call a legacy API which requires a {@code long, TimeUnit} pair.
1040   */
1041  @GwtIncompatible // java.time.Duration
1042  @SuppressWarnings("GoodTime") // duration decomposition
1043  private static long toNanosSaturated(java.time.Duration duration) {
1044    // Using a try/catch seems lazy, but the catch block will rarely get invoked (except for
1045    // durations longer than approximately +/- 292 years).
1046    try {
1047      return duration.toNanos();
1048    } catch (ArithmeticException tooBig) {
1049      return duration.isNegative() ? Long.MIN_VALUE : Long.MAX_VALUE;
1050    }
1051  }
1052}