Package bm.util

Class Lister<E>

java.lang.Object
bm.util.Lister<E>
Type Parameters:
E - is the type of elements in the list
Direct Known Subclasses:
StringLister

public class Lister<E> extends Object
Fluent api to build up a List of some type from various ingredients. The list is constructed in place, using an ArrayList internally.
  • Constructor Details

    • Lister

      public Lister()
      Creates an empty one.
    • Lister

      @SafeVarargs public Lister(E first, E... elements)
      Creates the object from initial elements.
      Parameters:
      first - a required element also used to allow to start with a single element while at the same time be able to differentiate this call from the one starting with a collection
      elements - to initialise with
    • Lister

      public Lister(Collection<E> elements)
      Creates the object from initial elements.
      Parameters:
      elements - to initialise with
  • Method Details

    • of

      public static <T> Lister<T> of(Collection<T> elements)
      Creates an initial object.
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      elements - to start the list with
      Returns:
      this
    • add

      @SafeVarargs public final Lister<E> add(E... elements)
      Adds all given elements.
      Parameters:
      elements - to add
      Returns:
      this
    • addIf

      @SafeVarargs public final Lister<E> addIf(Predicate<E> test, E... elements)
      Adds all given elements if the predicate applied on the elements is true.
      Parameters:
      test - to run on each element, to add it only if the test returns true
      elements - to add
      Returns:
      this
    • add

      public Lister<E> add(Collection<E> args)
      Adds all given elements.
      Parameters:
      args - elements to add
      Returns:
      this
    • add

      public <T> Lister<E> add(Function<T,E> mapper, Collection<T> args)
      Adds elements of a collection after mapping them.
      Type Parameters:
      T - the type of elements in the collection
      Parameters:
      mapper - to map elements of the collection to elements compatible with this object's elements
      args - the collection to map and add
      Returns:
      this
    • addJoined

      public <T, S> Lister<E> addJoined(Function<T,S> mapper, Function<List<S>,E> joiner, Collection<T> args)
      Map all elements of args through the mapper. The resulting list is then passed through the joiner to create the element to add to this Lister.
    • get

      public List<E> get()
      Provides the list.
      Returns:
      the internal list, leaking the representation. If the returned result is changed, this will also chang the contents of this Lister.