Package org.linkki.util
Class Sequence<T>
- java.lang.Object
-
- org.linkki.util.Sequence<T>
-
- All Implemented Interfaces:
Serializable,Iterable<T>
public class Sequence<T> extends Object implements Iterable<T>, Serializable
This sequence is a wrapper for a list to create immutable lists easily. It could be instantiated using the static of-methods or theempty()method.If you already have such a
Sequenceobject you could create a newSequenceconcatenated with additional objects using the with-methods.To allow access to the elements of this sequence it implements the
Iterableinterface and provides methods likelist()orstream()to access the internal list.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Collector<T,?,Sequence<T>>collect()static <T> Sequence<T>empty()Creates an emptySequence.Iterator<T>iterator()List<T>list()Returns the values/objects in the sequence as unmodifiable list in the order they are added.static <T> Sequence<T>of(Collection<? extends T> elements)Creates a newSequencewith the elements of the givenCollection.static <T> Sequence<T>of(T... elements)Creates a newSequencewith the given elements.Stream<T>stream()Directly access the stream of the list that contains all the elements of this sequence.StringtoString()Sequence<T>with(Collection<T> elements)Returns a newSequenceconcatenated with the given elements.Sequence<T>with(Sequence<T> sequence)Returns a newSequenceconcatenated with the given sequence of elements.Sequence<T>with(T... newElements)Returns a newSequenceconcatenated with the given elements.Sequence<T>withIf(boolean condition, Supplier<T> supplier)Sequence<T>withIf(boolean condition, Supplier<T>... suppliers)Sequence<T>withNewElementsFrom(Collection<T> elements)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
of
public static <T> Sequence<T> of(Collection<? extends T> elements)
Creates a newSequencewith the elements of the givenCollection.- Parameters:
elements- the elements that should be part of this sequence- Returns:
- the new
Sequencewith the given elements
-
of
@SafeVarargs public static <T> Sequence<T> of(T... elements)
Creates a newSequencewith the given elements.- Parameters:
elements- the elements that should be part of this sequence- Returns:
- the new
Sequencewith the given elements
-
with
@CheckReturnValue public Sequence<T> with(Collection<T> elements)
- Parameters:
elements- the new elements that should be concatenated- Returns:
- a new sequence with all elements of this
Sequenceconcatenated with the new elements
-
withNewElementsFrom
public Sequence<T> withNewElementsFrom(Collection<T> elements)
Returns a newSequenceconcatenated with those of the given elements that are not already contained in thisSequence. ThisSequenceis not affected.- Parameters:
elements- the new elements that should be concatenated if they are not already contained- Returns:
- a new sequence with all elements of this
Sequenceconcatenated with the new elements
-
with
@CheckReturnValue public final Sequence<T> with(Sequence<T> sequence)
Returns a newSequenceconcatenated with the given sequence of elements.
-
with
@CheckReturnValue @SafeVarargs public final Sequence<T> with(T... newElements)
- Parameters:
newElements- the new elements that should be concatenated- Returns:
- a new sequence with all elements of this
Sequenceconcatenated with the new elements
-
withIf
@SafeVarargs @CheckReturnValue public final Sequence<T> withIf(boolean condition, Supplier<T>... suppliers)
-
list
public List<T> list()
Returns the values/objects in the sequence as unmodifiable list in the order they are added.- Returns:
- an unmodifiable list containing all elements of this sequence.
-
stream
public Stream<T> stream()
Directly access the stream of the list that contains all the elements of this sequence.
-
-