com.namics.oss.java.tools.utils.PredicateUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000-2015 Namics AG. All rights reserved.
*/
package com.namics.oss.java.tools.utils;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* PredicateUtil.
*
* @author aschaefer, Namics AG
* @since 31.07.15 15:21 2.0
*/
public class PredicateUtils {
/**
* Helper method to negate a predicate in a functional way.
*
* @param t predicate to negate
* @param Predicate evaluation type
* @return negated predicate
*/
public static Predicate not(Predicate t) {
return t.negate();
}
/**
* Helper method to perform null filtering.
*
* @param Predicate evaluation type
* @return predicate that performs null check.
* @deprecated use {@link java.util.Objects::nonNull}
*/
@Deprecated
public static Predicate notNull() {
return x -> x != null;
}
/**
* Convert provided lambda to predicate instance to call methods on.
* e.g. ...stream().filter(as(test -> test != null).negate())...
*
* @param predicate predicate to be wrapped
* @param the type of the input to the predicate
* @return predicate instance of lambda
*/
public static Predicate as(Predicate predicate) {
return predicate;
}
/**
* Convert provided lambda to consumer instance to call methods on.
*
* @param consumer consumer to be wrapped
* @param the type of the input to the consumer
* @return consumer instance of lambda
*/
public static Consumer as(Consumer consumer) {
return consumer;
}
/**
* Convert provided lambda to supplier instance to call methods on.
*
* @param supplier supplier to be wrapped
* @param the type of the input to the supplier
* @return supplier instance of lambda
*/
public static Supplier as(Supplier supplier) {
return supplier;
}
}