Class FluentList<E>
- Type Parameters:
E- The element type.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>
This class wraps an underlying list and provides a fluent API for adding elements. All methods return
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
Listinterface, so it can be used anywhere a list is expected
Usage:
Example - Conditional Building:
Behavior Notes:
- All list operations are delegated to the underlying list
- The fluent methods (
a(Object),aa(Collection),ai(boolean, Object)) returnthis for chaining - If a
null collection is passed toaa(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:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a single element to this list.aa(Collection<? extends E> c) Adds all elements from the specified collection to this list.voidbooleanbooleanaddAll(int index, Collection<? extends E> c) booleanaddAll(Collection<? extends E> c) Adds an element to this list if the specified boolean condition istrue .voidclear()booleanbooleancontainsAll(Collection<?> c) booleanget(int index) inthashCode()intbooleanisEmpty()iterator()intlistIterator(int index) remove(int index) booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()subList(int fromIndex, int toIndex) Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from class java.util.AbstractList
removeRangeMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Constructor Details
-
FluentList
Constructor.- Parameters:
inner- The underlying list to wrap. Must not benull .
-
-
Method Details
-
a
Adds a single element to this list.This is a convenience method that calls
add(Object)and returnsthis for method chaining.Example:
FluentList<String>
list =new FluentList<>(new ArrayList<>());list .a("item1" ).a("item2" );- Parameters:
element- The element to add.- Returns:
- This object for method chaining.
-
aa
Adds all elements from the specified collection to this list.This is a convenience method that calls
addAll(Collection)and returnsthis for method chaining. If the specified collection isnull , this is a no-op.Example:
FluentList<String>
list =new FluentList<>(new ArrayList<>()); List<String>other = List.of("item1" ,"item2" );list .aa(other ).a("item3" );- Parameters:
c- The collection whose elements are to be added. Can benull (no-op).- Returns:
- This object for method chaining.
-
ai
Adds an element to this list if the specified boolean condition istrue .This method is useful for conditionally adding elements based on runtime conditions. If the condition is
false , the element is not added and this method returnsthis without modifying the list.Example:
boolean includeDebug =true ;boolean includeTest =false ; FluentList<String>list =new FluentList<>(new ArrayList<>()) .a("basic" ) .ai(includeDebug ,"debug" )// Added .ai(includeTest ,"test" );// Not added - Parameters:
condition- The condition to evaluate. Iftrue , the element is added; iffalse , it is not.element- The element to add if the condition istrue .- Returns:
- This object for method chaining.
-
get
-
size
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>
-
add
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>
-
addAll
-
remove
-
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classAbstractCollection<E>
-
clear
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
set
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>
-
listIterator
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>
-
listIterator
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>
-
subList
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>- Overrides:
containsAllin classAbstractCollection<E>
-
isEmpty
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
iterator
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toString
- Overrides:
toStringin classAbstractCollection<E>
-
equals
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>
-
hashCode
- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>
-