Interface NonEmptyVector<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>,java.util.RandomAccess,Vector<A>
- All Known Subinterfaces:
ImmutableNonEmptyVector<A>
public interface NonEmptyVector<A> extends software.kes.enhancediterables.NonEmptyFiniteIterable<A>, Vector<A>
AVectorthat is guaranteed at compile-time to contain at least one element.In addition to guarantees of
Vector, provides the following benefits:head()method that returns the first element.- Implements
NonEmptyIterable. fmap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends B>)always returns aNonEmptyVector.reverse()always returns aNonEmptyVector.zipWithIndex()always returns aNonEmptyVector.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <A> NonEmptyVectorBuilder<A>builder(int initialCapacity, A first)Creates a newNonEmptyVectorBuilderwith an initial capacity hint.static <A> NonEmptyVectorBuilder<A>builder(A first)Creates a newNonEmptyVectorBuilder.static <A> ImmutableNonEmptyVector<A>copyFromOrThrow(int maxCount, A[] source)Attempts to create anImmutableNonEmptyVectorthat is copied from an array, but with a maximum number of elements.static <A> ImmutableNonEmptyVector<A>copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptyVectorfrom anyIterable, but consuming a maximum number of elements.static <A> ImmutableNonEmptyVector<A>copyFromOrThrow(A[] source)Attempts to create anImmutableNonEmptyVectorthat is copied from an array.static <A> ImmutableNonEmptyVector<A>copyFromOrThrow(java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptyVectorfrom anyIterable.default <B> NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>cross(NonEmptyVector<B> other)Returns the cartesian product of thisNonEmptyVectorwith anotherNonEmptyVector.static <A> ImmutableNonEmptyVector<A>fill(int size, A value)Creates anImmutableNonEmptyVectorthat repeats the same elementsizetimes.default <B> NonEmptyVector<B>fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)Maps a function over thisNonEmptyVector.default Ahead()Returns the first element.default ImmutableNonEmptyVector<java.lang.Integer>indices()Returns anImmutableNonEmptyVector<Integer>that contains all the indices of thisNonEmptyVector.default Vector<A>init()Returns the init of thisNonEmptyVector.default booleanisEmpty()Tests whether thisNonEmptyVectoris empty.default java.util.Iterator<A>iterator()Returns an iterator over thisNonEmptyVector's elements.default Alast()Returns the last element.static <A> ImmutableNonEmptyVector<A>lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)Creates anImmutableNonEmptyVectorwhere elements are lazily evaluated.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>maybeCopyFrom(int maxCount, A[] source)Attempts to create anImmutableNonEmptyVectorthat is copied from an array, but with a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptyVectorthat is copied from anyIterable, but consuming a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>maybeCopyFrom(A[] source)Attempts to create anImmutableNonEmptyVectorthat is copied from an array.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>maybeCopyFrom(java.lang.Iterable<A> source)Attempts to create anImmutableNonEmptyVectorthat is copied from anyIterable.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>>maybeWrap(A[] underlying)Attempts to create aNonEmptyVectorthat wraps an array.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>>maybeWrap(java.util.List<A> underlying)Attempts to create aNonEmptyVectorthat wraps ajava.util.List.static <A> ImmutableNonEmptyVector<A>nonEmptyCopyFrom(int maxCount, software.kes.enhancediterables.NonEmptyIterable<A> source)Creates anImmutableNonEmptyVectorthat is copied from anyNonEmptyIterable, but consuming a maximum number of elements.static <A> ImmutableNonEmptyVector<A>nonEmptyCopyFrom(software.kes.enhancediterables.NonEmptyIterable<A> source)Creates anImmutableNonEmptyVectorthat is copied from anyNonEmptyIterable.static <A> ImmutableNonEmptyVector<A>of(A first, A... more)Creates aImmutableNonEmptyVectorwith the given elements.static ImmutableNonEmptyVector<java.lang.Integer>range(int size)Creates anImmutableNonEmptyVector<Integer>containing elements 0..size - 1.default NonEmptyVector<A>reverse()Creates aNonEmptyVectorwith thisNonEmptyVector's elements in reversed order.default Vector<A>tail()Returns the tail of thisNonEmptyVector.default ImmutableNonEmptyVector<A>toImmutable()Converts thisNonEmptyVectorto anImmutableNonEmptyVector.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>>toNonEmpty()Attempts to convert thisVectorto aNonEmptyVector.default NonEmptyVector<A>toNonEmptyOrThrow()Attempts to convert thisVectorto aNonEmptyVector.static <A> NonEmptyVector<A>wrapOrThrow(A[] underlying)Attempts to create aNonEmptyVectorthat wraps an array.static <A> NonEmptyVector<A>wrapOrThrow(java.util.List<A> underlying)Attempts to create aNonEmptyVectorthat wraps ajava.util.List.default <B,C>
NonEmptyVector<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, NonEmptyVector<B> other)Zips together thisNonEmptyVectorwith anotherNonEmptyVectorby applying a zipping function.default NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>>zipWithIndex()Zips thisNonEmptyVectorwith its indices.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, find, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, cross, cross, dropWhile, filter, foldLeft, foldRight, partition, prepend, span, takeWhile, zipWith
-
-
-
-
Method Detail
-
cross
default <B> NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(NonEmptyVector<B> other)
Returns the cartesian product of thisNonEmptyVectorwith anotherNonEmptyVector.Does not make copies of any underlying collections.
The returned
NonEmptyVectorwill have a size ofsize()×other.size(), but will allocate no extra memory (aside from a few bytes for housekeeping).- Type Parameters:
B- the type of the otherNonEmptyVector- Parameters:
other- aNonEmptyVectorof any type- Returns:
- a
NonEmptyVector<Tuple2<A, B>>
-
fmap
default <B> NonEmptyVector<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisNonEmptyVector.Returns a new
NonEmptyVectorof the same size (but possibly a different type).Does not make any copies of underlying collections.
This method is stack-safe, so a
NonEmptyVectorcan be mapped as many times as the heap permits.- Specified by:
fmapin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Specified by:
fmapin interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Specified by:
fmapin interfacecom.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>- Specified by:
fmapin interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>- Specified by:
fmapin interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>- Specified by:
fmapin interfaceVector<A>- Type Parameters:
B- The type of the elements contained in the output Vector.- Parameters:
f- a function fromAtoB. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- a
NonEmptyVector<B>of the same size
-
head
default A head()
Returns the first element.- Specified by:
headin interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>- Returns:
- an element of type
A
-
indices
default ImmutableNonEmptyVector<java.lang.Integer> indices()
Returns anImmutableNonEmptyVector<Integer>that contains all the indices of thisNonEmptyVector.
-
init
default Vector<A> init()
Returns the init of thisNonEmptyVector.The init of a
NonEmptyVectoris the sameVectorwith the last element dropped. May be empty.Does not make copies of any underlying collections.
- Specified by:
initin interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>- Returns:
- a
Vector<A>
-
isEmpty
default boolean isEmpty()
Tests whether thisNonEmptyVectoris empty.Always returns false for
NonEmptyVectors.
-
iterator
default java.util.Iterator<A> iterator()
Returns an iterator over thisNonEmptyVector's elements.
-
last
default A last()
Returns the last element.- Specified by:
lastin interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>- Returns:
- an element of type
A
-
reverse
default NonEmptyVector<A> reverse()
Creates aNonEmptyVectorwith thisNonEmptyVector's elements in reversed order.Does not make copies of any underlying collections.
-
tail
default Vector<A> tail()
Returns the tail of thisNonEmptyVector.The tail of a
NonEmptyVectoris the sameVectorwith the first element dropped. May be empty.Does not make copies of any underlying collections.
-
toImmutable
default ImmutableNonEmptyVector<A> toImmutable()
Converts thisNonEmptyVectorto anImmutableNonEmptyVector.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
NonEmptyVectoris already anImmutableNonEmptyVector, no copies are made and this method is a no-op.- Specified by:
toImmutablein interfaceVector<A>- Returns:
- an
ImmutableNonEmptyVectorof the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>> toNonEmpty()
Attempts to convert thisVectorto aNonEmptyVector.Since this will always be successful for
NonEmptyVectors, this method always returns itself wrapped in aMaybe.just(A).Does not make copies of any underlying collections.
- 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 interfaceVector<A>- Returns:
- this
NonEmptyVectorwrapped in aMaybe.just(A)
-
toNonEmptyOrThrow
default NonEmptyVector<A> toNonEmptyOrThrow()
Attempts to convert thisVectorto aNonEmptyVector.Since this will always be successful for
NonEmptyVectors, this method always returns itself.Does not make copies of any underlying collections.
- Specified by:
toNonEmptyOrThrowin interfaceVector<A>- Returns:
- this
NonEmptyVector
-
zipWith
default <B,C> NonEmptyVector<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, NonEmptyVector<B> other)
Zips together thisNonEmptyVectorwith anotherNonEmptyVectorby applying a zipping function.Applies the function to the successive elements of of each
NonEmptyVectoruntil one of them runs out of elements.Does not make copies of any underlying collections.
- Type Parameters:
B- The element type of the otherNonEmptyVectorC- The element type of the result- Parameters:
fn- The zipping function. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.other- The otherNonEmptyVector- Returns:
- A
NonEmptyVector<C>
-
zipWithIndex
default NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>> zipWithIndex()
Zips thisNonEmptyVectorwith its indices.Does not make copies of any underlying collections.
- Specified by:
zipWithIndexin interfaceVector<A>- Returns:
- a new
NonEmptyVectorcontaining pairs consisting of all elements of thisNonEmptyVectorpaired with their index. Indices start at 0.
-
of
@SafeVarargs static <A> ImmutableNonEmptyVector<A> of(A first, A... more)
Creates aImmutableNonEmptyVectorwith the given elements.- Type Parameters:
A- the element type- Parameters:
first- the first elementmore- the remaining elements- Returns:
- an
ImmutableNonEmptyVector<A>
-
builder
static <A> NonEmptyVectorBuilder<A> builder(A first)
Creates a newNonEmptyVectorBuilder.- Type Parameters:
A- the element type- Parameters:
first- the first element- Returns:
- an empty
VectorBuilder
-
builder
static <A> NonEmptyVectorBuilder<A> builder(int initialCapacity, A first)
Creates a newNonEmptyVectorBuilderwith an initial capacity hint.- Type Parameters:
A- the element type- Parameters:
initialCapacity- an initial capacity hint. Must be >= 0.first- the first element- Returns:
- an empty
VectorBuilder
-
maybeWrap
static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>> maybeWrap(A[] underlying)
Attempts to create aNonEmptyVectorthat wraps an array.Does not make any copies of the given array. The created
NonEmptyVectorwill hold on to a reference to the array, but will never alter it in any way.Bearers of the created
NonEmptyVectorwill be unable to gain access to the underlying array, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the array can still mutate it. Use
Vector.copyFrom(java.lang.Iterable<A>)instead if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying- the array to wrap; not null- Returns:
- a
NonEmptyVector<A>wrapped in aMaybe.just(A)ifunderlyingis non-empty;Maybe.nothing()otherwise.
-
maybeWrap
static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>> maybeWrap(java.util.List<A> underlying)
Attempts to create aNonEmptyVectorthat wraps ajava.util.List.Does not make any copies of the given
List. The createdNonEmptyVectorwill hold a reference to the givenList, but will not alter it in any way.Bearers of the
NonEmptyVectorwill be unable to gain access to the underlyingList, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the
Listcan still mutate it. Mutating theListis not advised. Operations that change the size of the underlyingListwill 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.Listto wrap; not null- Returns:
- a
NonEmptyVector<A>wrapped in aMaybe.just(A)ifunderlyingis non-empty;Maybe.nothing()otherwise.
-
wrapOrThrow
static <A> NonEmptyVector<A> wrapOrThrow(A[] underlying)
Attempts to create aNonEmptyVectorthat wraps an array. If it is not possible, throws anIllegalArgumentException.Does not make any copies of the given array. The created
NonEmptyVectorwill hold on to a reference to the array, but will never alter it in any way.Bearers of the created
NonEmptyVectorwill be unable to gain access to the underlying array, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the array can still mutate it. Use
copyFromOrThrow(Object[])instead if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying- array to wrap; not null- Returns:
- a
NonEmptyVector<A>ifunderlyingis non-empty; throws anIllegalArgumentExceptionotherwise
-
wrapOrThrow
static <A> NonEmptyVector<A> wrapOrThrow(java.util.List<A> underlying)
Attempts to create aNonEmptyVectorthat wraps ajava.util.List. If it is not possible, throws anIllegalArgumentException.Does not make any copies of the given
List. The createdNonEmptyVectorwill hold a reference to the givenjava.util.List, but will not alter it in any way.Bearers of the created
NonEmptyVectorwill be unable to gain access to the underlyingList, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the
Listcan still mutate it. Mutating theListis not advised. Operations that change the size of the underlyingListwill result in unpredictable behavior. UsecopyFromOrThrow(Iterable)if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying-Listto wrap; not null- Returns:
- a
NonEmptyVector<A>ifunderlyingis non-empty; throws anIllegalArgumentExceptionotherwise
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVectorthat 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 anImmutableNonEmptyVector, 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
ImmutableNonEmptyVector<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(A[] source)
Attempts to create anImmutableNonEmptyVectorthat 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
ImmutableNonEmptyVector<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVectorthat 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 anImmutableVectorand its size is less than or equal tomaxCount, in which case it will be returned directly.If
sourceis anImmutableVectorthat is greater thanmaxCountin size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVectorthat you no longer need.- 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
ImmutableNonEmptyVector<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVectorthat 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
ImmutableNonEmptyVector<A>wrapped in aMaybe.just(A)ifsourceis non-empty;Maybe.nothing()otherwise.
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVectorfrom 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 anImmutableVector, 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
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(A[] source)
Attempts to create anImmutableNonEmptyVectorthat 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
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVectorfrom 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 anImmutableVectorand its size is less than or equal tomaxCount, in which case it will be returned directly.If
sourceis anImmutableVectorthat is greater thanmaxCountin size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVectorthat you no longer need.- 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
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVectorthat 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
ImmutableNonEmptyVector<A>
-
fill
static <A> ImmutableNonEmptyVector<A> fill(int size, A value)
Creates anImmutableNonEmptyVectorthat repeats the same elementsizetimes.Uses O(1) memory.
- Type Parameters:
A- the element type- Parameters:
size- the number of elements. Must be >= 1.value- the value that will be repeated all elements of theImmutableNonEmptyVector- Returns:
- an
ImmutableVector<A>ofsizeelements, with each element having the valuevalue
-
lazyFill
static <A> ImmutableNonEmptyVector<A> lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)
Creates anImmutableNonEmptyVectorwhere elements are lazily evaluated.Uses O(1) memory.
- Type Parameters:
A- the element type- Parameters:
size- the number of elements. Must be >= 1.valueSupplier- a function that accepts an index and returns the computed value for that index. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableNonEmptyVector<A>
-
nonEmptyCopyFrom
static <A> ImmutableNonEmptyVector<A> nonEmptyCopyFrom(software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVectorthat is copied from anyNonEmptyIterable.The entire
NonEmptyIterablewill be eagerly iterated. Be careful not to pass in an infiniteNonEmptyIterableor this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
sourceis an untransformedImmutableNonEmptyVector, it will be returned directly.- Type Parameters:
A- the element type- Parameters:
source- aNonEmptyIterable<A>that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptyVector<A>
-
nonEmptyCopyFrom
static <A> ImmutableNonEmptyVector<A> nonEmptyCopyFrom(int maxCount, software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVectorthat is copied from anyNonEmptyIterable, but consuming a maximum number of elements.The
NonEmptyIterablewill 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 an untransformedImmutableNonEmptyVectorand its size is less than or equal tomaxCount, in which case it will be returned directly.If
sourceis anImmutableNonEmptyVectorthat is greater thanmaxCountin size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVectorthat you no longer need.- Type Parameters:
A- the element type- Parameters:
maxCount- the maximum number of elements to consume from the source. Must be >= 1.source- aNonEmptyIterable<A>that will be iterated eagerly for up tomaxCountelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableNonEmptyVectorthat contains at mostmaxCountelements
-
range
static ImmutableNonEmptyVector<java.lang.Integer> range(int size)
Creates anImmutableNonEmptyVector<Integer>containing elements 0..size - 1. In other words, each element of the returnedVectorwill contains its index.Uses O(1) memory.
- Parameters:
size- the number of elements. Must be >= 1.- Returns:
- an
ImmutableNonEmptyVector<Integer>
-
-