Class NonEmptySet<T>
- Type Parameters:
T- the type of elements in this set
- All Implemented Interfaces:
Iterable<T>
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.
The rationale for introducing dedicated non-empty collection types instead of using standard JDK types with runtime checks is documented in ADR-018 — NonEmptyList<T>, NonEmptySet<T>, NonEmptyMap<K,V> as structural guarantee types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collector<T, ?, Option<NonEmptySet<T>>> booleanReturnstrueif this set containselement.booleanReturns a newNonEmptySetcontaining elements that satisfypredicate, wrapped inOption.some(Object).static <T> Option<NonEmptySet<T>> fromOptional(Optional<? extends T> optional) Attempts to construct a singletonNonEmptySetfrom a JDKOptional.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 <E,T> Either <E, NonEmptySet<T>> sequenceEither(NonEmptySet<Either<E, T>> nes) Sequences aNonEmptySet<Either<E, T>>into anEither<E, NonEmptySet<T>>.static <T> Option<NonEmptySet<T>> sequenceOption(NonEmptySet<Option<T>> nes) Sequences aNonEmptySet<Option<T>>into anOption<NonEmptySet<T>>.static <T,E> Result <NonEmptySet<T>, E> sequenceResult(NonEmptySet<Result<T, E>> nes) Sequences aNonEmptySet<Result<T, E>>into aResult<NonEmptySet<T>, E>.static <T> Try<NonEmptySet<T>> sequenceTry(NonEmptySet<Try<T>> nes) Sequences aNonEmptySet<Try<T>>into aTry<NonEmptySet<T>>.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).toStream()Returns a sequentialStreamof all elements 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
-
fromOptional
Attempts to construct a singletonNonEmptySetfrom a JDKOptional.- Type Parameters:
T- the element type- Parameters:
optional- the source optional; must not benull- Returns:
Option.some(Object)wrapping a singletonNonEmptySetif the optional is present, orOption.none()if the optional is empty- Throws:
NullPointerException- ifoptionalisnull
-
collector
Returns aCollectorthat accumulates aStream<T>into anOption<NonEmptySet<T>>.Produces
Option.some(Object)for a non-empty stream andOption.none()for an empty stream. Duplicate elements are automatically deduplicated (set semantics).Example:
Option<NonEmptySet<String>> roles = userRoles.stream() .collect(NonEmptySet.collector());- Type Parameters:
T- the element type- Returns:
- a collector producing
Option<NonEmptySet<T>>
-
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
-
toStream
-
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
-
sequenceOption
Sequences aNonEmptySet<Option<T>>into anOption<NonEmptySet<T>>.Returns
Option.some(Object)containing all unwrapped values if every element isSome; returnsOption.none()as soon as any element isNone(fail-fast in inspection — the method stops iterating after the firstNone; elements are already materialized in the set before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
T- the unwrapped element type- Parameters:
nes- aNonEmptySet<Option<T>>; must not benull- Returns:
Some(NonEmptySet<T>)if all elements are present,Noneotherwise- Throws:
NullPointerException- ifnesisnull
-
sequenceTry
Sequences aNonEmptySet<Try<T>>into aTry<NonEmptySet<T>>.Returns
Try.success(Object)if all elements succeed; returnsTry.failure(Throwable)from the first failing element (fail-fast in inspection — the method stops iterating after the first failure; elements are already materialized in the set before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
T- the success value type- Parameters:
nes- aNonEmptySet<Try<T>>; must not benull- Returns:
Success(NonEmptySet<T>)if all succeed,Failureotherwise- Throws:
NullPointerException- ifnesisnull
-
sequenceEither
Sequences aNonEmptySet<Either<E, T>>into anEither<E, NonEmptySet<T>>.Returns
Either.right(Object)if all elements are right; returnsEither.left(Object)from the first left element (fail-fast in inspection — the method stops iterating after the first left; elements are already materialized in the set before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
E- the left (error) typeT- the right (success) type- Parameters:
nes- aNonEmptySet<Either<E, T>>; must not benull- Returns:
right(NonEmptySet<T>)if all are right,left(E)otherwise- Throws:
NullPointerException- ifnesisnull
-
sequenceResult
Sequences aNonEmptySet<Result<T, E>>into aResult<NonEmptySet<T>, E>.Returns
Result.ok(Object)if all elements are ok; returnsResult.err(Object)from the first error element (fail-fast in inspection — the method stops iterating after the first err; elements are already materialized in the set before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
T- the ok value typeE- the error type- Parameters:
nes- aNonEmptySet<Result<T, E>>; must not benull- Returns:
ok(NonEmptySet<T>)if all succeed,err(E)otherwise- Throws:
NullPointerException- ifnesisnull
-
iterator
-
equals
-
hashCode
-
toString
-