Class CollectionUtil.FluentMap

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

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

The keyed counterpart to CollectionUtil.FluentList. Start one with CollectionUtil.createEmptyFluentMap() or CollectionUtil.createFluentMap(String, Object) and finish with getMap() - useful for assembling a payload for a text template or an outbound call.

  • Constructor Details

  • Method Details

    • getMap

      public Map<String,Object> getMap()
      The underlying map - the last call in a chain, once everything has been put.
      Returns:
      the map built so far

      Groovy example:
      return collectionUtil.createEmptyFluentMap().put('name', person.fullName).getMap()

      Returns:
      ["name": "Jane Doe"]
    • setMap

      public void setMap(Map<String,Object> map)
      Replaces the underlying map. Returns nothing, so it cannot appear mid-chain; the factories on CollectionUtil are the normal way in.
      Parameters:
      map - the map to wrap
    • getParent

      public Object getParent()
      The object this map was created against, when it was built with the parent-taking constructor. Lets a nested structure keep a reference back to its owner while it is being assembled.
      Returns:
      the parent, or null if none was set
    • setParent

      public void setParent(Object parent)
      Sets the parent reference. Returns nothing, so it cannot appear mid-chain.
      Parameters:
      parent - the owning object
    • get

      public Object get(String key)
      The value for a key, or null if absent.
      Parameters:
      key - the key to read
      Returns:
      the value, or null
    • getOrPut

      public Object getOrPut(String key, Object initialValue)
      The value for a key, inserting an initial value first if the key is absent. Returns the value, not the map, so it does not continue a chain - it is for reading a slot that may need seeding, such as a running total.
      Parameters:
      key - the key to read
      initialValue - the value to insert if the key is absent
      Returns:
      the existing value, or initialValue once inserted
    • put

      public CollectionUtil.FluentMap put(String key, Object value)
      Puts a key and value and returns this map, so the call can be chained. Puts nulls; use putIfNotNull(String, Object) to skip them.
      Parameters:
      key - the key to set
      value - the value to set
      Returns:
      this map, for chaining

      SpEL example:
      #createEmptyFluentMap().put('id', #id).put('status', #statusId).getMap()
    • putIfNotNull

      public CollectionUtil.FluentMap putIfNotNull(String key, Object value)
      Puts a key and value unless the value is null, and returns this map. Keeps absent fields out of the result entirely rather than mapping them to null, which matters for a payload where a null and a missing key mean different things.
      Parameters:
      key - the key to set
      value - the value to set if present
      Returns:
      this map, for chaining

      SpEL example:
      #createEmptyFluentMap().putIfNotNull('middleName', #middleName).getMap()
    • putAllFrom

      public CollectionUtil.FluentMap putAllFrom(Collection<Map<String,Object>> items)
      Merges every entry of every map in a collection into this one, and returns this map. Later maps overwrite earlier ones on a repeated key. Throws on a null collection.
      Parameters:
      items - the maps to merge in
      Returns:
      this map, for chaining

      Groovy example:
      return collectionUtil.createEmptyFluentMap().putAllFrom([defaults, overrides]).getMap()

      Returns:
      the defaults with the overrides applied on top