
com.github.sheigutn.pushbullet.util.FunctionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pushbullet-java-8 Show documentation
Show all versions of pushbullet-java-8 Show documentation
Pushbullet Library for Java 8
The newest version!
package com.github.sheigutn.pushbullet.util;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
public class FunctionUtil {
/**
* Returns an object of a list where at least one result of the functions matches with the value
* The U param of the functions is given by another function
* Yeah, I know, it's complicated
* @param objects The object list
* @param mapperFunction The function
* @param value The value to match
* @param functions The functions array
* @param The object type
* @param The mapping type
* @param The result type of the array's functions
* @return An object of the list or null
*/
@SafeVarargs
public static T getFirstWithFunctionWithCondition(List objects, Function mapperFunction, V value, Function... functions) {
Optional first = objects.stream()
.filter(object -> {
U methodVal = mapperFunction.apply(object);
boolean has = false;
for (Function super U, V> function : functions) {
has = has || value.equals(function.apply(methodVal));
}
return has;
}).findFirst();
return first.isPresent() ? first.get() : null;
}
/**
* Returns an object of a list where at least one result of the functions matches with the value
* @param objects The object list
* @param value The value to match
* @param functions The functions array
* @param The object type
* @param The result type of the array's functions
* @return An object of the list or null
*/
@SafeVarargs
public static T getFirstWithCondition(List objects, U value, Function super T, U>... functions) {
Optional first = objects.stream()
.filter(object -> {
boolean has = false;
for (Function super T, U> function : functions) {
has = has || value.equals(function.apply(object));
}
return has;
}).findFirst();
return first.isPresent() ? first.get() : null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy