
org.xblackcat.sjpu.util.function.IntPredicateEx Maven / Gradle / Ivy
Show all versions of sjpu-utils Show documentation
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.xblackcat.sjpu.util.function;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.Supplier;
/**
* Represents a predicate (boolean-valued function) of one {@code int}-valued
* argument. This is the {@code int}-consuming primitive type specialization of
* {@link PredicateEx}.
*
*
This is a functional interface
* whose functional method is {@link #test(int)}.
*
* @param the type of exception could be thrown while performing operation
* @see PredicateEx
* @since 1.8
*/
@FunctionalInterface
public interface IntPredicateEx {
/**
* Evaluates this predicate on the given argument.
*
* @param value the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(int value) throws E;
/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
*
Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default IntPredicateEx and(IntPredicateEx other) {
Objects.requireNonNull(other);
return (value) -> test(value) && other.test(value);
}
/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
default IntPredicateEx negate() {
return (value) -> !test(value);
}
/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
*
Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default IntPredicateEx or(IntPredicateEx other) {
Objects.requireNonNull(other);
return (value) -> test(value) || other.test(value);
}
default BooleanSupplierEx fix(int value) {
return () -> test(value);
}
default IntPredicateEx cover(String exceptionText, BiFunction cover) {
return cover(() -> exceptionText, cover);
}
default IntPredicateEx cover(BiFunction cover) {
return cover(Throwable::getMessage, cover);
}
default IntPredicateEx cover(Supplier text, BiFunction cover) {
return cover(e -> text.get(), cover);
}
default IntPredicateEx cover(Function text, BiFunction cover) {
return t -> {
try {
return test(t);
} catch (Throwable e) {
throw cover.apply(text.apply(e), e);
}
};
}
default IntPredicate unchecked(String exceptionText, BiFunction cover) {
return unchecked(() -> exceptionText, cover);
}
default IntPredicate unchecked() {
return unchecked(CoveringException::new);
}
default IntPredicate unchecked(BiFunction cover) {
return unchecked(Throwable::getMessage, cover);
}
default IntPredicate unchecked(Supplier text, BiFunction cover) {
return unchecked(e -> text.get(), cover);
}
default IntPredicate unchecked(
Function text,
BiFunction cover
) {
return t -> {
try {
return test(t);
} catch (Throwable e) {
throw cover.apply(text.apply(e), e);
}
};
}
}