Class FluentMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
org.apache.juneau.commons.collections.FluentMap<K,V>
Type Parameters:
K - The key type.
V - The value type.
All Implemented Interfaces:
Map<K,V>

public class FluentMap<K,V> extends AbstractMap<K,V>
A fluent wrapper around an arbitrary map that provides convenient methods for adding entries.

This class wraps an underlying map and provides a fluent API for adding entries. All methods return this to allow method chaining. The underlying map can be any 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 Map interface, so it can be used anywhere a map is expected
Usage:

// Create a FluentMap wrapping a LinkedHashMap FluentMap<String, String> map = new FluentMap<>(new LinkedHashMap<>()); map .a("key1", "value1") .a("key2", "value2") .ai(true, "key3", "value3") // Added .ai(false, "key4", "value4"); // Not added // Add all entries from another map Map<String, String> other = Map.of("key5", "value5", "key6", "value6"); map.aa(other);

Example - Conditional Building:

boolean includeDebug = true; boolean includeTest = false; FluentMap<String, String> config = new FluentMap<>(new LinkedHashMap<>()) .a("host", "localhost") .a("port", "8080") .ai(includeDebug, "debug", "true") // Added .ai(includeTest, "test", "true"); // Not added

Behavior Notes:
  • All map operations are delegated to the underlying map
  • The fluent methods (a(Object, Object), aa(Map), ai(boolean, Object, Object)) return this for chaining
  • If a null map is passed to aa(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:
  • Constructor Details

    • FluentMap

      public FluentMap(Map<K,V> inner)
      Constructor.
      Parameters:
      inner - The underlying map to wrap. Must not be null.
  • Method Details

    • a

      public FluentMap<K,V> a(K key, V value)
      Adds a single key-value pair to this map.

      This is a convenience method that calls put(Object, Object) and returns this 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

      public FluentMap<K,V> aa(Map<? extends K,? extends V> m)
      Adds all entries from the specified map to this map.

      This is a convenience method that calls putAll(Map) and returns this for method chaining. If the specified map is null, 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 be null (no-op).
      Returns:
      This object for method chaining.
    • ai

      public FluentMap<K,V> ai(boolean condition, K key, V value)
      Adds a key-value pair to this map if the specified boolean condition is true.

      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 returns this 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. If true, the entry is added; if false, it is not.
      key - The key to add if the condition is true.
      value - The value to add if the condition is true.
      Returns:
      This object for method chaining.
    • ai

      public FluentMap<K,V> ai(Predicate<V> predicate, K key, V value)
      Adds a key-value pair to this map if the specified predicate returns true 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 returns false, the entry is not added and this method returns this 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 returns true, the entry is added; if false, it is not.
      key - The key to add if the predicate returns true.
      value - The value to add if the predicate returns true.
      Returns:
      This object for method chaining.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • toString

      public String toString()
      Overrides:
      toString in class AbstractMap<K,V>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class AbstractMap<K,V>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class AbstractMap<K,V>