Class CollectionUtil.FluentSet

java.lang.Object
com.ssgllc.fish.service.util.registered.CollectionUtil.FluentSet
Enclosing class:
CollectionUtil

public static class CollectionUtil.FluentSet extends Object
A set that returns itself from every mutation, so it can be built inside a single expression.

A SpEL expression has no statements, so there is nowhere to put a loop or a temporary variable - each call has to hand back something the next call can be written onto. Start one with CollectionUtil.createEmptyFluentSet() and finish with getSet().

Duplicates collapse, as in any set. Use CollectionUtil.FluentList to keep order and duplicates.

  • Constructor Details

    • FluentSet

      public FluentSet(Set<Object> set)
  • Method Details

    • getSet

      public Set<Object> getSet()
      The underlying set - the last call in a chain, once everything has been added.
      Returns:
      the set built so far

      Groovy example:
      return collectionUtil.createEmptyFluentSet().add('A').add('B').add('A').getSet()

      Returns:
      ["A", "B"]
    • setSet

      public void setSet(Set<Object> set)
      Replaces the underlying set. Returns nothing, so it cannot appear mid-chain; the factories on CollectionUtil are the normal way in.
      Parameters:
      set - the set to wrap
    • add

      public CollectionUtil.FluentSet add(Object value)
      Adds a value and returns this set, so the call can be chained. Adds nulls; use addIfNotNull(Object) to skip them.
      Parameters:
      value - the value to add
      Returns:
      this set, for chaining

      SpEL example:
      #createEmptyFluentSet().add(#statusId).add(#priorStatusId).getSet()
    • addIfNotNull

      public CollectionUtil.FluentSet addIfNotNull(Object value)
      Adds a value unless it is null, and returns this set. The usual choice when the value comes from an optional field, since it keeps the chain unbroken without admitting a null.
      Parameters:
      value - the value to add if present
      Returns:
      this set, for chaining

      SpEL example:
      #createEmptyFluentSet().addIfNotNull(#primaryPhone).addIfNotNull(#mobilePhone).getSet()
    • addValuesIfNotNull

      public CollectionUtil.FluentSet addValuesIfNotNull(Collection<Object> values)
      Adds every non-null element of a collection, and returns this set. Tolerates a null collection - unlike CollectionUtil.FluentList.addAllIfNotNull(Collection), which only guards the elements.
      Parameters:
      values - the values to add; null is ignored
      Returns:
      this set, for chaining

      Groovy example:
      return collectionUtil.createEmptyFluentSet().addValuesIfNotNull(person.phones).getSet()