Module com.github.benmanes.caffeine
Interface Interner<E>
-
- Type Parameters:
E- the type of elements
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Interner<E>
Provides similar behavior toString.intern()for any immutable type.Note that
String.intern()has some well-known performance limitations, and should generally be avoided. PrefernewWeakInterner()or anotherInternerimplementation even forStringinterning.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description Eintern(E sample)Chooses and returns the representative instance for any of a collection of instances that are equal to each other.static <E> Interner<E>newStrongInterner()Returns a new thread-safe interner which retains a strong reference to each instance it has interned, thus preventing these instances from being garbage-collected.static <E> Interner<E>newWeakInterner()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.
-
-
-
Method Detail
-
intern
E intern(E sample)
Chooses and returns the representative instance for any of a collection of instances that are equal to each other. If two equal inputs are given to this method, both calls will return the same instance. That is,intern(a).equals(a)always holds, andintern(a) == intern(b)if and only ifa.equals(b). Note thatintern(a)is permitted to return one instance now and a different instance later if the original interned instance was garbage-collected.Warning: do not use with mutable objects.
- Parameters:
sample- the element to add if absent- Returns:
- the representative instance, possibly the
sampleif absent - Throws:
NullPointerException- ifsampleis null
-
newStrongInterner
static <E> Interner<E> newStrongInterner()
Returns a new thread-safe interner which retains a strong reference to each instance it has interned, thus preventing these instances from being garbage-collected.- Type Parameters:
E- the type of elements- Returns:
- an interner for retrieving the canonical instance
-
newWeakInterner
static <E> Interner<E> newWeakInterner()
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.- Type Parameters:
E- the type of elements- Returns:
- an interner for retrieving the canonical instance
-
-