Class DerivedSet<S,E>

Type Parameters:
S - the type of elements in the storage set.
E - the type of elements in this set.
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>, CheckedContainer<E>
Direct Known Subclasses:
DerivedSet.Invertible

class DerivedSet<S,E> extends SetOfUnknownSize<E> implements CheckedContainer<E>, Serializable
A set whose values are derived on-the-fly from another set. Conversions are performed when needed by two converters:

Constraints

  • This set does not support null values, since null is used as a sentinel value when no mapping from storage to this exists.
  • Instances of this class are serializable if their underlying storage set and the converter are serializable.
  • This class performs no synchronization by itself. Nevertheless instances of this class may be thread-safe (depending on the sub-class implementation) if the underlying storage set (including its iterator) and the converter are thread-safe.

Performance considerations

This class does not cache any value, since the storage set is presumed modifiable. If the storage set is known to be immutable, then sub-classes may consider to cache some values, especially the result of the SetOfUnknownSize.size() method.
Since:
0.3
Version:
1.0
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial number for inter-operability with different versions.
      See Also:
    • storage

      protected final Set<S> storage
      The storage set whose values are derived from.
    • converter

      protected final ObjectConverter<S,E> converter
      The converter from the storage to the derived type.
  • Constructor Details

    • DerivedSet

      private DerivedSet(Set<S> storage, ObjectConverter<S,E> converter)
      Creates a new derived set from the specified storage set.
      Parameters:
      storage - the set which actually store the elements.
      converter - the converter from the storage to the derived type.
  • Method Details

    • create

      static <S, E> Set<E> create(Set<S> storage, ObjectConverter<S,E> converter)
      Creates a new derived set from the specified storage set.
      Parameters:
      storage - the set which actually store the elements.
      converter - the converter from the type in the storage set to the type in the derived set.
    • getElementType

      public final Class<E> getElementType()
      Returns the derived element type.
      Specified by:
      getElementType in interface CheckedContainer<S>
      Returns:
      the element type.
    • iterator

      public final Iterator<E> iterator()
      Returns an iterator over the elements contained in this set. The iterator will invoke the ObjectConverter.apply(Object) method for each element.
      Specified by:
      iterator in interface Collection<S>
      Specified by:
      iterator in interface Iterable<S>
      Specified by:
      iterator in interface Set<S>
      Specified by:
      iterator in class AbstractCollection<E>
      Returns:
      an iterator over the elements contained in this set.
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      Specified by:
      isEmpty in interface Collection<S>
      Specified by:
      isEmpty in interface Set<S>
      Overrides:
      isEmpty in class SetOfUnknownSize<E>
      Returns:
      true if this set contains no elements.
    • add

      public boolean add(E element) throws UnsupportedOperationException
      Ensures that this set contains the specified element. This method first checks if the given element is non-null, then delegates to the storage set like below:
      Specified by:
      add in interface Collection<S>
      Specified by:
      add in interface Set<S>
      Overrides:
      add in class AbstractCollection<E>
      Parameters:
      element - element whose presence in this set is to be ensured.
      Returns:
      true if the set changed as a result of the call.
      Throws:
      UnsupportedOperationException - if the storage set doesn't supports the add operation.
    • add

      final boolean add(E original, S element)
      Implementation of the add(Object) method adding the given converted value to the storage set. The original value is used only for formatting an error message in case of failure.