Record Class Sequence<T>

java.lang.Object
java.lang.Record
org.linkki.util.Sequence<T>
All Implemented Interfaces:
Serializable, Iterable<T>

public record Sequence<T>(List<T> list) extends Record 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:
  • Constructor Details

    • Sequence

      public Sequence(List<T> list)
      Creates an instance of a Sequence record class.
      Parameters:
      list - the value for the list record component
  • Method Details

    • 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
    • empty

      public static <T> Sequence<T> empty()
      Creates an empty Sequence.
      Returns:
      an empty Sequence
    • with

      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

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

      @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 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

      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
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • 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()
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • collect

      public static <T> Collector<T,?,Sequence<T>> collect()
      Creates a Collector that collects a Stream's elements into a Sequence.
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • list

      public List<T> list()
      Returns the value of the list record component.
      Returns:
      the value of the list record component