com.pivovarit.function.ThrowingBiPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of throwing-function Show documentation
Show all versions of throwing-function Show documentation
Java 8+ functional interfaces with checked exceptions support
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pivovarit.function;
import com.pivovarit.function.exception.WrappedException;
import java.util.Objects;
import java.util.function.BiPredicate;
import static java.util.Objects.requireNonNull;
/**
* Represents a predicate (boolean-valued function) of two arguments. This is
* the two-arity specialization of {@link ThrowingPredicate}.
* Function may throw a checked exception.
*
* @param the type of the first argument to the predicate
* @param the type of the second argument to the predicate
* @param the type of the thrown checked exception
*
* @author Grzegorz Piwowarek
*/
@FunctionalInterface
public interface ThrowingBiPredicate {
boolean test(T t, U u) throws E;
static BiPredicate unchecked(ThrowingBiPredicate predicate) {
return requireNonNull(predicate).uncheck();
}
/**
* @return BiPredicate instance that rethrows the checked exception using the Sneaky Throws pattern
*/
static BiPredicate sneaky(ThrowingBiPredicate predicate) {
Objects.requireNonNull(predicate);
return (t, u) -> {
try {
return predicate.test(t, u);
} catch (Exception e) {
return SneakyThrowUtil.sneakyThrow(e);
}
};
}
default ThrowingBiPredicate and(final ThrowingBiPredicate super T, ? super U, ? extends E> other) {
requireNonNull(other);
return (arg1, arg2) -> test(arg1, arg2) && other.test(arg1, arg2);
}
default ThrowingBiPredicate or(final ThrowingBiPredicate super T, ? super U, ? extends E> other) {
requireNonNull(other);
return (arg1, arg2) -> test(arg1, arg2) || other.test(arg1, arg2);
}
default ThrowingBiPredicate xor(final ThrowingBiPredicate super T, ? super U, ? extends E> other) {
requireNonNull(other);
return (arg1, arg2) -> test(arg1, arg2) ^ other.test(arg1, arg2);
}
default ThrowingBiPredicate negate() {
return (arg1, arg2) -> !test(arg1, arg2);
}
/**
* @return this ThrowingBiFunction instance as a ThrowingBiFunction
*/
default ThrowingBiFunction asFunction() {
return this::test;
}
/**
* @return a new BiPredicate instance which wraps thrown checked exception instance into a RuntimeException
*/
default BiPredicate uncheck() {
return (arg1, arg2) -> {
try {
return test(arg1, arg2);
} catch (final Exception e) {
throw new WrappedException(e);
}
};
}
}