Commons functional

A collection of Functional helpers

License

License

GroupId

GroupId

es.rubenjgarcia
ArtifactId

ArtifactId

commons-functional
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

Commons functional
A collection of Functional helpers
Project URL

Project URL

https://github.com/rubenjgarcia/commons-functional
Source Code Management

Source Code Management

http://github.com/rubenjgarcia/commons-functional/tree/master

Download commons-functional

How to add to project

<!-- https://jarcasting.com/artifacts/es.rubenjgarcia/commons-functional/ -->
<dependency>
    <groupId>es.rubenjgarcia</groupId>
    <artifactId>commons-functional</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/es.rubenjgarcia/commons-functional/
implementation 'es.rubenjgarcia:commons-functional:0.2.0'
// https://jarcasting.com/artifacts/es.rubenjgarcia/commons-functional/
implementation ("es.rubenjgarcia:commons-functional:0.2.0")
'es.rubenjgarcia:commons-functional:jar:0.2.0'
<dependency org="es.rubenjgarcia" name="commons-functional" rev="0.2.0">
  <artifact name="commons-functional" type="jar" />
</dependency>
@Grapes(
@Grab(group='es.rubenjgarcia', module='commons-functional', version='0.2.0')
)
libraryDependencies += "es.rubenjgarcia" % "commons-functional" % "0.2.0"
[es.rubenjgarcia/commons-functional "0.2.0"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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 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<Integer> ints = Arrays.asList(1, 2, 3);
List<String> strings = Arrays.asList("a", "b", "c");

List<Tuple2<Integer, String>> 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<String> strings = Arrays.asList("1", "2", "3");
List<Tuple2<Long, String>> tuples = zipWithIndex(strings.stream()).collect(Collectors.toList());

unzip

Converts a collection of Tuple2 into a Tuple2 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<Integer>, Stream<String>> tupleStream = unzip(tuples);
List<Integer> ints = tupleStream._1.collect(Collectors.toList());
List<String> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> list = Arrays.asList(1, 1, 1, 1);
List<Object> result = mapIf(list::stream, i -> i == 1, i -> "A")
        .collect(Collectors.toList()); // [A, A, A, A]
List<Integer> list = Arrays.asList(1, 2, 2, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 3, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 3, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 3, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 3, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 2, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 1, 1, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 3, 1);
List<Object> 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<Integer> list = Arrays.asList(1, 2, 1, 1);
List<Object> 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, BiFunction, Consumer, BiConsumer, Predicate, BiPredicate or Supplier that throws any checked Exception

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<String, String> 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<String, String> 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<String, String> 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

<dependency>
    <groupId>es.rubenjgarcia</groupId>
    <artifactId>commons-functional</artifactId>
    <version>0.2.0</version>
</dependency>

Versions

Version
0.2.0
0.1.0