Class NonEmptySet<T>
java.lang.Object
dmx.fun.NonEmptySet<T>
- Type Parameters:
T- the type of elements in this set
- All Implemented Interfaces:
Iterable<T>
An immutable, non-empty set: a set guaranteed at construction time to contain
at least one element.
This type makes the non-emptiness constraint part of the static type system.
APIs that require at least one item can declare NonEmptySet<T> instead of
Set<T> and eliminate runtime emptiness checks entirely.
Insertion order is preserved (backed by LinkedHashSet). The first element
inserted is the head().
This class is @NullMarked: all elements and parameters are non-null by default.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif this set containselement.booleanReturns a newNonEmptySetcontaining elements that satisfypredicate, wrapped inOption.some(Object).static <T> Option<NonEmptySet<T>> Attempts to construct aNonEmptySetfrom a plainSet.inthashCode()head()Returns the guaranteed head element of this set (the first inserted element).intersection(Set<? extends T> other) Returns a newNonEmptySetcontaining only elements present in both this set andother, wrapped inOption.some(Object).iterator()Returns an iterator over all elements (head first, then tail in insertion order).<R> NonEmptySet<R> Appliesmapperto every element and returns a newNonEmptySetof the results.static <T> NonEmptySet<T> Creates aNonEmptySetwith the given head element and additional elements.static <T> NonEmptySet<T> singleton(T head) Creates aNonEmptySetcontaining exactly one element.intsize()Returns the number of elements in this set.Converts this set to aNonEmptyListof its elements in insertion order.<V> NonEmptyMap<T, V> toNonEmptyMap(Function<? super T, ? extends V> valueMapper) Returns aNonEmptyMapby applyingvalueMapperto each element of this set.toSet()Returns an unmodifiableSetcontaining all elements (head first, then tail in insertion order).toString()union(NonEmptySet<T> other) Returns a newNonEmptySetthat is the union of this set andother.Methods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
of
Creates aNonEmptySetwith the given head element and additional elements.If
restcontainshead, the duplicate is silently ignored.- Type Parameters:
T- the element type- Parameters:
head- the first (mandatory) element; must not benullrest- additional elements; must not benull; elements must not benull- Returns:
- a new
NonEmptySet - Throws:
NullPointerException- ifhead,rest, or any element inrestisnull
-
singleton
Creates aNonEmptySetcontaining exactly one element.- Type Parameters:
T- the element type- Parameters:
head- the sole element; must not benull- Returns:
- a singleton
NonEmptySet - Throws:
NullPointerException- ifheadisnull
-
fromSet
Attempts to construct aNonEmptySetfrom a plainSet.- Type Parameters:
T- the element type- Parameters:
set- the source set; must not benull; elements must not benull- Returns:
Option.some(Object)wrapping theNonEmptySetif the set is non-empty, orOption.none()if the set is empty- Throws:
NullPointerException- ifsetor any element isnull
-
head
Returns the guaranteed head element of this set (the first inserted element).- Returns:
- the head element (never
null)
-
size
public int size()Returns the number of elements in this set. Always ≥ 1.- Returns:
- the size
-
contains
Returnstrueif this set containselement.- Parameters:
element- the element to test; must not benull- Returns:
trueif the element is present- Throws:
NullPointerException- ifelementisnull
-
toSet
-
map
Appliesmapperto every element and returns a newNonEmptySetof the results. Duplicate mapped values are deduplicated; the head is always the mapped value of the original head.- Type Parameters:
R- the result element type- Parameters:
mapper- a non-null function to apply to each element; must not returnnull- Returns:
- a new
NonEmptySetof mapped values - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
filter
Returns a newNonEmptySetcontaining elements that satisfypredicate, wrapped inOption.some(Object). ReturnsOption.none()if no elements pass the predicate.- Parameters:
predicate- a non-null predicate to test each element- Returns:
Some(filteredSet)if at least one element passes,Noneotherwise- Throws:
NullPointerException- ifpredicateisnull
-
union
Returns a newNonEmptySetthat is the union of this set andother. The result is always non-empty since both inputs are non-empty.- Parameters:
other- the other set; must not benull- Returns:
- a new
NonEmptySetcontaining all elements from both sets - Throws:
NullPointerException- ifotherisnull
-
intersection
Returns a newNonEmptySetcontaining only elements present in both this set andother, wrapped inOption.some(Object). ReturnsOption.none()if the intersection is empty.- Parameters:
other- the set to intersect with; must not benull- Returns:
Some(intersection)if at least one common element exists,Noneotherwise- Throws:
NullPointerException- ifotherisnull
-
toNonEmptyList
Converts this set to aNonEmptyListof its elements in insertion order.- Returns:
- a
NonEmptyList<T>with the same elements
-
toNonEmptyMap
Returns aNonEmptyMapby applyingvalueMapperto each element of this set. Elements become keys; mapped results become values. The head of this set is the head key of the returned map.- Type Parameters:
V- the value type- Parameters:
valueMapper- a non-null function to derive a value from each element; must not returnnull- Returns:
- a new
NonEmptyMap<T, V> - Throws:
NullPointerException- ifvalueMapperisnullor returnsnull
-
iterator
-
equals
-
hashCode
-
toString
-