Class FluentMap<K,V>
- Type Parameters:
K- The key type.V- The value type.
- All Implemented Interfaces:
Map<K,V>
This class wraps an underlying map and provides a fluent API for adding entries. All methods return
Map implementation.
Features:
- Fluent API: All methods return
this for method chaining - Arbitrary Map Support: Works with any map implementation
- Conditional Adding: Add entries conditionally based on boolean expressions
- Transparent Interface: Implements the full
Mapinterface, so it can be used anywhere a map is expected
Usage:
Example - Conditional Building:
Behavior Notes:
- All map operations are delegated to the underlying map
- The fluent methods (
a(Object, Object),aa(Map),ai(boolean, Object, Object)) returnthis for chaining - If a
null map is passed toaa(Map), it is treated as a no-op - The underlying map is stored by reference (not copied), so modifications affect the original map
Thread Safety:
This class is not thread-safe unless the underlying map is thread-safe. If thread safety is required,
use a thread-safe map type (e.g., ConcurrentHashMap).
See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a single key-value pair to this map.Adds all entries from the specified map to this map.Adds a key-value pair to this map if the specified boolean condition istrue .Adds a key-value pair to this map if the specified predicate returnstrue when applied to the value.voidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleaninthashCode()booleanisEmpty()keySet()voidintsize()toString()values()Methods inherited from class java.util.AbstractMap
cloneMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
FluentMap
Constructor.- Parameters:
inner- The underlying map to wrap. Must not benull .
-
-
Method Details
-
a
Adds a single key-value pair to this map.This is a convenience method that calls
put(Object, Object)and returnsthis for method chaining.Example:
FluentMap<String, String>
map =new FluentMap<>(new LinkedHashMap<>());map .a("key1" ,"value1" ).a("key2" ,"value2" );- Parameters:
key- The key to add.value- The value to add.- Returns:
- This object for method chaining.
-
aa
Adds all entries from the specified map to this map.This is a convenience method that calls
putAll(Map)and returnsthis for method chaining. If the specified map isnull , this is a no-op.Example:
FluentMap<String, String>
map =new FluentMap<>(new LinkedHashMap<>()); Map<String, String>other = Map.of("key1" ,"value1" ,"key2" ,"value2" );map .aa(other ).a("key3" ,"value3" );- Parameters:
m- The map whose entries are to be added. Can benull (no-op).- Returns:
- This object for method chaining.
-
ai
Adds a key-value pair to this map if the specified boolean condition istrue .This method is useful for conditionally adding entries based on runtime conditions. If the condition is
false , the entry is not added and this method returnsthis without modifying the map.Example:
boolean includeDebug =true ;boolean includeTest =false ; FluentMap<String, String>map =new FluentMap<>(new LinkedHashMap<>()) .a("host" ,"localhost" ) .ai(includeDebug ,"debug" ,"true" )// Added .ai(includeTest ,"test" ,"true" );// Not added - Parameters:
condition- The condition to evaluate. Iftrue , the entry is added; iffalse , it is not.key- The key to add if the condition istrue .value- The value to add if the condition istrue .- Returns:
- This object for method chaining.
-
ai
Adds a key-value pair to this map if the specified predicate returnstrue when applied to the value.This method is useful for conditionally adding entries based on the value itself. The predicate is applied to the value, and if it returns
true , the entry is added. If the predicate returnsfalse , the entry is not added and this method returnsthis without modifying the map.Example:
FluentMap<String, String>
map =new FluentMap<>(new LinkedHashMap<>()) .a("host" ,"localhost" ) .ai(s -> !s.isEmpty(),"debug" ,"true" )// Added (value is not empty) .ai(s -> !s.isEmpty(),"test" ,"" );// Not added (value is empty) - Parameters:
predicate- The predicate to test on the value. If it returnstrue , the entry is added; iffalse , it is not.key- The key to add if the predicate returnstrue .value- The value to add if the predicate returnstrue .- Returns:
- This object for method chaining.
-
entrySet
-
get
-
put
-
remove
-
putAll
-
clear
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V>
-
size
-
isEmpty
-
keySet
-
values
-
toString
- Overrides:
toStringin classAbstractMap<K,V>
-
equals
-
hashCode
-