Package com.activeviam.collections
Interface ImmutableCollection<E>
-
- Type Parameters:
E- The type of elements in this collection.
- All Superinterfaces:
Iterable<E>
- All Known Subinterfaces:
ImmutableList<E>,ImmutableSet<E>
- All Known Implementing Classes:
ImmutableArrayList,ImmutableHashSet
public interface ImmutableCollection<E> extends Iterable<E>
Immutable collections with a functional API.- Author:
- ActiveViam
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description ImmutableCollection<E>add(E e)Create a new collection having the items of this collection plus one new item.ImmutableCollection<E>add(E first, E second, E... rest)Create a new collection having the items of this collection plus new items.default ImmutableCollection<E>addAll(ImmutableCollection<E> items)Create a new collection having the items of this collection plus items from another collection.default ImmutableCollection<E>addAll(E[] items)Create a new collection having the items of this collection plus items from an array.ImmutableCollection<E>addAll(Collection<? extends E> items)Create a new collection having the items of this collection plus items from another collection.booleancontains(E item)Indicate if a given item is contained in this collection.default booleandoesNotContain(E item)Indicate if a given item is not contained in this collection.default booleanevery(Predicate<E> predicate)Test if all the items of this collection match a given predicate.default Optional<E>find(Predicate<E> predicate)Find the first item that matches a given predicate.default intfindIndex(Predicate<E> predicate)Find the number of iterations that were needed to find an item matching the given predicate.default Efirst()Give the first item of this collection.<R> ImmutableCollection<R>flatMap(Function<E,Iterable<R>> mapper)Flat map this collection.default voidforEach(BiFunction<E,Integer,Boolean> loop)Apply a function for each item of this collection until this function return false.default voidforEachUntil(Predicate<E> loop)Apply a function for each item of this collection until this function return false.default <R> Optional<R>getFirst(Function<E,Optional<R>> optionalResult)When all the items of this collection can produce an optional result via a method, returns the first non empty result.default booleanisEmpty()Indicate if this collection is empty.default booleanisNotEmpty()Indicate if this collection contains at least one item.default <R> Rreduce(BiFunction<R,E,R> reducer, R initialValue)Apply a reducer to this collection.intsize()Give the size of this collection.default booleansome(Predicate<E> predicate)Test if one of the items of this collection matches a given predicate.E[]toArray(E[] a)Copy the content of this collection in an array.default E[]toArray(IntFunction<E[]> a)Transforms this collection into an array.Collection<E>toCollection()Create a read-only java util collection from this collection.-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
Method Detail
-
size
int size()
Give the size of this collection.- Returns:
- The size of this collection.
-
add
ImmutableCollection<E> add(E e)
Create a new collection having the items of this collection plus one new item.- Parameters:
e- The items to add.- Returns:
- The new collection.
-
add
ImmutableCollection<E> add(E first, E second, E... rest)
Create a new collection having the items of this collection plus new items.- Parameters:
first- The first item to add.second- The second item to add.rest- The other items to add.- Returns:
- The new collection.
-
contains
boolean contains(E item)
Indicate if a given item is contained in this collection.- Parameters:
item- The item to search for.- Returns:
- If this item was found in this collection.
- See Also:
Collection.contains(Object)
-
toCollection
Collection<E> toCollection()
Create a read-only java util collection from this collection.- Returns:
- The collection.
-
flatMap
<R> ImmutableCollection<R> flatMap(Function<E,Iterable<R>> mapper)
Flat map this collection.- Type Parameters:
R- The type returned by the mapper.- Parameters:
mapper- The mapping function.- Returns:
- The iterable representing the mapped elements.
- See Also:
Stream.flatMap(Function)
-
first
default E first() throws NoSuchElementException
Give the first item of this collection.- Returns:
- The first item of this collection.
- Throws:
NoSuchElementException- If this collection is empty.
-
isEmpty
default boolean isEmpty()
Indicate if this collection is empty.- Returns:
- If this collection is empty.
-
isNotEmpty
default boolean isNotEmpty()
Indicate if this collection contains at least one item.- Returns:
- If this collection contains any item.
-
addAll
ImmutableCollection<E> addAll(Collection<? extends E> items)
Create a new collection having the items of this collection plus items from another collection.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
addAll
default ImmutableCollection<E> addAll(ImmutableCollection<E> items)
Create a new collection having the items of this collection plus items from another collection.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
addAll
default ImmutableCollection<E> addAll(E[] items)
Create a new collection having the items of this collection plus items from an array.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
forEachUntil
default void forEachUntil(Predicate<E> loop)
Apply a function for each item of this collection until this function return false.- Parameters:
loop- The function to call with each item of this collection.
-
forEach
default void forEach(BiFunction<E,Integer,Boolean> loop)
Apply a function for each item of this collection until this function return false.- Parameters:
loop- The function to call with each item of this collection and its index.
-
reduce
default <R> R reduce(BiFunction<R,E,R> reducer, R initialValue)
Apply a reducer to this collection.- Type Parameters:
R- The output type of the reducer.- Parameters:
reducer- The reducer.initialValue- The initial value.- Returns:
- The reduced value.
- See Also:
Stream.reduce(Object, java.util.function.BinaryOperator)
-
some
default boolean some(Predicate<E> predicate)
Test if one of the items of this collection matches a given predicate.- Parameters:
predicate- The predicate to use.- Returns:
- If one of the items of this collection matches the predicate.
-
every
default boolean every(Predicate<E> predicate)
Test if all the items of this collection match a given predicate.- Parameters:
predicate- The predicate to use.- Returns:
- If all the items of this collection match the predicate.
-
find
default Optional<E> find(Predicate<E> predicate)
Find the first item that matches a given predicate.- Parameters:
predicate- The predicate with which to test this collection's items.- Returns:
- The item in an optional. This optional will be empty if no item matched.
-
findIndex
default int findIndex(Predicate<E> predicate)
Find the number of iterations that were needed to find an item matching the given predicate.- Parameters:
predicate- The predicate with which to test this collection's items.- Returns:
- The number of iterations, or -1 if no item matched the predicate.
-
doesNotContain
default boolean doesNotContain(E item)
Indicate if a given item is not contained in this collection.- Parameters:
item- The item to search for.- Returns:
- If this item was not found in this collection.
- See Also:
contains(Object)
-
toArray
E[] toArray(E[] a)
Copy the content of this collection in an array.- Parameters:
a- The array to attempt copy into.- Returns:
- An array containing the items of this collection. Can be different than a if a was not large enough.
- See Also:
Collection.toArray(Object[])
-
toArray
default E[] toArray(IntFunction<E[]> a)
Transforms this collection into an array.- Parameters:
a- The function that creates an array of the correct type given its size as input.- Returns:
- The array containing the items in this collection.
-
getFirst
default <R> Optional<R> getFirst(Function<E,Optional<R>> optionalResult)
When all the items of this collection can produce an optional result via a method, returns the first non empty result.- Type Parameters:
R- The type of the value contained in the createdOptional.- Parameters:
optionalResult- The function creating the optional result from an item in the collection. Will only be called while the current result is an empty optional.- Returns:
- The optional with the first non empty result, or an empty optional if all results were empty.
-
-