Interface Set<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>
- All Known Subinterfaces:
ImmutableNonEmptySet<A>
,ImmutableSet<A>
,NonEmptySet<A>
public interface Set<A> extends software.kes.enhancediterables.FiniteIterable<A>
A finite, unordered view of a collection that contains no duplicate elements.A
Set
guarantees the following:- A
size()
method that executes in O(1). - A
contains(A)
method that tests an value for membership in O(1). - An
isEmpty()
method that executes in O(1). - Iteration will always terminate.
- Protected from mutation by the bearer.
- The bearer cannot gain access to a reference to the underlying collection.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <A> SetBuilder<A>
builder()
Creates a newSetBuilder
.static <A> SetBuilder<A>
builder(int initialCapacity)
Creates a newSetBuilder
with an initial capacity hint.boolean
contains(A element)
Tests if an element is a member of thisSet
.static <A> ImmutableSet<A>
copyFrom(int maxCount, A[] source)
Returns a newImmutableSet
that is copied from an array.static <A> ImmutableSet<A>
copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableSet
that is copied from anyIterable
, but consuming a maximum number of elements.static <A> ImmutableSet<A>
copyFrom(A[] source)
Creates anImmutableSet
that is copied from an array.static <A> ImmutableSet<A>
copyFrom(java.lang.Iterable<A> source)
Creates anImmutableSet
that is copied from anyIterable
.default Set<A>
distinct()
Since aSet
already contains only distinct values, this method always returns itself.static <A> ImmutableSet<A>
empty()
Returns an emptyImmutableSet
.default com.jnape.palatable.lambda.adt.Maybe<A>
find(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Finds an element of thisSet
that satisfies a predicate, if any.default boolean
isEmpty()
Tests whether thisSet
is empty.static <A> ImmutableNonEmptySet<A>
of(A first, A... more)
Creates aImmutableNonEmptySet
with the given elements.int
size()
Returns the size of thisSet
.default ImmutableSet<A>
toImmutable()
Converts thisSet
to anImmutableSet
.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> Set<A>
wrap(java.util.Set<A> underlying)
Creates aSet
that wraps ajava.util.Set
.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, take, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, concat, concat, cross, cross, cycle, drop, dropWhile, filter, fmap, foldLeft, foldRight, inits, intersperse, magnetizeBy, partition, prepend, prependAll, reverse, slide, span, tails, takeWhile, toFinite, zipWith
-
-
-
-
Method Detail
-
contains
boolean contains(A element)
Tests if an element is a member of thisSet
.- Parameters:
element
- the element to test- Returns:
- true if
element
is a member of thisSet
, false otherwise
-
size
int size()
Returns the size of thisSet
.Executes in O(1).
-
distinct
default Set<A> distinct()
Since aSet
already contains only distinct values, this method always returns itself.- Specified by:
distinct
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Returns:
- itself
-
find
default com.jnape.palatable.lambda.adt.Maybe<A> find(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Finds an element of thisSet
that satisfies a predicate, if any.- Specified by:
find
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Parameters:
predicate
- a predicate; not null- Returns:
- an element wrapped in a
Maybe.just(A)
if a matching element is found;Maybe.nothing()
otherwise.
-
isEmpty
default boolean isEmpty()
Tests whether thisSet
is empty.Executes in O(1).
-
toImmutable
default ImmutableSet<A> toImmutable()
Converts thisSet
to anImmutableSet
. Converts thisSet
to anImmutableSet
.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
Set
is already anImmutableSet
, no copies are made and this method is a no-op.- Returns:
- an
ImmutableSet
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
.If successful, returns a
NonEmptySet
containing the same elements as this one, wrapped in aMaybe.just(A)
.If this
Set
is empty, returnsMaybe.nothing()
.Does not make copies of any underlying data structures.
-
toNonEmptyOrThrow
default NonEmptySet<A> toNonEmptyOrThrow()
Attempts to convert thisSet
to aNonEmptySet
.If successful, returns a
NonEmptySet
containing the same elements as this one. Use this if you are confident that thisSet
is not empty.If this
Set
is empty, throws anIllegalArgumentException
.Does not make copies of any underlying data structures.
- Returns:
- a
NonEmptySet<A>
- Throws:
java.lang.IllegalArgumentException
- if thisSet
is empty
-
empty
static <A> ImmutableSet<A> empty()
Returns an emptyImmutableSet
.- Type Parameters:
A
- the element type- Returns:
- an empty
ImmutableSet<A>
-
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>
-
builder
static <A> SetBuilder<A> builder()
Creates a newSetBuilder
.- Type Parameters:
A
- the element type- Returns:
- an empty
SetBuilder
-
builder
static <A> SetBuilder<A> builder(int initialCapacity)
Creates a newSetBuilder
with an initial capacity hint.- Type Parameters:
A
- the element type- Parameters:
initialCapacity
- an initial capacity hint. Must be >= 0.- Returns:
- an empty
SetBuilder
-
wrap
static <A> Set<A> wrap(java.util.Set<A> underlying)
Creates aSet
that wraps ajava.util.Set
.Does not make any copies of the given
Set
. The createdSet
will hold a reference to the given underlyingSet
, but will not alter it in any way.Bearers of the created
Set
will be unable to gain access to the underlyingSet
, so 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. UsecopyFrom(java.lang.Iterable<A>)
if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
-Set
to wrap; not null- Returns:
- a
Set<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(java.lang.Iterable<A> source)
Creates anImmutableSet
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 anImmutableSet
, 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
ImmutableSet<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(A[] source)
Creates anImmutableSet
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
ImmutableSet<A>
-
copyFrom
static <A> ImmutableSet<A> copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableSet
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 anImmutableSet
and its size 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.source
- anIterable<A>
that will be iterated eagerly for up tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableSet
that contains at mostmaxCount
elements
-
copyFrom
static <A> ImmutableSet<A> copyFrom(int maxCount, A[] source)
Returns a newImmutableSet
that is copied from an array.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to copy from the array. Must be >= 0.source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableSet<A>
-
-