saker.util Documentation TaskDoc JavaDoc Packages
package saker.util
public abstract class TransformingSet<SE, Eextends AbstractSet<E>
Pseudo-Set implementation that dynamically generated the elements for it based on the elements of a subject set.

The set is immutable, and designed to provide a set implementation that is only used to iterate over its elements. Each element in this set is dynamically generated from the undelying set.

Any method that is not the iterator(), forEach(Consumer<super E>), size() functions, may throw an UnsupportedOperationException any time.

Important: Implementations should ensure that the transformed elements still stay unique in the set, as they was in the subject set. Violating this may result in undefined behaviour in some implementations.

An use-case for this kind of set is to create a new Set or Collection with the given elements without pre-allocating the transformed elements beforehand.

Example:
A new set that is created from a set of integers, which have the original integer elements squared.

 Set<Integer> ints = ...;
 Set<Integer> squares = new TreeSet<>(new TransformingSet<Integer, Integer>(ints) {
 	@Override
 	protected Integer transform(Integer e) {
 		return e * e;
 	}
 });
 
Constructing a collection in this way instead of calling Collection.add(E) for every object can be more efficient, as new collection constructors can allocate and construct their instances more efficiently.
SEThe source set element type.
EThe element type of this set.
Fields
protected final Set<extends SE>
The backing set of the transforming set.
Constructors
public
TransformingSet(Set<extends SE> set)
Creates a new instance with the given set.
Methods
public void
forEach(Consumer<super E> action)
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
public boolean
Returns true if this set contains no elements.
public Iterator<E>
Returns an iterator over the elements in this set.
public int
Returns the number of elements in this set (its cardinality).
protected abstract E
transform(SE e)
Transforms the source set element to the actual element.
protected final Set<extends SE> set
The backing set of the transforming set.
public TransformingSet(Set<extends SE> set) throws NullPointerException
Creates a new instance with the given set.
setThe subject set.
NullPointerExceptionIf the set is null.
public void forEach(Consumer<super E> action)
Overridden from: Iterable
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
actionThe action to be performed for each element
1.8
public boolean isEmpty()
Overridden from: Set
Returns true if this set contains no elements.
true if this set contains no elements
public Iterator<E> iterator()
Overridden from: Set
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).
an iterator over the elements in this set
public int size()
Overridden from: Set
Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
the number of elements in this set (its cardinality)
protected abstract E transform(SE e)
Transforms the source set element to the actual element.
eThe element to transform.
The transformed element.