APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.internal.util

public final Class ReferencedKeySet<T>

extends AbstractSet<T>
Class Inheritance
Type Parameters
<T>
the type of elements maintained by this set
Imports
java.lang.ref.Reference, .ReferenceQueue, .SoftReference, .WeakReference, java.util.*, java.util.concurrent.ConcurrentHashMap, java.util.function.Supplier, .UnaryOperator

This class provides management of set where it is desirable to remove elements automatically when the element is garbage collected. This is accomplished by using a backing map where the keys and values are either a WeakReference or a SoftReference.

To create a ReferencedKeySet the user must provide a Supplier of the backing map and whether WeakReference or SoftReference is to be used.

Set<Long> set; set = ReferencedKeySet.create(false, HashMap::new); set.add(30_000_000L); set.add(30_000_001L); set.add(30_000_002L); set.add(30_000_003L); set.add(30_000_004L); set = ReferencedKeySet.create(true, ConcurrentHashMap::new); set.add(40_000_000L); set.add(40_000_001L); set.add(40_000_002L); set.add(40_000_003L); set.add(40_000_004L);
Set<Long> set;

set = ReferencedKeySet.create(false, HashMap::new);
set.add(30_000_000L);
set.add(30_000_001L);
set.add(30_000_002L);
set.add(30_000_003L);
set.add(30_000_004L);

set = ReferencedKeySet.create(true, ConcurrentHashMap::new);
set.add(40_000_000L);
set.add(40_000_001L);
set.add(40_000_002L);
set.add(40_000_003L);
set.add(40_000_004L);

Implementation Note

Care must be given that the backing map does replacement by replacing the value in the map entry instead of deleting the old entry and adding a new entry, otherwise replaced entries may end up with a strongly referenced key. HashMap and ConcurrentHashMap are known to be safe.

Field Summary

Modifier and TypeField and Description
pack-priv final ReferencedKeyMap<T, ReferenceKey<T>>
map

Backing ReferencedKeySet map.

Constructor Summary

AccessConstructor and Description
private
ReferencedKeySet(ReferencedKeyMap<T, ReferenceKey<T>>
backing map
map
)

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
add(T e)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

Adds the specified element to this set if it is not already present (optional operation).

public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Removes all of the elements from this set (optional operation).

public static <
the type of elements maintained by this set
E
>
Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>>
concurrentHashMapSupplier()

Returns: a supplier to create a ConcurrentHashMap appropriate for use in the create methods.

public boolean
contains(Object o)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Returns true if this set contains the specified element.

public static <
the type of elements maintained by this set
E
>
ReferencedKeySet<E>

Returns:

a new set with Reference elements
create
(boolean
true if SoftReference elements are to be used, WeakReference otherwise.
isSoft
,
Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>>
Supplier of the backing map
supplier
)

Create a new ReferencedKeySet elements.

public T

Returns:

the old element if present, otherwise, null
get
(T
element to get
e
)

Gets an existing element from the set, returning null if not present or the old element if previously added.

public T

Returns:

the old element if present, otherwise, the new element
intern
(T
element to add
e
)

Intern an element to the set, returning the element if newly added or the old element if previously added.

public T

Returns:

the old element if present, otherwise, the new element
intern
(T
element to add
e
,
UnaryOperator<T>
operation to apply to key before adding to set
interner
)

Intern an element to the set, returning the element if newly added or the old element if previously added.

public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Returns true if this set contains no elements.

public Iterator<T>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.

public void
public boolean
remove(Object o)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

Removes the specified element from this set if it is present (optional operation).

public void
removeStaleReferences()

Removes enqueued weak references from set.

public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set (its cardinality).

Inherited from java.util.AbstractSet:
equalshashCoderemoveAll

Field Detail

mapback to summary
pack-priv final ReferencedKeyMap<T, ReferenceKey<T>> map

Backing ReferencedKeySet map.

Constructor Detail

ReferencedKeySetback to summary
private ReferencedKeySet(ReferencedKeyMap<T, ReferenceKey<T>> map)

Private constructor.

Parameters
map:ReferencedKeyMap<T, ReferenceKey<T>>

backing map

Method Detail

addback to summary
public boolean add(T e)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

Doc from java.util.Set.add.

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

Annotations
@Override
clearback to summary
public void clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Doc from java.util.Set.clear.

Removes all of the elements from this set (optional operation). The set will be empty after this call returns.

Annotations
@Override
concurrentHashMapSupplierback to summary
public static <E> Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>> concurrentHashMapSupplier()
Parameters
<E>
the type of elements maintained by this set
Returns:Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>>

a supplier to create a ConcurrentHashMap appropriate for use in the create methods.

containsback to summary
public boolean contains(Object o)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Doc from java.util.Set.contains.

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Annotations
@Override
@SuppressWarnings:unchecked
createback to summary
public static <E> ReferencedKeySet<E> create(boolean isSoft, Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>> supplier)

Create a new ReferencedKeySet elements.

Parameters
<E>
the type of elements maintained by this set
isSoft:boolean

true if SoftReference elements are to be used, WeakReference otherwise.

supplier:Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>>

Supplier of the backing map

Returns:ReferencedKeySet<E>

a new set with Reference elements

getback to summary
public T get(T e)

Gets an existing element from the set, returning null if not present or the old element if previously added.

Parameters
e:T

element to get

Returns:T

the old element if present, otherwise, null

internback to summary
public T intern(T e)

Intern an element to the set, returning the element if newly added or the old element if previously added.

Parameters
e:T

element to add

Returns:T

the old element if present, otherwise, the new element

internback to summary
public T intern(T e, UnaryOperator<T> interner)

Intern an element to the set, returning the element if newly added or the old element if previously added.

Implementation Note

This version of intern should not be called during phase1 using a lambda. Use an UnaryOperator instance instead.

Parameters
e:T

element to add

interner:UnaryOperator<T>

operation to apply to key before adding to set

Returns:T

the old element if present, otherwise, the new element

isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Doc from java.util.Set.isEmpty.

Returns true if this set contains no elements.

Annotations
@Override
iteratorback to summary
public Iterator<T> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Annotations
@Override
prepareForAOTCacheback to summary
public void prepareForAOTCache()
removeback to summary
public boolean remove(Object o)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

Doc from java.util.Set.remove.

Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Annotations
@Override
removeStaleReferencesback to summary
public void removeStaleReferences()

Removes enqueued weak references from set.

sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Doc from java.util.Set.size.

Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Annotations
@Override