Class CheckedHashSet<E>

Type Parameters:
E - the type of elements in the set.
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>, CheckedContainer<E>

public final class CheckedHashSet<E> extends LinkedHashSet<E> implements CheckedContainer<E>
A checked LinkedHashSet. The type checks are performed at run-time in addition to the compile-time checks.

Using this class is similar to wrapping a LinkedHashSet using the methods provided in the standard Collections class, except for the following differences:

  • Avoid one level of indirection.
  • Does not accept null elements.
Since:
0.3
Version:
0.4
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial version UID for compatibility with different versions.
      See Also:
    • type

      private final Class<E> type
      The element type.
  • Constructor Details

    • CheckedHashSet

      public CheckedHashSet(Class<E> type)
      Constructs a set of the specified type.
      Parameters:
      type - the element type (cannot be null).
    • CheckedHashSet

      public CheckedHashSet(Class<E> type, int capacity)
      Constructs a set of the specified type and initial capacity.
      Parameters:
      type - the element type (should not be null).
      capacity - the initial capacity.
  • Method Details

    • getElementType

      public Class<E> getElementType()
      Returns the element type given at construction time.
      Specified by:
      getElementType in interface CheckedContainer<E>
      Returns:
      the element type.
    • add

      public boolean add(E element) throws IllegalArgumentException
      Adds the specified element to this set if it is not already present.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class HashSet<E>
      Parameters:
      element - element to be added to this set.
      Returns:
      true if the set did not already contain the specified element.
      Throws:
      IllegalArgumentException - if the specified element is not of the expected type.