All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sheigutn.pushbullet.util.FunctionUtil Maven / Gradle / Ivy

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 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... functions) { Optional first = objects.stream() .filter(object -> { boolean has = false; for (Function function : functions) { has = has || value.equals(function.apply(object)); } return has; }).findFirst(); return first.isPresent() ? first.get() : null; } }