Class WeakHashSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.apache.sis.util.collection.WeakHashSet<E>
Type Parameters:
E - the type of elements in the set.
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>, CheckedContainer<E>

public class WeakHashSet<E> extends AbstractSet<E> implements CheckedContainer<E>
A set of objects hold by weak references. An entry in a WeakHashSet will automatically be removed when it is no longer in ordinary use. More precisely, the presence of an entry will not prevent the entry from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When an entry has been discarded it is effectively removed from the set, so this class behaves somewhat differently than other Set implementations.

If the elements stored in this set are arrays like int[], float[] or Object[], then the hash code computations and the comparisons are performed using the static hashCode(a) and equals(a1, a2) methods defined in the Arrays class.

Optimizing memory use in factory implementations

The WeakHashSet class has a get(Object) method that is not part of the Set interface. This get method retrieves an entry from this set that is equal to the supplied object. The unique(Object) method combines a get followed by a add operation if the specified object was not in the set. This is similar in spirit to the String.intern() method. The following example shows a convenient way to use WeakHashSet as an internal pool of immutable objects: Thus, WeakHashSet can be used inside a factory to prevent creating duplicate immutable objects.

Thread safety

The same WeakHashSet instance can be safely used by many threads without synchronization on the part of the caller. But if a sequence of two or more method calls need to appear atomic from other threads perspective, then the caller can synchronize on this.
Since:
0.3
Version:
0.3
See Also:
  • Field Details

    • table

      private WeakHashSet<E>.Entry[] table
      Table of weak references.
    • count

      private int count
      Number of non-null elements in table. This is used for determining when WeakEntry.rehash(WeakEntry[], int, String) needs to be invoked.
    • elementType

      private final Class<E> elementType
      The type of the elements in this set.
    • mayContainArrays

      private final boolean mayContainArrays
      true if the elements in this set may be arrays. If the elements cannot be arrays, then we can avoid the calls to the costly Utilities methods.
    • lastTimeNormalCapacity

      private transient long lastTimeNormalCapacity
      The last time when table was not in need for rehash. When the garbage collector collected a lot of elements, we will wait a few seconds before rehashing table in case lot of news elements are going to be added. Without this field, we noticed many "reduce", "expand", "reduce", "expand", etc. cycles.
    • REMOVE

      private static final int REMOVE
      The "remove" operation.
      See Also:
    • GET

      private static final int GET
      The "get" operation.
      See Also:
    • ADD

      private static final int ADD
      The "add" operation.
      See Also:
    • INTERN

      private static final int INTERN
      The "intern" operation.
      See Also:
  • Constructor Details

    • WeakHashSet

      public WeakHashSet(Class<E> type)
      Creates a WeakHashSet for elements of the specified type.
      Parameters:
      type - the type of the element to be included in this set.
  • Method Details

    • getElementType

      public Class<E> getElementType()
      Returns the type of elements in this set.
      Specified by:
      getElementType in interface CheckedContainer<E>
      Returns:
      the element type.
    • removeEntry

      private void removeEntry(WeakHashSet<E>.Entry toRemove)
      Invoked by WeakHashSet<E>.Entry when an element has been collected by the garbage collector. This method removes the weak reference from the table.
      Parameters:
      toRemove - the entry to remove from this map.
    • isValid

      @Debug private boolean isValid()
      Checks if this WeakHashSet is valid. This method counts the number of elements and compares it to count. This method is invoked in assertions only.
      Returns:
      whether count matches the expected value.
    • size

      public int size()
      Returns the count of element in this set.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      number of elements in this set.
    • add

      public boolean add(E element) throws NullArgumentException
      Adds the specified element to this set if it is not already present. If this set already contains the specified element, the call leaves this set unchanged and returns false.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
      Parameters:
      element - element to be added to this set.
      Returns:
      true if this set did not already contain the specified element.
      Throws:
      NullArgumentException - if the given object is null.
    • remove

      public boolean remove(Object element)
      Removes a single instance of the specified element from this set, if it is present Null values are considered never present.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      element - element to be removed from this set, if present. Can be null.
      Returns:
      true if the set contained the specified element.
    • get

      public E get(Object element)
      Returns an object equals to the specified object, if present. If this set doesn't contain any object equals to element, then this method returns null. Null values are considered never present.
      Parameters:
      element - the element to get.
      Returns:
      an element equals to the given one if already presents in the set, or null otherwise.
      See Also:
    • contains

      public boolean contains(Object element)
      Returns true if this set contains the specified element. Null values are considered never present.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      element - object to be checked for containment in this set. Can be null.
      Returns:
      true if this set contains the specified element.
    • unique

      public <T extends E> T unique(T element)
      Returns an object equals to element if such an object already exist in this WeakHashSet. Otherwise, adds element to this WeakHashSet. This method is functionally equivalents to the following code:
      Type Parameters:
      T - the type of the element to get. Can be null.
      Parameters:
      element - the element to get or to add in the set if not already presents, or null if the given element was null.
      Returns:
      an element equals to the given one if already presents in the set, or the given object otherwise.
    • intern

      private E intern(Object obj, int operation)
    • clear

      public void clear()
      Removes all of the elements from this set.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
    • toArray

      public E[] toArray()
      Returns a view of this set as an array. Elements will be in an arbitrary order. Note that this array contains strong references. Consequently, no object reclamation will occur as long as a reference to this array is hold.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Returns:
      all elements in this set.
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements contained in this collection. No element from this set will be garbage collected as long as a reference to the iterator is hold.
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
      Returns:
      an iterator over all elements in this set.