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);
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.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final ReferencedKeyMap | map
Backing |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | add(T e)
Overrides java. Implements java. Adds the specified element to this set if it is not already present (optional operation). |
| public void | clear()
Overrides java. Implements java. Removes all of the elements from this set (optional operation). |
| public static < the type of elements maintained by this set E> Supplier | concurrentHashMapSupplier()
Returns: a supplier to create a |
| public boolean | contains(Object o)
Overrides java. Implements java. Returns |
| public static < the type of elements maintained by this set E> ReferencedKeySet | Returns: a new set withReference elementstrue if isSoft, Supplier<Map<ReferenceKey<E>, ReferenceKey<E>>> SoftReference elements are to
be used, WeakReference otherwise.Supplier of the backing mapCreate a new |
| public T | Returns: the old element if present, otherwise, nullelement 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 elementelement 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 elementelement 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. Implements java. Returns |
| public Iterator | iterator()
Implements abstract java. Implements java. Returns an iterator over the elements in this set. |
| public void | |
| public boolean | remove(Object o)
Overrides java. Implements java. Removes the specified element from this set if it is present (optional operation). |
| public void | |
| public int | size()
Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality). |
| map | back to summary |
|---|---|
| pack-priv final ReferencedKeyMap<T, ReferenceKey<T>> map Backing | |
| ReferencedKeySet | back to summary |
|---|---|
| private ReferencedKeySet(ReferencedKeyMap<T, ReferenceKey<T>> map) Private constructor.
| |
| add | back to summary |
|---|---|
| public boolean add(T e) Overrides java. Implements java. Doc from java. Adds the specified element to this set if it is not already present
(optional operation). More formally, adds the specified element
The stipulation above does not imply that sets must accept all
elements; sets may refuse to add any particular element, including
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this set (optional operation). The set will be empty after this call returns.
| |
| concurrentHashMapSupplier | back to summary |
|---|---|
public static <E> Supplier
| |
| contains | back to summary |
|---|---|
| public boolean contains(Object o) Overrides java. Implements java. Doc from java. Returns
| |
| create | back to summary |
|---|---|
| public static <E> ReferencedKeySet Create a new
| |
| get | back 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.
| |
| intern | back 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.
| |
| intern | back 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.
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides java. Implements java. Doc from java. Returns
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. Doc from java. 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).
| |
| prepareForAOTCache | back to summary |
|---|---|
| public void prepareForAOTCache() | |
| remove | back to summary |
|---|---|
| public boolean remove(Object o) Overrides java. Implements java. Doc from java. Removes the specified element from this set if it is present
(optional operation). More formally, removes an element
| |
| removeStaleReferences | back to summary |
|---|---|
| public void removeStaleReferences() Removes enqueued weak references from set. | |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this set (its cardinality). If this
set contains more than
| |