Commit fb045d45 authored by Hernan Ponce de Leon's avatar Hernan Ponce de Leon
Browse files

Merge branch '3203-Extend_the_LambdaUtils' into 'master'

3203 extend the lambda utils

See merge request !18
parents b09ab4f9 ae9925ec
LambdaUtils.java 7c3e3be7d04d62a8829b67bba78c10503bb4d2be GREEN
LambdaUtils.java 8eb64e9bc18049e78e3bdd98fd17fe82dfc8bb47 GREEN
......@@ -26,7 +26,6 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
......@@ -129,8 +128,8 @@ public class LambdaUtils {
* Filter to apply.
* @return Filtered collection as a List.
*/
public static <T, S extends T> List<T> filterList(Collection<S> collection,
Predicate<S> filter) {
public static <T, S extends T> List<T>
filterList(Collection<S> collection, Predicate<S> filter) {
return filterStream(collection, filter).collect(Collectors.toList());
}
......@@ -144,8 +143,8 @@ public class LambdaUtils {
* @return Sub-{@link Collection} of {@code types} that is derived from all {@code classifiers}.
*/
@SafeVarargs
public static <T, C> Collection<Class<? extends T>>
filterTypes(Collection<Class<? extends T>> types, Class<? extends C>... classifiers) {
public static <T, C> Collection<Class<? extends T>> filterTypes(
Collection<Class<? extends T>> types, Class<? extends C>... classifiers) {
// Checks if a single type is of the given single classifier type and returns the
// result as a Boolean. Cannot be implemented as a BiPredicate since "currying" / partial
......@@ -155,8 +154,9 @@ public class LambdaUtils {
// Applies isOfType for all provided "classifiers" to a single type. The "orElse"
// handles the case that no "classifiers" have been provided.
Predicate<Class<? extends T>> typeClassifierFilter = t -> Stream.of(classifiers)
.map(c -> isOfClassifier.apply(t, c)).reduce((b1, b2) -> b1 && b2).orElse(true);
Predicate<Class<? extends T>> typeClassifierFilter =
t -> Stream.of(classifiers).map(c -> isOfClassifier.apply(t, c))
.reduce((b1, b2) -> b1 && b2).orElse(true);
return types.stream().filter(typeClassifierFilter).collect(toList());
}
......@@ -178,6 +178,24 @@ public class LambdaUtils {
return elements.stream().filter(e -> isInstanceOfAll(e, classifiers)).collect(toList());
}
/**
* Filters the elements of the given {@code inCol} collection based on their type by the given
* {@code typeFilter}.
*
* @param inCol
* Collection to be filtered.
* @param typeFilter
* Type for which the elements shall be filtered.
* @return Collection of elements of the given {@code filterType} (same as {@code outCol}).
*/
public static <S, T, U extends T> Collection<T> filterByType(Collection<S> inCol,
Class<T> typeFilter) {
Collection<T> outCol = new ArrayList<>();
filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast)
.forEach(e -> outCol.add(e));
return outCol;
}
/**
* Filters the elements of the given {@code inCol} collection based on their type by the given
* {@code typeFilter}. The matching elements are added to the given {@code outCol} collection
......@@ -192,7 +210,7 @@ public class LambdaUtils {
* @return Collection of elements of the given {@code filterType} (same as {@code outCol}).
*/
public static <S, T extends S, U extends T> Collection<T> filterByType(Collection<S> inCol,
Collection<T> outCol, Class<U> typeFilter) {
Collection<T> outCol, Class<T> typeFilter) {
filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast)
.forEach(e -> outCol.add(e));
return outCol;
......@@ -251,18 +269,6 @@ public class LambdaUtils {
return getFirst(filterType(inCol, typeFilter));
}
/**
* Applies the given {@code action} to each element of the given {@code inputCollection}.
*
* @param inputCollection
* Collection to traverse.
* @param action
* Action to be applied to each element.
*/
public static void forEach(Collection<?> inputCollection, Consumer<? super Object> action) {
inputCollection.stream().forEach(action);
}
/**
* Filters the given collection by the given filter and returns the first of the remaining
* elements .
......@@ -369,8 +375,8 @@ public class LambdaUtils {
* Supplier for the output {@link Collection}, e.g. TreeSet::new.
* @return Collection of mapping results.
*/
public static <S, T, R extends Stream<S>, U extends Collection<S>> Collection<S>
flatMapInOut(Collection<T> inColl, Function<T, R> mapper, Supplier<U> sup) {
public static <S, T, R extends Stream<S>, U extends Collection<S>> Collection<S> flatMapInOut(
Collection<T> inColl, Function<T, R> mapper, Supplier<U> sup) {
return inColl.parallelStream().flatMap(mapper).collect(Collectors.toCollection(sup));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment