Class FluentSet<E>
- Type Parameters:
E- The element type.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
This class wraps an underlying set and provides a fluent API for adding elements. All methods return
Set implementation.
Features:
- Fluent API: All methods return
this for method chaining - Arbitrary Set Support: Works with any set implementation
- Conditional Adding: Add elements conditionally based on boolean expressions
- Transparent Interface: Implements the full
Setinterface, so it can be used anywhere a set is expected - Automatic Deduplication: Duplicate elements are automatically handled by the underlying set
Usage:
Example - Conditional Building:
Behavior Notes:
- All set operations are delegated to the underlying set
- 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 set is stored by reference (not copied), so modifications affect the original set
- Duplicate elements are automatically handled by the underlying set (only one occurrence is stored)
Thread Safety:
This class is not thread-safe unless the underlying set is thread-safe. If thread safety is required,
use a thread-safe set type (e.g., CopyOnWriteArraySet).
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a single element to this set.aa(Collection<? extends E> c) Adds all elements from the specified collection to this set.booleanbooleanaddAll(Collection<? extends E> c) Adds an element to this set if the specified boolean condition istrue .voidclear()booleanbooleancontainsAll(Collection<?> c) booleaninthashCode()booleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) toString()Methods 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.Set
spliterator
-
Constructor Details
-
FluentSet
Constructor.- Parameters:
inner- The underlying set to wrap. Must not benull .
-
-
Method Details
-
a
Adds a single element to this set.This is a convenience method that calls
add(Object)and returnsthis for method chaining. If the element already exists in the set, it is not added again (sets do not allow duplicates).Example:
FluentSet<String>
set =new FluentSet<>(new LinkedHashSet<>());set .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 set.This is a convenience method that calls
addAll(Collection)and returnsthis for method chaining. If the specified collection isnull , this is a no-op. Duplicate elements in the collection are automatically handled (only one occurrence is stored).Example:
FluentSet<String>
set =new FluentSet<>(new LinkedHashSet<>()); List<String>other = List.of("item1" ,"item2" );set .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 set 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 set.Example:
boolean includeDebug =true ;boolean includeTest =false ; FluentSet<String>set =new FluentSet<>(new LinkedHashSet<>()) .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.
-
iterator
-
size
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<E>
-
clear
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>- Overrides:
containsAllin classAbstractCollection<E>
-
isEmpty
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toString
- Overrides:
toStringin classAbstractCollection<E>
-
equals
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classAbstractSet<E>
-
hashCode
- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceSet<E>- Overrides:
hashCodein classAbstractSet<E>
-