Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rubenjgarcia/commons-functional
A collection of Functional helpers for Java 8
https://github.com/rubenjgarcia/commons-functional
functional functional-programming java-8 tuples
Last synced: 9 days ago
JSON representation
A collection of Functional helpers for Java 8
- Host: GitHub
- URL: https://github.com/rubenjgarcia/commons-functional
- Owner: rubenjgarcia
- License: mit
- Created: 2016-10-07T15:31:34.000Z (over 8 years ago)
- Default Branch: dev
- Last Pushed: 2017-03-24T11:56:44.000Z (almost 8 years ago)
- Last Synced: 2024-11-15T04:48:39.437Z (2 months ago)
- Topics: functional, functional-programming, java-8, tuples
- Language: Java
- Homepage:
- Size: 33.2 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Commons functional
A collection of Functional helpers
## Tuples
From 1 to 10 fields
```
import es.rubenjgarcia.commons.functional.tuple.*;
import static es.rubenjgarcia.commons.functional.tuple.Tuple.tuple;Tuple2 t2 = tuple("1", "2");
t2.toString(); // (1, 2)
t2.equals(tuple("1", "2")); // true
```## Stream Utils
### zip
Returns a list of [Tuple2](src/main/java/es/rubenjgarcia/commons/functional/tuple/Tuple2.java) formed from two streams by combining corresponding elements in pairs. If one of the two collections is longer than the other, its remaining elements are ignored.
```
import static es.rubenjgarcia.commons.functional.StreamUtils.zip;List ints = Arrays.asList(1, 2, 3);
List strings = Arrays.asList("a", "b", "c");List> tuples = zip(ints.stream(), strings.stream()).collect(Collectors.toList());
tuples.toString(); // [(1, a), (2, b), (3, c)]
```### zipWithIndex
```
import static es.rubenjgarcia.commons.functional.StreamUtils.zipWithIndex;List strings = Arrays.asList("1", "2", "3");
List> tuples = zipWithIndex(strings.stream()).collect(Collectors.toList());
```### unzip
Converts a collection of [Tuple2](src/main/java/es/rubenjgarcia/commons/functional/tuple/Tuple2.java) into a [Tuple2](src/main/java/es/rubenjgarcia/commons/functional/tuple/Tuple2.java) of stream of the first and second half of each pair
```
import es.rubenjgarcia.commons.functional.tuple.*;
import static es.rubenjgarcia.commons.functional.StreamUtils.unzip;Tuple2, Stream> tupleStream = unzip(tuples);
List ints = tupleStream._1.collect(Collectors.toList());
List strings = tupleStream._2.collect(Collectors.toList());
```## Functional Filters
### anyOf
Return if the value matches any predicate
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.anyOf;Predicate p = anyOf(n -> n == 1, n -> n == 2);
p.test(1); // true
p.test(3); // false
```### noneOf
Return if the value doesn't match any of the predicates
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.noneOf;Predicate p = noneOf(n -> n == 1, n -> n == 2);
p.test(1); // false
p.test(3); // true
```### allOf
Return if the value matches all the predicates
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.allOf;Predicate p = allOf(n -> n > 1, n -> n < 3);
p.test(1); // false
p.test(2); // true
```### findFirst
Find the first element that matchs the predicate
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.findFirst;Optional found = findFirst(Stream.of(1, 2), n -> n == 1);
found.isPresent(); // true
found.get(); 1
```### findLast
Finds the last element that matchs the predicate
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.findLast;Optional last = findLast(Stream.of(1, 2, 3), p -> p > 1 && p <= 2);
last.isPresent(); // true
last.get(); // 2
```### findFirstNotMatch
Finds first element that doesn't match the predicate
```
import static es.rubenjgarcia.commons.functional.FunctionalFilters.findFirstNotMatch;Optional found = findFirstNotMatch(Stream.of(1, 2), n -> n == 1);
found.isPresent(); // true
found.get(); // 2
```## FlowStream
### mapIf
Apply a map function to the element of a stream if the element matches the predicate
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 1, 1, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.collect(Collectors.toList()); // [A, A, A, A]
``````
List list = Arrays.asList(1, 2, 2, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.collect(Collectors.toList()); // [A, 2, 2, A]
```### elseIfMap
Apply a map function to the element of a stream if the element matches the predicate after a mapIf code
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 2, 3, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.elseIfMap(i -> i == 2, i -> "B")
.collect(Collectors.toList()); // [A, B, 3, A]
``````
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 2, 3, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.elseIfMap(i -> i == 2, i -> "B")
.elseIfMap(i -> i == 3, i -> "C")
.collect(Collectors.toList()); // [A, B, C, A]
```### elseMap
Apply a map function to the elements of a stream that don't match any of the other if or elseif predicates
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 2, 3, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.elseIfMap(i -> i == 2, i -> "B")
.elseMap(i -> "C")
.collect(Collectors.toList()); // [A, B, C, A]
``````
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 2, 3, 1);
List result = mapIf(list::stream, i -> i == 1, i -> "A")
.elseMap(i -> "C")
.collect(Collectors.toList()); // [A, C, C, A]
```### mapIfAny
Apply a map function to all the elements of the stream if any of them match the predicate
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIfAny;List list = Arrays.asList(1, 2, 2, 1);
List result = mapIfAny(list::stream, i -> i == 1, i -> "A")
.collect(Collectors.toList()); // [A, A, A, A]
```### elseIfAnyMap
Apply a map function to all the elements of the stream if any of them match the predicate and don't match the other predicates
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIfAny;List list = Arrays.asList(1, 1, 1, 1);
List result = mapIfAny(list::stream, i -> i == 2, i -> "A")
.elseIfAnyMap(i -> i == 1, i -> "B")
.collect(Collectors.toList()); // [B, B, B, B]
``````
import static es.rubenjgarcia.commons.functional.FlowStream.mapIfAny;List list = Arrays.asList(1, 2, 3, 1);
List result = mapIfAny(list::stream, i -> i == 2, i -> "A")
.elseIfAnyMap(i -> i == 4, i -> "B")
.elseIfAnyMap(i -> i == 1, i -> "C")
.collect(Collectors.toList()); // [C, C, C, C]
```You can combine any of the flows
```
import static es.rubenjgarcia.commons.functional.FlowStream.mapIf;List list = Arrays.asList(1, 2, 1, 1);
List result = mapIf(list::stream, i -> i == 2, i -> "A")
.elseIfAnyMap(i -> i == 1, i -> "B")
.elseMap(i -> "C")
.collect(Collectors.toList()); // [B, A, B, B]
```## Exceptions
You can use `rethrowFunction`, `rethrowBiFunction`, `rethrowConsumer`, `rethrowBiConsumer`, `rethrowPredicate`, `rethrowBiPredicate` or `rethrowSupplier` to deal with [Function](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html), [BiFunction](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html), [Consumer](https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html), [BiConsumer](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html), [Predicate](https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html), [BiPredicate](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html) or [Supplier](https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html) that throws any checked [Exception](https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html)
```
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowFunction;Stream.of("a", "b").map(rethrowFunction(s -> new String(s.getBytes(), "Foo")));
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowBiFunction;Map map = new HashMap<>();
map.put("a", "b");
map.replaceAll(rethrowBiFunction((k, v) -> new String(k.getBytes(), "Foo")))
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowConsumer;Stream.of("a", "b").forEach(rethrowConsumer(s -> new String(s.getBytes(), "Foo")));
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowBiConsumer;Map map = new HashMap<>();
map.put("a", "b");
map.forEach(rethrowBiConsumer((k, v) -> new String(k.getBytes(), "Foo")));
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowPredicate;Stream.of("a", "b").filter(rethrowPredicate(s -> new String(s.getBytes(), "Foo") == null));
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowBiPredicate;BiPredicate predicate = rethrowBiPredicate((s, e) -> new String(s.getBytes(), e).isEmpty());
predicate.test("a", "Foo");
``````
import static es.rubenjgarcia.commons.functional.FunctionalExceptions.rethrowSupplier;rethrowPredicate(() -> new String(s.getBytes(), "Foo"));
```# Installation
Just add to your pom.xml
```
es.rubenjgarcia
commons-functional
0.2.0```