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

aQute.bnd.exceptions.BiPredicateWithException Maven / Gradle / Ivy

The newest version!
package aQute.bnd.exceptions;

import static java.util.Objects.requireNonNull;

import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;

/**
 * BiPredicate interface that allows exceptions.
 *
 * @param  the type of the first argument
 * @param  the type of the second argument
 */
@FunctionalInterface
public interface BiPredicateWithException {
	boolean test(T t, U u) throws Exception;

	default BiPredicate orElseThrow() {
		return (t, u) -> {
			try {
				return test(t, u);
			} catch (Exception e) {
				throw Exceptions.duck(e);
			}
		};
	}

	default BiPredicate orElse(boolean orElse) {
		return (t, u) -> {
			try {
				return test(t, u);
			} catch (Exception e) {
				return orElse;
			}
		};
	}

	default BiPredicate orElseGet(BooleanSupplier orElseGet) {
		requireNonNull(orElseGet);
		return (t, u) -> {
			try {
				return test(t, u);
			} catch (Exception e) {
				return orElseGet.getAsBoolean();
			}
		};
	}

	static  BiPredicate asBiPredicate(BiPredicateWithException unchecked) {
		return unchecked.orElseThrow();
	}

	static  BiPredicate asBiPredicateOrElse(BiPredicateWithException unchecked, boolean orElse) {
		return unchecked.orElse(orElse);
	}

	static  BiPredicate asBiPredicateOrElseGet(BiPredicateWithException unchecked,
		BooleanSupplier orElseGet) {
		return unchecked.orElseGet(orElseGet);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy