Class 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 the empty() method.

    If you already have such a Sequence object you could create a new Sequence concatenated with additional objects using the with-methods.

    To allow access to the elements of this sequence it implements the Iterable interface and provides methods like list() or stream() to access the internal list.

    See Also:
    Serialized Form
    • Method Detail

      • of

        public static <T> Sequence<T> of​(Collection<? extends T> elements)
        Creates a new Sequence with the elements of the given Collection.
        Parameters:
        elements - the elements that should be part of this sequence
        Returns:
        the new Sequence with the given elements
      • of

        @SafeVarargs
        public static <T> Sequence<T> of​(T... elements)
        Creates a new Sequence with the given elements.
        Parameters:
        elements - the elements that should be part of this sequence
        Returns:
        the new Sequence with the given elements
      • with

        @CheckReturnValue
        public Sequence<T> with​(Collection<T> elements)
        Returns a new Sequence concatenated with the given elements. This Sequence is not affected.
        Parameters:
        elements - the new elements that should be concatenated
        Returns:
        a new sequence with all elements of this Sequence concatenated with the new elements
      • withNewElementsFrom

        public Sequence<T> withNewElementsFrom​(Collection<T> elements)
        Returns a new Sequence concatenated with those of the given elements that are not already contained in this Sequence. This Sequence is 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 Sequence concatenated with the new elements
      • with

        @CheckReturnValue
        public final Sequence<T> with​(Sequence<T> sequence)
        Returns a new Sequence concatenated with the given sequence of elements.
      • with

        @CheckReturnValue
        @SafeVarargs
        public final Sequence<T> with​(T... newElements)
        Returns a new Sequence concatenated with the given elements. This Sequence is not affected.
        Parameters:
        newElements - the new elements that should be concatenated
        Returns:
        a new sequence with all elements of this Sequence concatenated with the new elements
      • withIf

        @SafeVarargs
        @CheckReturnValue
        public final Sequence<T> withIf​(boolean condition,
                                        Supplier<T>... suppliers)
        Returns a new Sequence concatenated with the elements produced by the given Suppliers if the condition is true. This Sequence is not affected.
        Parameters:
        suppliers - the suppliers for new elements that should be concatenated
        Returns:
        a new sequence with all elements of this Sequence concatenated with the new elements or this Sequence if the condition is false
      • withIf

        @CheckReturnValue
        public final Sequence<T> withIf​(boolean condition,
                                        Supplier<T> supplier)
        Returns a new Sequence concatenated with the element produced by the given Supplier if the condition is true. This Sequence is not affected.
        Parameters:
        supplier - the supplier for a new element that should be concatenated
        Returns:
        a new sequence with all elements of this Sequence concatenated with the new elements or this Sequence if the condition is false
      • 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.
        Returns:
        the Stream of list()