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

aQute.lib.exceptions.PredicateWithException Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package aQute.lib.exceptions;

import static java.util.Objects.requireNonNull;

import java.util.function.BooleanSupplier;
import java.util.function.Predicate;

/**
 * Predicate interface that allows exceptions.
 *
 * @param  the type of the argument
 */
@FunctionalInterface
public interface PredicateWithException {
	boolean test(T t) throws Exception;

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

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

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

	static  Predicate asPredicate(PredicateWithException unchecked) {
		return unchecked.orElseThrow();
	}

	static  Predicate asPredicateOrElse(PredicateWithException unchecked, boolean orElse) {
		return unchecked.orElse(orElse);
	}

	static  Predicate asPredicateOrElseGet(PredicateWithException unchecked, BooleanSupplier orElseGet) {
		return unchecked.orElseGet(orElseGet);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy