com.ajjpj.abase.collection.immutable.AMonadicOps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.collection.immutable;
import com.ajjpj.abase.function.AFunction1;
import com.ajjpj.abase.function.APredicate;
/**
* This interface contains those functional operations that are supported for all kinds of collections. This is the greatest common
* denominator of all a-base collection classes.
*
* @author arno
*/
public interface AMonadicOps {
/**
* Filters this collection's elements, this method returns a new collection comprised of only those elements that match
* a given predicate.
*/
AMonadicOps filter(APredicate super T, E> pred) throws E;
/**
* Turns a collection of collections into a 'flat' collection, removing one layer of collections.
*/
AMonadicOps flatten();
/**
* Applies a transformation function to each element, creating a new collection instance from the results. For a
* collection of strings, this could e.g. be used to create a collection of integer values with each string's length.
*/
AMonadicOps map(AFunction1 super T, ? extends X, E> f) throws E;
/**
* Maps each element to a collection of values, flattening the results into a single collection. Let there be a
* collection of strings. Calling flatMap
with a function that splits a string into tokens, the result
* would be a collection of all tokens of all original strings.
*/
AMonadicOps flatMap(AFunction1 super T, ? extends Iterable, E> f) throws E;
/**
* Wraps a filter around the existing collection, making creation of the result a constant time operation. This comes
* at the price that the actual filtering is done for every iteration.
*/
//TODO AMonadicOps> withFilter(APredicate pred) throws E;
/**
* Searches through this collection's elements and returns the first element matching a given predicate. if any.
*/
AOption find(APredicate super T, E> pred) throws E;
/**
* Returns true iff all elements of the collection match the predicate.
*/
boolean forAll(APredicate super T, E> pred) throws E;
/**
* Returns true iff at least one element of the collection matches the predicate.
*/
boolean exists(APredicate super T, E> pred) throws E;
}