
ru.progrm_jarvis.javacommons.util.function.FloatPredicate Maven / Gradle / Ivy
package ru.progrm_jarvis.javacommons.util.function;
import lombok.NonNull;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.function.Predicate;
/**
* Represents a predicate (boolean-valued function) of one argument.
* This is the primitive type specialization of {@link Predicate} for {@code float}.
*
* @see Predicate non-primitive generic equivalent
*/
@FunctionalInterface
public interface FloatPredicate extends Predicate<@NotNull Float> {
/**
* Evaluates this predicate on the given argument.
*
* @param value the input argument
* @return {@code true} if the input argument matches the predicate or {@code false} otherwise
*/
boolean testAsFloat(float value);
@Override
@Contract("null -> fail")
default boolean test(final @NotNull Float value) {
return testAsFloat(value.floatValue());
}
/**
* 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 {@code null}
*/
@Contract(value = "null -> fail; _ -> _", pure = true)
default @NotNull FloatPredicate and(final @NonNull FloatPredicate other) {
return value -> testAsFloat(value) && other.testAsFloat(value);
}
@Override
@Contract(value = "null -> fail; _ -> _", pure = true)
default @NotNull FloatPredicate and(final @NonNull Predicate< @NotNull ? super Float> other) {
return value -> testAsFloat(value) && other.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 {@code null}
*/
@Contract(value = "null -> fail; _ -> _", pure = true)
default @NotNull FloatPredicate or(final @NonNull FloatPredicate other) {
return value -> testAsFloat(value) || other.testAsFloat(value);
}
@Override
@Contract(value = "null -> fail; _ -> _", pure = true)
default @NotNull FloatPredicate or(final @NonNull Predicate< @NotNull ? super Float> other) {
return value -> testAsFloat(value) || other.test(value);
}
@Override
@Contract(value = "-> _", pure = true)
default @NotNull FloatPredicate negate() {
return value -> !testAsFloat(value);
}
/**
* Returns a predicate that tests if two arguments are equal.
*
* @param value value with which to compare the tested one
* @return a predicate that tests if two arguments are equal
*/
@Contract(value = "_ -> _", pure = true)
static @NotNull FloatPredicate isEqual(final float value) {
return tested -> tested == value;
}
/**
* Returns a predicate that tests if two arguments are not equal.
*
* @param value value with which to compare the tested one
* @return a predicate that tests if two arguments are not equal
*/
@Contract(value = "_ -> _", pure = true)
static @NotNull FloatPredicate isNotEqual(final float value) {
return tested -> tested != value;
}
/**
* Creates a predicate which is always {@code true}.
*
* @return predicate which is always {@code true}
*/
@Contract(value = "-> _", pure = true)
static @NotNull DoublePredicate alwaysTrue() {
return value -> true;
}
/**
* Creates a predicate which is always {@code false}.
*
* @return predicate which is always {@code false}
*/
@Contract(value = "-> _", pure = true)
static @NotNull DoublePredicate alwaysFalse() {
return value -> false;
}
}