Interface NonEmptySet<A>
-
- Type Parameters:
A- the element type
- All Superinterfaces:
software.kes.enhancediterables.EnhancedIterable<A>,software.kes.enhancediterables.FiniteIterable<A>,com.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>,java.lang.Iterable<A>,software.kes.enhancediterables.NonEmptyFiniteIterable<A>,software.kes.enhancediterables.NonEmptyIterable<A>,Set<A>
- All Known Subinterfaces:
ImmutableNonEmptySet<A>
public interface NonEmptySet<A> extends software.kes.enhancediterables.NonEmptyFiniteIterable<A>, Set<A>
ASetthat is guaranteed at compile-time to contain at least one element.In addition to guarantees of
Set, provides the following benefits:NonEmptyIterable.head()method that returns an element.- Implements
NonEmptyIterable.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <A> ImmutableNonEmptySet<A>copyFromOrThrow(int maxCount, A[] source)Attempts to create anImmutableNonEmptySetthat is copied from an array, but with a maximum number of elements.static <A> ImmutableNonEmptySet<A>copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptySetfrom anyIterable, but consuming a maximum number of elements.static <A> ImmutableNonEmptySet<A>copyFromOrThrow(A[] source)Attempts to create anImmutableNonEmptySetthat is copied from an array.static <A> ImmutableNonEmptySet<A>copyFromOrThrow(java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptySetfrom anyIterable.default NonEmptySet<A>distinct()Since aNonEmptySetalready contains only distinct values, this method always returns itself.default booleanisEmpty()Tests whether thisNonEmptySetis empty.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>maybeCopyFrom(int maxCount, A[] source)Attempts to create anImmutableNonEmptySetthat is copied from an array, but with a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptySetthat is copied from anyIterable, but consuming a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>maybeCopyFrom(A[] source)Attempts to create anImmutableNonEmptySetthat is copied from an array.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>maybeCopyFrom(java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptySetthat is copied from anyIterable.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptySet<A>>maybeWrap(java.util.Set<A> underlying)Attempts to create aNonEmptySetthat wraps ajava.util.Set.static <A> ImmutableNonEmptySet<A>of(A first, A... more)Creates aImmutableNonEmptySetwith the given elements.default ImmutableNonEmptySet<A>toImmutable()Converts thisNonEmptySetto anImmutableNonEmptySet.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>>toNonEmpty()Attempts to convert thisSetto aNonEmptySet.default NonEmptySet<A>toNonEmptyOrThrow()Attempts to convert thisSetto aNonEmptySet.static <A> NonEmptySet<A>wrapOrThrow(java.util.Set<A> underlying)Attempts to create aNonEmptySetthat wraps ajava.util.Set.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, take, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, cross, cross, drop, dropWhile, filter, foldLeft, foldRight, inits, partition, prepend, slide, span, tails, takeWhile, zipWith
-
Methods inherited from interface software.kes.enhancediterables.NonEmptyFiniteIterable
concat, concat, cross, cycle, fmap, init, intersperse, last, magnetizeBy, prependAll, reduceLeft, reduceRight, reverse, tail, toFinite, zipWith
-
-
-
-
Method Detail
-
distinct
default NonEmptySet<A> distinct()
Since aNonEmptySetalready contains only distinct values, this method always returns itself.
-
isEmpty
default boolean isEmpty()
Tests whether thisNonEmptySetis empty.Always returns false for
NonEmptySets.
-
toImmutable
default ImmutableNonEmptySet<A> toImmutable()
Converts thisNonEmptySetto anImmutableNonEmptySet.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
NonEmptySetis already anImmutableNonEmptySet, no copies are made and this method is a no-op.- Specified by:
toImmutablein interfaceSet<A>- Returns:
- an
ImmutableNonEmptySetof the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>> toNonEmpty()
Attempts to convert thisSetto aNonEmptySet.Since this will always be successful for
NonEmptySets, this method always returns itself wrapped in aMaybe.just(A).Does not make copies of any underlying data structures.
- Specified by:
toNonEmptyin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Specified by:
toNonEmptyin interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Specified by:
toNonEmptyin interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>- Specified by:
toNonEmptyin interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>- Specified by:
toNonEmptyin interfaceSet<A>- Returns:
- this
NonEmptySetwrapped in aMaybe.just(A)
-
toNonEmptyOrThrow
default NonEmptySet<A> toNonEmptyOrThrow()
Attempts to convert thisSetto aNonEmptySet.Since this will always be successful for
NonEmptySets, this method always returns itself.Does not make copies of any underlying data structures.
- Specified by:
toNonEmptyOrThrowin interfaceSet<A>- Returns:
- this
NonEmptySet
-
of
@SafeVarargs static <A> ImmutableNonEmptySet<A> of(A first, A... more)
Creates aImmutableNonEmptySetwith the given elements.- Type Parameters:
A- the element type- Parameters:
first- the first elementmore- the remaining elements- Returns:
- an
ImmutableNonEmptySet<A>
-
maybeWrap
static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptySet<A>> maybeWrap(java.util.Set<A> underlying)
Attempts to create aNonEmptySetthat wraps ajava.util.Set.Does not make any copies of the given
Set. The createdNonEmptySetwill hold a reference to the givenSet, but will not alter it in any way.Bearers of the
NonEmptySetwill be unable to gain access to the underlyingSet, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the underlying
Setcan still mutate it. Mutating the underlyingSetis not advised. Operations that change the size of the underlyingSetwill result in unpredictable behavior. UseVector.copyFrom(java.lang.Iterable<A>)if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying-java.util.Setto wrap; not null- Returns:
- a
NonEmptySet<A>wrapped in aMaybe.just(A)ifunderlyingis non-empty;Maybe.nothing()otherwise.
-
wrapOrThrow
static <A> NonEmptySet<A> wrapOrThrow(java.util.Set<A> underlying)
Attempts to create aNonEmptySetthat wraps ajava.util.Set. If it is not possible, throws anIllegalArgumentException.Does not make any copies of the given
Set. The createdNonEmptySetwill hold a reference to the givenjava.util.Set, but will not alter it in any way.Bearers of the created
NonEmptySetwill be unable to gain access to the underlyingSet, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the underlying
Setcan still mutate it. Mutating the underlyingSetis not advised. Operations that change the size of the underlyingSetwill result in unpredictable behavior. UsecopyFromOrThrow(Iterable)if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying-java.util.Setto wrap; not null- Returns:
- a
NonEmptySet<A>ifunderlyingis non-empty; throws anIllegalArgumentExceptionotherwise
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySetthat is copied from anyIterable.The entire
Iterablewill be eagerly iterated. Be careful not to pass in an infiniteIterableor this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
sourcealready is anImmutableNonEmptySet, it will be returned directly.- Type Parameters:
A- the element type- Parameters:
source- anIterable<A>that may be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptySet<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(A[] source)
Attempts to create anImmutableNonEmptySetthat is copied from an array.- Type Parameters:
A- the element type- Parameters:
source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptySet<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySetthat is copied from anyIterable, but consuming a maximum number of elements.The
Iterablewill be eagerly iterated, but only up to a maximum ofmaxCountelements. IfmaxCountelements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
sourceis anImmutableNonEmptySetand its size is equal tomaxCount, in which case it will be returned directly.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to consume from the source. Must be >= 0. If 0, this method will always returnMaybe.nothing().source- anIterable<A>that will be iterated eagerly for up tomaxCountelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableNonEmptySet<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptySetthat is copied from an array, but with a maximum number of elements.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to copy from the array. Must be >= 0. If 0, this method will always returnMaybe.nothing().source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptySet<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySetfrom anyIterable. If theIterableis empty, throws anIllegalArgumentException.The entire
Iterablewill be eagerly iterated. Be careful not to pass in an infiniteIterableor this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
sourcealready is anImmutableNonEmptySet, it will be returned directly.- Type Parameters:
A- the element type- Parameters:
source- anIterable<A>that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptySet<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(A[] source)
Attempts to create anImmutableNonEmptySetthat is copied from an array. If the array is empty, throws anIllegalArgumentException.- Type Parameters:
A- the element type- Parameters:
source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptySet<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySetfrom anyIterable, but consuming a maximum number of elements. If theIterableis empty, throws anIllegalArgumentException.The
Iterablewill be eagerly iterated, but only up to a maximum ofmaxCountelements. IfmaxCountelements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
sourceis anImmutableNonEmptySetand its size is equal tomaxCount, in which case it will be returned directly.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to consume from the source. Must be >= 1.source- anIterable<A>that will be iterated eagerly for up tomaxCountelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableNonEmptySet<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(int maxCount, A[] source)
Attempts to create anImmutableNonEmptySetthat is copied from an array, but with a maximum number of elements. If the array is empty, throws anIllegalArgumentException.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to copy from the array. Must be >= 1.source- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptySet<A>
-
-