Class FluentList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.apache.juneau.commons.collections.FluentList<E>
Type Parameters:
E - The element type.
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class FluentList<E> extends AbstractList<E>
A fluent wrapper around an arbitrary list that provides convenient methods for adding elements.

This class wraps an underlying list and provides a fluent API for adding elements. All methods return this to allow method chaining. The underlying list can be any List implementation.

Features:
  • Fluent API: All methods return this for method chaining
  • Arbitrary List Support: Works with any list implementation
  • Conditional Adding: Add elements conditionally based on boolean expressions
  • Transparent Interface: Implements the full List interface, so it can be used anywhere a list is expected
Usage:

// Create a FluentList wrapping an ArrayList FluentList<String> list = new FluentList<>(new ArrayList<>()); list .a("item1") .a("item2") .ai(true, "item3") // Added .ai(false, "item4"); // Not added // Add all elements from another collection List<String> other = List.of("item5", "item6"); list.aa(other);

Example - Conditional Building:

boolean includeDebug = true; boolean includeTest = false; FluentList<String> config = new FluentList<>(new ArrayList<>()) .a("setting1") .a("setting2") .ai(includeDebug, "debug") // Added .ai(includeTest, "test"); // Not added

Behavior Notes:
  • All list operations are delegated to the underlying list
  • The fluent methods (a(Object), aa(Collection), ai(boolean, Object)) return this for chaining
  • If a null collection is passed to aa(Collection), it is treated as a no-op
  • The underlying list is stored by reference (not copied), so modifications affect the original list
Thread Safety:

This class is not thread-safe unless the underlying list is thread-safe. If thread safety is required, use a thread-safe list type (e.g., CopyOnWriteArrayList).

See Also: