Top Description Methods
com.github.benmanes.caffeine.cache

public Interface Interner<E>

Additional top-level classes in compilation unit: StrongInterner, WeakInterner, Interned.

Known Direct Implementers
com.github.benmanes.caffeine.cache.StrongInterner, com.github.benmanes.caffeine.cache.WeakInterner
Annotations
@FunctionalInterface
Type Parameters
<E>
the type of elements
Imports
java.lang.ref.Reference, .ReferenceQueue, java.util.Objects, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, com.github.benmanes.caffeine.cache.References.LookupKeyEqualsReference, .References.WeakKeyEqualsReference

Provides similar behavior to String#intern for any immutable type.

Note that String.intern() has some well-known performance limitations and should generally be avoided. Prefer Interner#newWeakInterner or another Interner implementation even for String interning.

Author
[email protected] (Ben Manes)

Method Summary

Modifier and TypeMethod and Description
public E

Returns:

the representative instance, possibly the sample if absent
intern
(E
the element to add if absent
sample
)

Chooses and returns the representative instance for any collection of instances that are equal to each other.

public static <
the type of elements
E
>
Interner<E>

Returns:

an interner for retrieving the canonical instance
newStrongInterner
()

Returns a new thread-safe interner that retains a strong reference to each instance it has interned, thus preventing these instances from being garbage-collected.

public static <
the type of elements
E
>
Interner<E>

Returns:

an interner for retrieving the canonical instance
newWeakInterner
()

Returns a new thread-safe interner that retains a weak reference to each instance it has interned, and so does not prevent these instances from being garbage-collected.

Method Detail

internback to summary
public E intern(E sample)

Chooses and returns the representative instance for any 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, and intern(a) == intern(b) if and only if a.equals(b). Note that intern(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:E

the element to add if absent

Returns:E

the representative instance, possibly the sample if absent

Exceptions
NullPointerException:
if sample is null
newStrongInternerback to summary
public static <E> Interner<E> newStrongInterner()

Returns a new thread-safe interner that retains a strong reference to each instance it has interned, thus preventing these instances from being garbage-collected.

Parameters
<E>
the type of elements
Returns:Interner<E>

an interner for retrieving the canonical instance

newWeakInternerback to summary
public static <E> Interner<E> newWeakInterner()

Returns a new thread-safe interner that retains a weak reference to each instance it has interned, and so does not prevent these instances from being garbage-collected.

Parameters
<E>
the type of elements
Returns:Interner<E>

an interner for retrieving the canonical instance