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

ratpack.func.Predicate Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * 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.Exceptions;
import ratpack.util.Types;

/**
 * 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 ratpack.util.Exceptions#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 Exceptions.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 ratpack.util.Exceptions#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 Exceptions.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; } /** * A predicate that always returns {@code true}, regardless of the input object. * * @since 1.1 */ Predicate TRUE = o -> true; /** * A predicate that always returns {@code true}, regardless of the input object. * * @param the type of input object * @return a predicate that always returns {@code true} * @since 1.1 */ static Predicate alwaysTrue() { return Types.cast(TRUE); } /** * A predicate that always returns {@code false}, regardless of the input object. * * @since 1.1 */ Predicate FALSE = o -> false; /** * A predicate that always returns {@code false}, regardless of the input object. * * @param the type of input object * @return a predicate that always returns {@code false} * @since 1.1 */ static Predicate alwaysFalse() { return Types.cast(FALSE); } }