Record Class NonEmptyList<T>
java.lang.Object
java.lang.Record
dev.fforj.NonEmptyList<T>
- All Implemented Interfaces:
Iterable<T>
An immutable sequence with a compile-time guarantee that it contains at least one
element. Iterable head-first; convert to a plain
List with toList().
Use cases:
- Validation:
Validated.InvalidcarriesNonEmptyList<E>so callers can't forget to handle the empty-error case. - Domain constraints: a
Permission's required scopes, a tenant's owners, any "must have at least one" collection.
Backed by a defensively-copied List for tail() so callers cannot
mutate the internal state.
-
Constructor Summary
ConstructorsConstructorDescriptionNonEmptyList(T head, List<T> tail) Creates an instance of aNonEmptyListrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal NonEmptyList<T> Append one or more elements.concat(NonEmptyList<T> other) Concatenate two non-empty lists.final booleanIndicates whether some other object is "equal to" this one.static <T> Optional<NonEmptyList<T>> Try to lift aList.final inthashCode()Returns a hash code value for this object.head()Returns the value of theheadrecord component.iterator()Iterate all elements, head first.<U> NonEmptyList<U> Transform every element; the result is non-empty by construction.static <T> NonEmptyList<T> of(T head) One element.static <T> NonEmptyList<T> of(T head, T... rest) One element + N more.intsize()Total element count: head plus tail; always at least 1.stream()Stream all elements, head first.tail()Returns the value of thetailrecord component.toList()Convert to an immutable plainList, head first.final StringtoString()Returns a string representation of this record class.Methods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
NonEmptyList
-
-
Method Details
-
of
One element. -
of
One element + N more. -
fromList
Try to lift aList. Returns empty if the list itself is empty: absence, not failure; useResult.fromOptionalto attach a domain error if needed. -
size
public int size()Total element count: head plus tail; always at least 1. -
toList
-
stream
-
iterator
-
map
Transform every element; the result is non-empty by construction. -
append
Append one or more elements. -
concat
Concatenate two non-empty lists. -
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
head
Returns the value of theheadrecord component.- Returns:
- the value of the
headrecord component
-
tail
-