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>
ASet
that 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 anImmutableNonEmptySet
that 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 anImmutableNonEmptySet
from anyIterable
, but consuming a maximum number of elements.static <A> ImmutableNonEmptySet<A>
copyFromOrThrow(A[] source)
Attempts to create anImmutableNonEmptySet
that is copied from an array.static <A> ImmutableNonEmptySet<A>
copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySet
from anyIterable
.default NonEmptySet<A>
distinct()
Since aNonEmptySet
already contains only distinct values, this method always returns itself.default boolean
isEmpty()
Tests whether thisNonEmptySet
is empty.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>
maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptySet
that 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 anImmutableNonEmptySet
that 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 anImmutableNonEmptySet
that is copied from an array.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>>
maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySet
that is copied from anyIterable
.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptySet<A>>
maybeWrap(java.util.Set<A> underlying)
Attempts to create aNonEmptySet
that wraps ajava.util.Set
.static <A> ImmutableNonEmptySet<A>
of(A first, A... more)
Creates aImmutableNonEmptySet
with the given elements.default ImmutableNonEmptySet<A>
toImmutable()
Converts thisNonEmptySet
to anImmutableNonEmptySet
.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>>
toNonEmpty()
Attempts to convert thisSet
to aNonEmptySet
.default NonEmptySet<A>
toNonEmptyOrThrow()
Attempts to convert thisSet
to aNonEmptySet
.static <A> NonEmptySet<A>
wrapOrThrow(java.util.Set<A> underlying)
Attempts to create aNonEmptySet
that 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 aNonEmptySet
already contains only distinct values, this method always returns itself.
-
isEmpty
default boolean isEmpty()
Tests whether thisNonEmptySet
is empty.Always returns false for
NonEmptySet
s.
-
toImmutable
default ImmutableNonEmptySet<A> toImmutable()
Converts thisNonEmptySet
to anImmutableNonEmptySet
.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
NonEmptySet
is already anImmutableNonEmptySet
, no copies are made and this method is a no-op.- Specified by:
toImmutable
in interfaceSet<A>
- Returns:
- an
ImmutableNonEmptySet
of the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptySet<A>> toNonEmpty()
Attempts to convert thisSet
to aNonEmptySet
.Since this will always be successful for
NonEmptySet
s, this method always returns itself wrapped in aMaybe.just(A)
.Does not make copies of any underlying data structures.
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>
- Specified by:
toNonEmpty
in interfaceSet<A>
- Returns:
- this
NonEmptySet
wrapped in aMaybe.just(A)
-
toNonEmptyOrThrow
default NonEmptySet<A> toNonEmptyOrThrow()
Attempts to convert thisSet
to aNonEmptySet
.Since this will always be successful for
NonEmptySet
s, this method always returns itself.Does not make copies of any underlying data structures.
- Specified by:
toNonEmptyOrThrow
in interfaceSet<A>
- Returns:
- this
NonEmptySet
-
of
@SafeVarargs static <A> ImmutableNonEmptySet<A> of(A first, A... more)
Creates aImmutableNonEmptySet
with 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 aNonEmptySet
that wraps ajava.util.Set
.Does not make any copies of the given
Set
. The createdNonEmptySet
will hold a reference to the givenSet
, but will not alter it in any way.Bearers of the
NonEmptySet
will 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
Set
can still mutate it. Mutating the underlyingSet
is not advised. Operations that change the size of the underlyingSet
will 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.Set
to wrap; not null- Returns:
- a
NonEmptySet<A>
wrapped in aMaybe.just(A)
ifunderlying
is non-empty;Maybe.nothing()
otherwise.
-
wrapOrThrow
static <A> NonEmptySet<A> wrapOrThrow(java.util.Set<A> underlying)
Attempts to create aNonEmptySet
that wraps ajava.util.Set
. If it is not possible, throws anIllegalArgumentException
.Does not make any copies of the given
Set
. The createdNonEmptySet
will hold a reference to the givenjava.util.Set
, but will not alter it in any way.Bearers of the created
NonEmptySet
will 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
Set
can still mutate it. Mutating the underlyingSet
is not advised. Operations that change the size of the underlyingSet
will result in unpredictable behavior. UsecopyFromOrThrow(Iterable)
if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
-java.util.Set
to wrap; not null- Returns:
- a
NonEmptySet<A>
ifunderlying
is non-empty; throws anIllegalArgumentException
otherwise
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySet
that is copied from anyIterable
.The entire
Iterable
will be eagerly iterated. Be careful not to pass in an infiniteIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
already 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)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(A[] source)
Attempts to create anImmutableNonEmptySet
that 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)
ifsource
is 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 anImmutableNonEmptySet
that is copied from anyIterable
, but consuming a maximum number of elements.The
Iterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is anImmutableNonEmptySet
and 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 tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableNonEmptySet<A>
wrapped in aMaybe.just(A)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptySet<A>> maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptySet
that 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)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptySet
from anyIterable
. If theIterable
is empty, throws anIllegalArgumentException
.The entire
Iterable
will be eagerly iterated. Be careful not to pass in an infiniteIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
already 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 anImmutableNonEmptySet
that 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 anImmutableNonEmptySet
from anyIterable
, but consuming a maximum number of elements. If theIterable
is empty, throws anIllegalArgumentException
.The
Iterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is anImmutableNonEmptySet
and 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 tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableNonEmptySet<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptySet<A> copyFromOrThrow(int maxCount, A[] source)
Attempts to create anImmutableNonEmptySet
that 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>
-
-