11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in compliance with the License. You may obtain a copy of the License at
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software distributed under the License
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or implied. See the License for the specific language governing permissions and limitations under
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the License.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect;
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Objects.firstNonNull;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkArgument;
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkState;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Ascii;
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Equivalence;
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Equivalences;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Function;
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Objects;
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Ticker;
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ComputingConcurrentHashMap.ComputingMapAdapter;
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.MapMakerInternalMap.Strength;
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.io.Serializable;
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.ref.SoftReference;
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.ref.WeakReference;
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.AbstractMap;
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collections;
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.ConcurrentModificationException;
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map;
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Set;
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.ConcurrentHashMap;
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.ConcurrentMap;
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.TimeUnit;
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport javax.annotation.Nullable;
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>A builder of {@link ConcurrentMap} instances having any combination of the following features:
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>keys or values automatically wrapped in {@linkplain WeakReference weak} or {@linkplain
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     SoftReference soft} references
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>least-recently-used eviction when a maximum size is exceeded
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>time-based expiration of entries, measured since last access or last write
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>notification of evicted (or otherwise removed) entries
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>on-demand computation of values for keys not already present
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Usage example: <pre>   {@code
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   ConcurrentMap<Key, Graph> graphs = new MapMaker()
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       .concurrencyLevel(4)
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       .weakKeys()
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       .maximumSize(10000)
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       .expireAfterWrite(10, TimeUnit.MINUTES)
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       .makeComputingMap(
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *           new Function<Key, Graph>() {
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *             public Graph apply(Key key) {
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *               return createExpensiveGraph(key);
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *             }
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *           });}</pre>
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * These features are all optional; {@code new MapMaker().makeMap()} returns a valid concurrent map
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that behaves similarly to a {@link ConcurrentHashMap}.
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The returned map is implemented as a hash table with similar performance characteristics to
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link ConcurrentHashMap}. It supports all optional operations of the {@code ConcurrentMap}
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * interface. It does not permit null keys or values.
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> by default, the returned map uses equality comparisons (the {@link Object#equals
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * equals} method) to determine equality for keys or values. However, if {@link #weakKeys} or {@link
821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #softKeys} was specified, the map uses identity ({@code ==}) comparisons instead for keys.
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Likewise, if {@link #weakValues} or {@link #softValues} was specified, the map uses identity
841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * comparisons for values.
851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The view collections of the returned map have <i>weakly consistent iterators</i>. This means
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that they are safe for concurrent use, but if other threads modify the map after the iterator is
881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * created, it is undefined which of these changes, if any, are reflected in that iterator. These
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterators never throw {@link ConcurrentModificationException}.
901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>If soft or weak references were requested, it is possible for a key or value present in the
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the map to be reclaimed by the garbage collector. If this happens, the entry automatically
931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * disappears from the map. A partially-reclaimed entry is never exposed to the user. Any {@link
941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * java.util.Map.Entry} instance retrieved from the map's {@linkplain Map#entrySet entry set} is a
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * snapshot of that entry's state at the time of retrieval; such entries do, however, support {@link
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * java.util.Map.Entry#setValue}, which simply calls {@link Map#put} on the entry's key.
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The maps produced by {@code MapMaker} are serializable, and the deserialized maps retain all
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the configuration properties of the original map. During deserialization, if the original map had
1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * used soft or weak references, the entries are reconstructed as they were, but it's not unlikely
1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * they'll be quickly garbage-collected before they are ever accessed.
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>{@code new MapMaker().weakKeys().makeMap()} is a recommended replacement for {@link
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * java.util.WeakHashMap}, but note that it compares keys using object identity whereas {@code
1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WeakHashMap} uses {@link Object#equals}.
1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Bob Lee
1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Charles Fry
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic final class MapMaker extends GenericMapMaker<Object, Object> {
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static final int DEFAULT_INITIAL_CAPACITY = 16;
1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static final int DEFAULT_CONCURRENCY_LEVEL = 4;
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static final int DEFAULT_EXPIRATION_NANOS = 0;
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static final int UNSET_INT = -1;
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // TODO(kevinb): dispense with this after benchmarking
1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  boolean useCustomMap;
1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  int initialCapacity = UNSET_INT;
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  int concurrencyLevel = UNSET_INT;
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  int maximumSize = UNSET_INT;
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Strength keyStrength;
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Strength valueStrength;
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  long expireAfterWriteNanos = UNSET_INT;
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  long expireAfterAccessNanos = UNSET_INT;
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  RemovalCause nullRemovalCause;
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Equivalence<Object> keyEquivalence;
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Equivalence<Object> valueEquivalence;
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Ticker ticker;
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Constructs a new {@code MapMaker} instance with default settings, including strong keys, strong
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * values, and no automatic eviction of any kind.
1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker() {}
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private boolean useNullMap() {
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (nullRemovalCause == null);
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Sets a custom {@code Equivalence} strategy for comparing keys.
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>By default, the map uses {@link Equivalences#identity} to determine key equality when
1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #weakKeys} or {@link #softKeys} is specified, and {@link Equivalences#equals()}
1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * otherwise.
1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("To be supported")
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker keyEquivalence(Equivalence<Object> equivalence) {
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(keyEquivalence == null, "key equivalence was already set to %s", keyEquivalence);
1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    keyEquivalence = checkNotNull(equivalence);
1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.useCustomMap = true;
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Equivalence<Object> getKeyEquivalence() {
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return firstNonNull(keyEquivalence, getKeyStrength().defaultEquivalence());
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Sets a custom {@code Equivalence} strategy for comparing values.
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>By default, the map uses {@link Equivalences#identity} to determine value equality when
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #weakValues} or {@link #softValues} is specified, and {@link Equivalences#equals()}
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * otherwise.
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("To be supported")
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker valueEquivalence(Equivalence<Object> equivalence) {
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(valueEquivalence == null,
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        "value equivalence was already set to %s", valueEquivalence);
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.valueEquivalence = checkNotNull(equivalence);
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.useCustomMap = true;
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Equivalence<Object> getValueEquivalence() {
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return firstNonNull(valueEquivalence, getValueStrength().defaultEquivalence());
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Sets the minimum total size for the internal hash tables. For example, if the initial capacity
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is {@code 60}, and the concurrency level is {@code 8}, then eight segments are created, each
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * having a hash table of size eight. Providing a large enough estimate at construction time
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * avoids the need for expensive resizing operations later, but setting this value unnecessarily
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * high wastes memory.
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code initialCapacity} is negative
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if an initial capacity was already set
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker initialCapacity(int initialCapacity) {
2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(this.initialCapacity == UNSET_INT, "initial capacity was already set to %s",
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        this.initialCapacity);
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(initialCapacity >= 0);
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.initialCapacity = initialCapacity;
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  int getInitialCapacity() {
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (initialCapacity == UNSET_INT) ? DEFAULT_INITIAL_CAPACITY : initialCapacity;
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies the maximum number of entries the map may contain. Note that the map <b>may evict an
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * entry before this limit is exceeded</b>. As the map size grows close to the maximum, the map
2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * evicts entries that are less likely to be used again. For example, the map may evict an entry
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * because it hasn't been used recently or very often.
2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>When {@code size} is zero, elements can be successfully added to the map, but are evicted
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * immediately. This has the same effect as invoking {@link #expireAfterWrite
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * expireAfterWrite}{@code (0, unit)} or {@link #expireAfterAccess expireAfterAccess}{@code (0,
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * unit)}. It can be useful in testing, or to disable caching temporarily without a code change.
2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Caching functionality in {@code MapMaker} is being moved to
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link com.google.common.cache.CacheBuilder}.
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param size the maximum size of the map
2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code size} is negative
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if a maximum size was already set
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}, with {@link #maximumSize} being
2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     replaced by {@link com.google.common.cache.CacheBuilder#maximumSize}.
2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker maximumSize(int size) {
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(this.maximumSize == UNSET_INT, "maximum size was already set to %s",
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        this.maximumSize);
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(size >= 0, "maximum size must not be negative");
2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.maximumSize = size;
2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.useCustomMap = true;
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (maximumSize == 0) {
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // SIZE trumps EXPIRED
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.nullRemovalCause = RemovalCause.SIZE;
2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Guides the allowed concurrency among update operations. Used as a hint for internal sizing. The
2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * table is internally partitioned to try to permit the indicated number of concurrent updates
2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * without contention. Because assignment of entries to these partitions is not necessarily
2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * uniform, the actual concurrency observed may vary. Ideally, you should choose a value to
2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * accommodate as many threads as will ever concurrently modify the table. Using a significantly
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * higher value than you need can waste space and time, and a significantly lower value can lead
2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * to thread contention. But overestimates and underestimates within an order of magnitude do not
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * usually have much noticeable impact. A value of one permits only one thread to modify the map
2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * at a time, but since read operations can proceed concurrently, this still yields higher
2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * concurrency than full synchronization. Defaults to 4.
2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Note:</b> Prior to Guava release 9.0, the default was 16. It is possible the default will
2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * change again in the future. If you care about this value, you should always choose it
2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * explicitly.
2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code concurrencyLevel} is nonpositive
2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if a concurrency level was already set
2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker concurrencyLevel(int concurrencyLevel) {
2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(this.concurrencyLevel == UNSET_INT, "concurrency level was already set to %s",
2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        this.concurrencyLevel);
2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(concurrencyLevel > 0);
2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.concurrencyLevel = concurrencyLevel;
2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  int getConcurrencyLevel() {
2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (concurrencyLevel == UNSET_INT) ? DEFAULT_CONCURRENCY_LEVEL : concurrencyLevel;
2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each key (not value) stored in the map should be strongly referenced.
2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the key strength was already set
2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker strongKeys() {
2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setKeyStrength(Strength.STRONG);
2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each key (not value) stored in the map should be wrapped in a {@link
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * WeakReference} (by default, strong references are used).
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> when this method is used, the resulting map will use identity ({@code ==})
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * comparison to determine equality of keys, which is a technical violation of the {@link Map}
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * specification, and may not be what you expect.
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the key strength was already set
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see WeakReference
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.lang.ref.WeakReference")
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker weakKeys() {
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setKeyStrength(Strength.WEAK);
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <b>This method is broken.</b> Maps with soft keys offer no functional advantage over maps with
3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * weak keys, and they waste memory by keeping unreachable elements in the map. If your goal is to
3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * create a memory-sensitive map, then consider using soft values instead.
3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Specifies that each key (not value) stored in the map should be wrapped in a
3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link SoftReference} (by default, strong references are used). Softly-referenced objects will
3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * be garbage-collected in a <i>globally</i> least-recently-used manner, in response to memory
3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * demand.
3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> when this method is used, the resulting map will use identity ({@code ==})
3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * comparison to determine equality of keys, which is a technical violation of the {@link Map}
3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * specification, and may not be what you expect.
3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the key strength was already set
3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see SoftReference
3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated use {@link #softValues} to create a memory-sensitive map, or {@link #weakKeys} to
3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     create a map that doesn't hold strong references to the keys.
3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     <b>This method is scheduled for deletion in January 2013.</b>
3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.lang.ref.SoftReference")
3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker softKeys() {
3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setKeyStrength(Strength.SOFT);
3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker setKeyStrength(Strength strength) {
3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(keyStrength == null, "Key strength was already set to %s", keyStrength);
3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    keyStrength = checkNotNull(strength);
3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (strength != Strength.STRONG) {
3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // STRONG could be used during deserialization.
3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      useCustomMap = true;
3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
3441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Strength getKeyStrength() {
3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return firstNonNull(keyStrength, Strength.STRONG);
3481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each value (not key) stored in the map should be strongly referenced.
3521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the value strength was already set
3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
3561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker strongValues() {
3571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setValueStrength(Strength.STRONG);
3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each value (not key) stored in the map should be wrapped in a
3621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link WeakReference} (by default, strong references are used).
3631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Weak values will be garbage collected once they are weakly reachable. This makes them a poor
3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * candidate for caching; consider {@link #softValues} instead.
3661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> when this method is used, the resulting map will use identity ({@code ==})
3681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * comparison to determine equality of values. This technically violates the specifications of
3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the methods {@link Map#containsValue containsValue},
3701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link ConcurrentMap#remove(Object, Object) remove(Object, Object)} and
3711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link ConcurrentMap#replace(Object, Object, Object) replace(K, V, V)}, and may not be what you
3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * expect.
3731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the value strength was already set
3751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see WeakReference
3761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.lang.ref.WeakReference")
3781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker weakValues() {
3801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setValueStrength(Strength.WEAK);
3811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each value (not key) stored in the map should be wrapped in a
3851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link SoftReference} (by default, strong references are used). Softly-referenced objects will
3861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * be garbage-collected in a <i>globally</i> least-recently-used manner, in response to memory
3871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * demand.
3881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> in most circumstances it is better to set a per-cache {@linkplain
3901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * #maximumSize maximum size} instead of using soft references. You should only use this method if
3911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * you are well familiar with the practical consequences of soft references.
3921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> when this method is used, the resulting map will use identity ({@code ==})
3941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * comparison to determine equality of values. This technically violates the specifications of
3951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the methods {@link Map#containsValue containsValue},
3961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link ConcurrentMap#remove(Object, Object) remove(Object, Object)} and
3971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link ConcurrentMap#replace(Object, Object, Object) replace(K, V, V)}, and may not be what you
3981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * expect.
3991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the value strength was already set
4011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see SoftReference
4021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
4031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.lang.ref.SoftReference")
4041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
4051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public MapMaker softValues() {
4061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return setValueStrength(Strength.SOFT);
4071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker setValueStrength(Strength strength) {
4101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(valueStrength == null, "Value strength was already set to %s", valueStrength);
4111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    valueStrength = checkNotNull(strength);
4121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (strength != Strength.STRONG) {
4131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // STRONG could be used during deserialization.
4141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      useCustomMap = true;
4151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
4171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Strength getValueStrength() {
4201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return firstNonNull(valueStrength, Strength.STRONG);
4211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
4241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Old name of {@link #expireAfterWrite}.
4251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
4271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}. Functionality equivalent to
4281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link MapMaker#expiration} is provided by
4291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder#expireAfterWrite}.
4301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     <b>This method is scheduled for deletion in July 2012.</b>
4311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
4321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
4331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
4341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public
4351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker expiration(long duration, TimeUnit unit) {
4361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return expireAfterWrite(duration, unit);
4371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
4401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each entry should be automatically removed from the map once a fixed duration
4411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * has elapsed after the entry's creation, or the most recent replacement of its value.
4421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>When {@code duration} is zero, elements can be successfully added to the map, but are
4441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * evicted immediately. This has a very similar effect to invoking {@link #maximumSize
4451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * maximumSize}{@code (0)}. It can be useful in testing, or to disable caching temporarily without
4461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * a code change.
4471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Expired entries may be counted by {@link Map#size}, but will never be visible to read or
4491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * write operations. Expired entries are currently cleaned up during write operations, or during
4501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * occasional read operations in the absense of writes; though this behavior may change in the
4511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * future.
4521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param duration the length of time after an entry is created that it should be automatically
4541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     removed
4551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param unit the unit that {@code duration} is expressed in
4561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code duration} is negative
4571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the time to live or time to idle was already set
4581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
4591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}, with {@link #expireAfterWrite} being
4601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     replaced by {@link com.google.common.cache.CacheBuilder#expireAfterWrite}.
4611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
4621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
4631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
4641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker expireAfterWrite(long duration, TimeUnit unit) {
4651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkExpiration(duration, unit);
4661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.expireAfterWriteNanos = unit.toNanos(duration);
4671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (duration == 0 && this.nullRemovalCause == null) {
4681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // SIZE trumps EXPIRED
4691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.nullRemovalCause = RemovalCause.EXPIRED;
4701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
4711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    useCustomMap = true;
4721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
4731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private void checkExpiration(long duration, TimeUnit unit) {
4761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(expireAfterWriteNanos == UNSET_INT, "expireAfterWrite was already set to %s ns",
4771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        expireAfterWriteNanos);
4781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(expireAfterAccessNanos == UNSET_INT, "expireAfterAccess was already set to %s ns",
4791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        expireAfterAccessNanos);
4801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(duration >= 0, "duration cannot be negative: %s %s", duration, unit);
4811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  long getExpireAfterWriteNanos() {
4841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (expireAfterWriteNanos == UNSET_INT) ? DEFAULT_EXPIRATION_NANOS : expireAfterWriteNanos;
4851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
4881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies that each entry should be automatically removed from the map once a fixed duration
4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * has elapsed after the entry's last read or write access.
4901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>When {@code duration} is zero, elements can be successfully added to the map, but are
4921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * evicted immediately. This has a very similar effect to invoking {@link #maximumSize
4931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * maximumSize}{@code (0)}. It can be useful in testing, or to disable caching temporarily without
4941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * a code change.
4951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Expired entries may be counted by {@link Map#size}, but will never be visible to read or
4971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * write operations. Expired entries are currently cleaned up during write operations, or during
4981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * occasional read operations in the absense of writes; though this behavior may change in the
4991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * future.
5001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param duration the length of time after an entry is last accessed that it should be
5021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     automatically removed
5031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param unit the unit that {@code duration} is expressed in
5041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code duration} is negative
5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if the time to idle or time to live was already set
5061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
5071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}, with {@link #expireAfterAccess} being
5081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     replaced by {@link com.google.common.cache.CacheBuilder#expireAfterAccess}.
5091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
5111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("To be supported")
5121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
5131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  MapMaker expireAfterAccess(long duration, TimeUnit unit) {
5141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkExpiration(duration, unit);
5151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.expireAfterAccessNanos = unit.toNanos(duration);
5161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (duration == 0 && this.nullRemovalCause == null) {
5171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // SIZE trumps EXPIRED
5181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.nullRemovalCause = RemovalCause.EXPIRED;
5191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
5201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    useCustomMap = true;
5211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
5221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  long getExpireAfterAccessNanos() {
5251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (expireAfterAccessNanos == UNSET_INT)
5261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ? DEFAULT_EXPIRATION_NANOS : expireAfterAccessNanos;
5271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  Ticker getTicker() {
5301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return firstNonNull(ticker, Ticker.systemTicker());
5311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
5341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Specifies a listener instance, which all maps built using this {@code MapMaker} will notify
5351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each time an entry is removed from the map by any means.
5361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Each map built by this map maker after this method is called invokes the supplied listener
5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * after removing an element for any reason (see removal causes in {@link RemovalCause}). It will
5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * invoke the listener during invocations of any of that map's public methods (even read-only
5401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * methods).
5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Important note:</b> Instead of returning <i>this</i> as a {@code MapMaker} instance,
5431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * this method returns {@code GenericMapMaker<K, V>}. From this point on, either the original
5441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * reference or the returned reference may be used to complete configuration and build the map,
5451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * but only the "generic" one is type-safe. That is, it will properly prevent you from building
5461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * maps whose key or value types are incompatible with the types accepted by the listener already
5471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * provided; the {@code MapMaker} type cannot do this. For best results, simply use the standard
5481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * method-chaining idiom, as illustrated in the documentation at top, configuring a {@code
5491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * MapMaker} and building your {@link Map} all in a single statement.
5501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> if you ignore the above advice, and use this {@code MapMaker} to build a map
5521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or cache whose key or value type is incompatible with the listener, you will likely experience
5531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * a {@link ClassCastException} at some <i>undefined</i> point in the future.
5541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalStateException if a removal listener was already set
5561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
5571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}, with {@link #removalListener} being
5581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     replaced by {@link com.google.common.cache.CacheBuilder#removalListener}.
5591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
5611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("To be supported")
5621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  <K, V> GenericMapMaker<K, V> removalListener(RemovalListener<K, V> listener) {
5631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkState(this.removalListener == null);
5641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // safely limiting the kinds of maps this can produce
5661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @SuppressWarnings("unchecked")
5671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    GenericMapMaker<K, V> me = (GenericMapMaker<K, V>) this;
5681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    me.removalListener = checkNotNull(listener);
5691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    useCustomMap = true;
5701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return me;
5711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
5741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Builds a thread-safe map, without on-demand computation of values. This method does not alter
5751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the state of this {@code MapMaker} instance, so it can be invoked again to create multiple
5761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * independent maps.
5771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The bulk operations {@code putAll}, {@code equals}, and {@code clear} are not guaranteed to
5791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * be performed atomically on the returned map. Additionally, {@code size} and {@code
5801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * containsValue} are implemented as bulk read operations, and thus may fail to observe concurrent
5811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * writes.
5821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a serializable concurrent map having the requested features
5841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
5861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public <K, V> ConcurrentMap<K, V> makeMap() {
5871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (!useCustomMap) {
5881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return new ConcurrentHashMap<K, V>(getInitialCapacity(), 0.75f, getConcurrencyLevel());
5891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
5901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (nullRemovalCause == null)
5911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ? new MapMakerInternalMap<K, V>(this)
5921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        : new NullConcurrentMap<K, V>(this);
5931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
5961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a MapMakerInternalMap for the benefit of internal callers that use features of
5971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * that class not exposed through ConcurrentMap.
5981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
6001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("MapMakerInternalMap")
6011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  <K, V> MapMakerInternalMap<K, V> makeCustomMap() {
6021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new MapMakerInternalMap<K, V>(this);
6031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
6041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
6061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Builds a map that supports atomic, on-demand computation of values. {@link Map#get} either
6071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returns an already-computed value for the given key, atomically computes it using the supplied
6081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * function, or, if another thread is currently computing the value for this key, simply waits for
6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * that thread to finish and returns its computed value. Note that the function may be executed
6101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * concurrently by multiple threads, but only for distinct keys.
6111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>New code should use {@link com.google.common.cache.CacheBuilder}, which supports
6131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@linkplain com.google.common.cache.CacheStats statistics} collection, introduces the
6141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link com.google.common.cache.CacheLoader} interface for loading entries into the cache
6151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * (allowing checked exceptions to be thrown in the process), and more cleanly separates
6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * computation from the cache's {@code Map} view.
6171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>If an entry's value has not finished computing yet, query methods besides {@code get} return
6191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * immediately as if an entry doesn't exist. In other words, an entry isn't externally visible
6201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * until the value's computation completes.
6211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>{@link Map#get} on the returned map will never return {@code null}. It may throw:
6231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <ul>
6251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>{@link NullPointerException} if the key is null or the computing function returns a null
6261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     result
6271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>{@link ComputationException} if an exception was thrown by the computing function. If that
6281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * exception is already of type {@link ComputationException} it is propagated directly; otherwise
6291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * it is wrapped.
6301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * </ul>
6311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Note:</b> Callers of {@code get} <i>must</i> ensure that the key argument is of type
6331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code K}. The {@code get} method accepts {@code Object}, so the key type is not checked at
6341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * compile time. Passing an object of a type other than {@code K} can result in that object being
6351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * unsafely passed to the computing function as type {@code K}, and unsafely stored in the map.
6361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>If {@link Map#put} is called before a computation completes, other threads waiting on the
6381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * computation will wake up and return the stored value.
6391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>This method does not alter the state of this {@code MapMaker} instance, so it can be invoked
6411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * again to create multiple independent maps.
6421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Insertion, removal, update, and access operations on the returned map safely execute
6441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * concurrently by multiple threads. Iterators on the returned map are weakly consistent,
6451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returning elements reflecting the state of the map at some point at or since the creation of
6461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iterator. They do not throw {@link ConcurrentModificationException}, and may proceed
6471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * concurrently with other operations.
6481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The bulk operations {@code putAll}, {@code equals}, and {@code clear} are not guaranteed to
6501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * be performed atomically on the returned map. Additionally, {@code size} and {@code
6511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * containsValue} are implemented as bulk read operations, and thus may fail to observe concurrent
6521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * writes.
6531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param computingFunction the function used to compute new values
6551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a serializable concurrent map having the requested features
6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Caching functionality in {@code MapMaker} is being moved to
6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link com.google.common.cache.CacheBuilder}, with {@link #makeComputingMap} being replaced
6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     by {@link com.google.common.cache.CacheBuilder#build}. Note that uses of
6591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link #makeComputingMap} with {@code AtomicLong} values can often be migrated to
6601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link AtomicLongMap}.
6611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     <b>This method is scheduled for deletion in February 2013.</b>
6621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
6641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
6651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
6661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public <K, V> ConcurrentMap<K, V> makeComputingMap(
6671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Function<? super K, ? extends V> computingFunction) {
6681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return useNullMap()
6691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ? new ComputingMapAdapter<K, V>(this, computingFunction)
6701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        : new NullComputingConcurrentMap<K, V>(this, computingFunction);
6711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
6721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
6741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a string representation for this MapMaker instance. The exact form of the returned
6751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * string is not specificed.
6761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
6771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
6781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public String toString() {
6791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Objects.ToStringHelper s = Objects.toStringHelper(this);
6801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (initialCapacity != UNSET_INT) {
6811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("initialCapacity", initialCapacity);
6821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (concurrencyLevel != UNSET_INT) {
6841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("concurrencyLevel", concurrencyLevel);
6851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (maximumSize != UNSET_INT) {
6871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("maximumSize", maximumSize);
6881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (expireAfterWriteNanos != UNSET_INT) {
6901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("expireAfterWrite", expireAfterWriteNanos + "ns");
6911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (expireAfterAccessNanos != UNSET_INT) {
6931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("expireAfterAccess", expireAfterAccessNanos + "ns");
6941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (keyStrength != null) {
6961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("keyStrength", Ascii.toLowerCase(keyStrength.toString()));
6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (valueStrength != null) {
6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.add("valueStrength", Ascii.toLowerCase(valueStrength.toString()));
7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (keyEquivalence != null) {
7021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.addValue("keyEquivalence");
7031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (valueEquivalence != null) {
7051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.addValue("valueEquivalence");
7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (removalListener != null) {
7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      s.addValue("removalListener");
7091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return s.toString();
7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An object that can receive a notification when an entry is removed from a map. The removal
7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * resulting in notification could have occured to an entry being manually removed or replaced, or
7161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * due to eviction resulting from timed expiration, exceeding a maximum size, or garbage
7171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * collection.
7181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>An instance may be called concurrently by multiple threads to process different entries.
7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Implementations of this interface should avoid performing blocking calls or synchronizing on
7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * shared resources.
7221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param <K> the most general type of keys this listener can listen for; for
7241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     example {@code Object} if any key is acceptable
7251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param <V> the most general type of values this listener can listen for; for
7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     example {@code Object} if any key is acceptable
7271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  interface RemovalListener<K, V> {
7291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Notifies the listener that a removal occurred at some point in the past.
7311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    void onRemoval(RemovalNotification<K, V> notification);
7331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * A notification of the removal of a single entry. The key or value may be null if it was already
7371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * garbage collected.
7381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Like other {@code Map.Entry} instances associated with MapMaker, this class holds strong
7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * references to the key and value, regardless of the type of references the map may be using.
7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static final class RemovalNotification<K, V> extends ImmutableEntry<K, V> {
7431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private static final long serialVersionUID = 0;
7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private final RemovalCause cause;
7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    RemovalNotification(@Nullable K key, @Nullable V value, RemovalCause cause) {
7481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super(key, value);
7491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.cause = cause;
7501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Returns the cause for which the entry was removed.
7541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public RemovalCause getCause() {
7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return cause;
7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}).
7621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
7631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean wasEvicted() {
7641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return cause.wasEvicted();
7651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The reason why an entry was removed.
7701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  enum RemovalCause {
7721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The entry was manually removed by the user. This can result from the user invoking
7741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@link Map#remove}, {@link ConcurrentMap#remove}, or {@link java.util.Iterator#remove}.
7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    EXPLICIT {
7771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      boolean wasEvicted() {
7791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return false;
7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
7811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    },
7821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The entry itself was not actually removed, but its value was replaced by the user. This can
7851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * result from the user invoking {@link Map#put}, {@link Map#putAll},
7861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@link ConcurrentMap#replace(Object, Object)}, or
7871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@link ConcurrentMap#replace(Object, Object, Object)}.
7881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
7891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    REPLACED {
7901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
7911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      boolean wasEvicted() {
7921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return false;
7931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
7941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    },
7951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
7971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The entry was removed automatically because its key or value was garbage-collected. This
7981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * can occur when using {@link #softKeys}, {@link #softValues}, {@link #weakKeys}, or {@link
7991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * #weakValues}.
8001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
8011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    COLLECTED {
8021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
8031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      boolean wasEvicted() {
8041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return true;
8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
8061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    },
8071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
8091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The entry's expiration timestamp has passed. This can occur when using {@link
8101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * #expireAfterWrite} or {@link #expireAfterAccess}.
8111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
8121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    EXPIRED {
8131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      boolean wasEvicted() {
8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return true;
8161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
8171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    },
8181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
8201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The entry was evicted due to size constraints. This can occur when using {@link
8211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * #maximumSize}.
8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    SIZE {
8241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
8251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      boolean wasEvicted() {
8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return true;
8271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
8281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither
8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@link #EXPLICIT} nor {@link #REPLACED}).
8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    abstract boolean wasEvicted();
8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** A map that is always empty and evicts on insertion. */
8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static class NullConcurrentMap<K, V> extends AbstractMap<K, V>
8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      implements ConcurrentMap<K, V>, Serializable {
8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private static final long serialVersionUID = 0;
8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private final RemovalListener<K, V> removalListener;
8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private final RemovalCause removalCause;
8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    NullConcurrentMap(MapMaker mapMaker) {
8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      removalListener = mapMaker.getRemovalListener();
8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      removalCause = mapMaker.nullRemovalCause;
8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // implements ConcurrentMap
8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean containsKey(@Nullable Object key) {
8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean containsValue(@Nullable Object value) {
8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V get(@Nullable Object key) {
8641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    void notifyRemoval(K key, V value) {
8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      RemovalNotification<K, V> notification =
8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          new RemovalNotification<K, V>(key, value, removalCause);
8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      removalListener.onRemoval(notification);
8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V put(K key, V value) {
8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(key);
8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(value);
8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      notifyRemoval(key, value);
8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V putIfAbsent(K key, V value) {
8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return put(key, value);
8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V remove(@Nullable Object key) {
8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean remove(@Nullable Object key, @Nullable Object value) {
8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V replace(K key, V value) {
8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(key);
8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(value);
9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean replace(K key, @Nullable V oldValue, V newValue) {
9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(key);
9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(newValue);
9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
9081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public Set<Entry<K, V>> entrySet() {
9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return Collections.emptySet();
9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** Computes on retrieval and evicts the result. */
9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static final class NullComputingConcurrentMap<K, V> extends NullConcurrentMap<K, V> {
9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private static final long serialVersionUID = 0;
9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Function<? super K, ? extends V> computingFunction;
9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    NullComputingConcurrentMap(
9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        MapMaker mapMaker, Function<? super K, ? extends V> computingFunction) {
9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super(mapMaker);
9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.computingFunction = checkNotNull(computingFunction);
9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @SuppressWarnings("unchecked") // unsafe, which is why Cache is preferred
9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public V get(Object k) {
9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      K key = (K) k;
9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      V value = compute(key);
9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(value, computingFunction + " returned null for key " + key + ".");
9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      notifyRemoval(key, value);
9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return value;
9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private V compute(K key) {
9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      checkNotNull(key);
9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      try {
9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return computingFunction.apply(key);
9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (ComputationException e) {
9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        throw e;
9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (Throwable t) {
9451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        throw new ComputationException(t);
9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
951