Overview
enumerable4j is a Ruby's well known Enumerable ported to java
as interface with set of default methods which simplify typical operations with collections.
/**
* The iterable with primitive operations witch simplify typical actions like count, map, etc.
*
* The API is based on Ruby's Enumerable:
* https://ruby-doc.org/core-2.6/Enumerable.html.
*
* The Enumerable provides methods with several traversal and searching features, and with the
* ability to sort. The class must provide a method each, which yields successive members of the
* collection.
*
* @param <X> The type of entities.
* @since 0.1.0
*/
public interface Enumerable<X> extends Collection<X> {
/**
* Passes each element of the collection to the given block.
* @param prd The predicate to match each element.
* @return The true if the block never returns false or nil.
*/
default boolean all(Predicate<T> prd) {
// ...
}
/**
* Passes at least one element of the collection to the given block.
* @param prd The predicate to match at least one element.
* @return The true if the block never returns false or nil.
*/
default boolean any(Predicate<T> prd) {
// ...
}
/**
* Doesn't passes elements of the collection to the given block.
* @param prd The predicate to match none elements.
* @return The true if the block never returns false or nil.
*/
default boolean none(Predicate<T> prd) {
// ...
}
/**
* Returns an enumerable containing all elements of enumerable for which the given function
* returns a true value.
* If no predicate (null) is given, then 'this' is returned instead.
* @param prd The function to match each element.
* @return The enumerable.
*/
default Enumerable<T> select(Predicate<T> prd) {
// ...
}
/**
* Returns an enumerable containing all elements of enumerable for which the given function
* returns a false value.
* If no predicate (null) is given, then 'this' is returned instead.
* @param prd The function to match each element.
* @return The enumerable.
*/
default Enumerable<T> reject(Predicate<T> prd) {
// ...
}
/**
* Returns an enumerable containing first element of enumerable for which the given function
* returns a true value.
* If no predicate (null) is given, or no element found then null is returned instead.
* @param prd The function to match each element.
* @return The first element of enumerable, that matches predicate.
*/
default T find(Predicate<T> prd) {
// ...
}
/**
* Returns an enumerable containing first element of enumerable for which the given function
* returns a true value.
* If no predicate (null) is given, or no element found then alternative is returned instead.
* @param prd The function to match each element.
* @param alt The alternative to return in case of null predicate or no element found.
* @return The first element of enumerable, that matches predicate.
*/
default T find(Predicate<T> prd, T alt) {
// ...
}
/**
* Returns an enumerable containing all elements, on which given function was applied.
* If no function (null) is given, then 'this' is returned instead.
* @param fnc The function to apply to each element.
* @param <Y> The type of target entity.
* @return The enumerable.
*/
default <Y> Enumerable<Y> map(Function<? super T, ? extends Y> fnc) {
// ...
}
/**
* Returns the number of elements that are present in enumerable for which the given
* function return true.
* If no function (null) is given, then 'size' is returned instead.
* @param prd The function to match each element.
* @return Number of elements satisfying the given function.
*/
default long count(Predicate<T> prd) {
// ...
}
}
See more.
How to use
-
Get the latest version here:
<dependency> <groupId>io.github.dgroup</groupId> <artifactId>enumerable4j</artifactId> <version>${version}</version> </dependency>
-
Assign the Enumerable interface with default methods to your own collection
/** * The collection which you implemented in your project for some purposes. */ public class YourOwnCollection<X> extends Collection<X> implements Enumerable<X> { // }
You may (but not required) override the default implementations of methods from Enumerable if needed.
-
Java version required: 1.8+.
-
Comparing matrix with other libs:
enumerable4j (MIT) Java 8 cactoos (MIT) eclipse-collections (EDL) .all(...)
.stream().allMatch(...);
new And<>(...,...).value()
tbd .any(...)
.stream().anyMatch(...);
new Or<>(...,...).value()
tbd .none(...)
.stream().noneMatch(...);
new And<>(...,...).value()
tbd .select(...)
.stream().filter(...).collect(Collectors.toList())
new Filtered<>(...,...)
tbd .reject(...)
.stream().filter((...).negate()).collect(Collectors.toList())
new Filtered<>(...,...)
tbd .map(...)
.stream().map(...).collect(Collectors.toList())
new Mapped<>(...,...)
tbd .count(...)
.stream().filter(...).count()
new Filtered<>(...).size()
tbd .find(...)
.stream().filter(...).findFirst().orElse(...)
new FirstOf<>(...,...).value()
tbd
.all
YourOwnCollection<Integer> src = ... // with elements [1, 2, 3]
boolean allPositive = src.all(v -> v > 0); // true
.any
YourOwnCollection<Integer> src = ... // with elements [-1, 0, 1]
boolean oneIsPositive = src.any(v -> v > 0); // true
.none
YourOwnCollection<Integer> src = ... // with elements [-2, -1, 0]
boolean noneIsPositive = src.none(v -> v > 0); // true
.select
YourOwnCollection<Integer> src = ... // with elements [-1, 1, 2]
Enumerable<Integer> positive = src.select(v -> v > 0); // [1, 2]
.reject
YourOwnCollection<Integer> src = ... // with elements [-1, 1, 2]
Enumerable<Integer> negative = src.reject(v -> v > 0); // [-1]
.map
YourOwnCollection<Integer> src = ... // with elements [0, 1, 2]
Enumerable<Integer> positive = src.map(v -> v + 1); // [1, 2, 3]
.count
YourOwnCollection<Integer> src = ... // with elements [-1, 0, 1]
long countNegative = src.count(val -> val < 0); // 1
.find
YourOwnCollection<Integer> src = ... // with elements [-1, 0, 1]
Integer first = src.find(val -> val > 0); // 1
Integer alternative = src.find(val -> val > 5, 50); // 50
How to contribute?
- Pull requests are welcome! Don't forget to add your name to contribution section and run this, beforehand:
mvn -Pqulice clean install
- Everyone interacting in this project’s codebases, issue trackers, chat rooms is expected to follow the code of conduct.
- Latest maven coordinates here:
<dependency> <groupId>io.github.dgroup</groupId> <artifactId>enumerable4j</artifactId> <version>${version}</version> </dependency>
Contributors
- dgroup as Yurii Dubinka ([email protected])
- smithros as Rostyslav Koval ([email protected])
- ashutosh as Ashutosh Singh ([email protected])