ratpack.func.Predicate Maven / Gradle / Ivy
/*
* Copyright 2014 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 ratpack.func;
import ratpack.util.ExceptionUtils;
/**
* A function that returns {@code true} or {@code false} for a value.
*
* This type serves the same purpose as the JDK's {@link java.util.function.Predicate}, but allows throwing checked exceptions.
* It contains methods for bridging to and from the JDK type.
*
* @param the type of object “tested” by the predicate
*/
@FunctionalInterface
public interface Predicate {
/**
* Tests the given value.
*
* @param t the value to “test”
* @return {@code true} if the predicate applied, otherwise {@code false}
* @throws Exception any
*/
boolean apply(T t) throws Exception;
/**
* Creates a JDK {@link java.util.function.Predicate} from this predicate.
*
* Any exceptions thrown by {@code this} action will be unchecked via {@link ExceptionUtils#uncheck(Throwable)} and rethrown.
*
* @return this function as a JDK style predicate.
*/
default java.util.function.Predicate toPredicate() {
return t -> {
try {
return apply(t);
} catch (Exception e) {
throw ExceptionUtils.uncheck(e);
}
};
}
/**
* Creates a Guava {@link com.google.common.base.Predicate} from this predicate.
*
* Any exceptions thrown by {@code this} action will be unchecked via {@link ExceptionUtils#uncheck(Throwable)} and rethrown.
*
* @return this function as a Guava style predicate.
*/
default com.google.common.base.Predicate toGuavaPredicate() {
return t -> {
try {
return apply(t);
} catch (Exception e) {
throw ExceptionUtils.uncheck(e);
}
};
}
/**
* Creates a predicate from a JDK predicate.
*
* @param predicate the JDK predicate
* @param the type of object this predicate tests
* @return the given JDK predicate as a predicate
*/
static Predicate from(java.util.function.Predicate predicate) {
return predicate::test;
}
/**
* Creates a predicate from a Guava predicate.
*
* @param predicate the Guava predicate
* @param the type of object this predicate tests
* @return the given Guava predicate as a predicate
*/
static Predicate fromGuava(com.google.common.base.Predicate predicate) {
return predicate::apply;
}
}