Class CollectionUtil.FluentMap
java.lang.Object
com.ssgllc.fish.service.util.registered.CollectionUtil.FluentMap
- Enclosing class:
CollectionUtil
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe value for a key, ornullif absent.getMap()The underlying map - the last call in a chain, once everything has been put.The value for a key, inserting an initial value first if the key is absent.The object this map was created against, when it was built with the parent-taking constructor.Puts a key and value and returns this map, so the call can be chained.putAllFrom(Collection<Map<String, Object>> items) Merges every entry of every map in a collection into this one, and returns this map.putIfNotNull(String key, Object value) Puts a key and value unless the value is null, and returns this map.voidReplaces the underlying map.voidSets the parent reference.
-
Constructor Details
-
FluentMap
-
FluentMap
-
-
Method Details
-
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
Replaces the underlying map. Returns nothing, so it cannot appear mid-chain; the factories onCollectionUtilare the normal way in.- Parameters:
map- the map to wrap
-
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
nullif none was set
-
setParent
Sets the parent reference. Returns nothing, so it cannot appear mid-chain.- Parameters:
parent- the owning object
-
get
The value for a key, ornullif absent.- Parameters:
key- the key to read- Returns:
- the value, or
null
-
getOrPut
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 readinitialValue- the value to insert if the key is absent- Returns:
- the existing value, or
initialValueonce inserted
-
put
Puts a key and value and returns this map, so the call can be chained. Puts nulls; useputIfNotNull(String, Object)to skip them.- Parameters:
key- the key to setvalue- the value to set- Returns:
- this map, for chaining
SpEL example:
#createEmptyFluentMap().put('id', #id).put('status', #statusId).getMap()
-
putIfNotNull
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 setvalue- the value to set if present- Returns:
- this map, for chaining
SpEL example:
#createEmptyFluentMap().putIfNotNull('middleName', #middleName).getMap()
-
putAllFrom
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
-