diff options
Diffstat (limited to 'guava/src/com/google/common/collect/Interners.java')
-rw-r--r-- | guava/src/com/google/common/collect/Interners.java | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/guava/src/com/google/common/collect/Interners.java b/guava/src/com/google/common/collect/Interners.java index bd592d6..f5a6c85 100644 --- a/guava/src/com/google/common/collect/Interners.java +++ b/guava/src/com/google/common/collect/Interners.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtIncompatible; -import com.google.common.base.Equivalence; +import com.google.common.base.Equivalences; import com.google.common.base.Function; import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; @@ -51,24 +51,16 @@ public final class Interners { }; } - /** - * Returns a new thread-safe interner which retains a weak reference to each instance it has - * interned, and so does not prevent these instances from being garbage-collected. This most - * likely does not perform as well as {@link #newStrongInterner}, but is the best alternative - * when the memory usage of that implementation is unacceptable. Note that unlike {@link - * String#intern}, using this interner does not consume memory in the permanent generation. - */ - @GwtIncompatible("java.lang.ref.WeakReference") - public static <E> Interner<E> newWeakInterner() { - return new WeakInterner<E>(); - } - - private static class WeakInterner<E> implements Interner<E> { + private static class CustomInterner<E> implements Interner<E> { // MapMaker is our friend, we know about this type - private final MapMakerInternalMap<E, Dummy> map = new MapMaker() - .weakKeys() - .keyEquivalence(Equivalence.equals()) + private final MapMakerInternalMap<E, Dummy> map; + + CustomInterner(GenericMapMaker<? super E, Object> mm) { + this.map = mm + .strongValues() + .keyEquivalence(Equivalences.equals()) .makeCustomMap(); + } @Override public E intern(E sample) { while (true) { @@ -100,6 +92,18 @@ public final class Interners { } /** + * Returns a new thread-safe interner which retains a weak reference to each instance it has + * interned, and so does not prevent these instances from being garbage-collected. This most + * likely does not perform as well as {@link #newStrongInterner}, but is the best alternative + * when the memory usage of that implementation is unacceptable. Note that unlike {@link + * String#intern}, using this interner does not consume memory in the permanent generation. + */ + @GwtIncompatible("java.lang.ref.WeakReference") + public static <E> Interner<E> newWeakInterner() { + return new CustomInterner<E>(new MapMaker().weakKeys()); + } + + /** * Returns a function that delegates to the {@link Interner#intern} method of the given interner. * * @since 8.0 @@ -125,7 +129,7 @@ public final class Interners { } @Override public boolean equals(Object other) { - if (other instanceof InternerFunction) { + if (other instanceof InternerFunction<?>) { InternerFunction<?> that = (InternerFunction<?>) other; return interner.equals(that.interner); } |