Example usage for java.util.function Predicate test

List of usage examples for java.util.function Predicate test

Introduction

In this page you can find the example usage for java.util.function Predicate test.

Prototype

boolean test(T t);

Source Link

Document

Evaluates this predicate on the given argument.

Usage

From source file:fr.landel.utils.commons.ObjectUtils.java

/**
 * Get the transformed value if not {@code null}
 * // w  ww  .  j ava  2 s .  co m
 * @param predicate
 *            the predicate (cannot be {@code null})
 * @param object
 *            the object to transform
 * @param transformer
 *            the transformer function (cannot be {@code null})
 * @param <T>
 *            the type of the object to check
 * @param <X>
 *            the type of the output
 * @return the transformed value or {@code null}
 */
public static <T, X> X ifPredicate(final Predicate<T> predicate, final T object,
        final Function<T, X> transformer) {
    Objects.requireNonNull(predicate, PREDICATE_ERROR);
    Objects.requireNonNull(transformer, TRANSFORMER_ERROR);

    return predicate.test(object) ? transformer.apply(object) : null;
}

From source file:fr.landel.utils.commons.ObjectUtils.java

/**
 * Get the transformed value if not {@code null}
 * /*from   w w  w .j  a v  a  2 s . c o m*/
 * @param predicate
 *            the predicate (cannot be {@code null})
 * @param object
 *            the object to transform
 * @param transformer
 *            the transformer function (cannot be {@code null})
 * @param <T>
 *            the type of the object to check
 * @param <X>
 *            the type of the output
 * @return an {@link Optional} of the transformed value or
 *         {@link Optional#empty()}
 */
public static <T, X> Optional<X> ifPredicateOptional(final Predicate<T> predicate, final T object,
        final Function<T, X> transformer) {
    Objects.requireNonNull(predicate, PREDICATE_ERROR);
    Objects.requireNonNull(transformer, TRANSFORMER_ERROR);

    return predicate.test(object) ? Optional.ofNullable(transformer.apply(object)) : Optional.empty();
}

From source file:com.vmware.photon.controller.common.xenon.ServiceHostUtils.java

/**
 * Generic wait function./*from ww  w.  j  a va 2 s  .c om*/
 */
public static <T> T waitForState(Supplier<T> supplier, Predicate<T> predicate, long waitIterationSleepMillis,
        long waitIterationCount, Runnable cleanup, String timeoutMessage) throws Throwable {
    for (int i = 0; i < waitIterationCount; i++) {
        T t = supplier.get();
        if (predicate.test(t)) {
            return t;
        }
        Thread.sleep(waitIterationSleepMillis);
    }

    if (cleanup != null) {
        cleanup.run();
    }

    logger.warn(timeoutMessage);
    throw new TimeoutException(timeoutMessage);
}

From source file:at.gridtec.lambda4j.predicate.tri.obj.ObjBiBooleanPredicate.java

/**
 * Creates a {@link ObjBiBooleanPredicate} which uses the {@code first} parameter of this one as argument for the
 * given {@link Predicate}.//from w w w  . j  a va 2 s. com
 *
 * @param <T> The type of the first argument to the predicate
 * @param predicate The predicate which accepts the {@code first} parameter of this one
 * @return Creates a {@code ObjBiBooleanPredicate} which uses the {@code first} parameter of this one as argument
 * for the given {@code Predicate}.
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T> ObjBiBooleanPredicate<T> onlyFirst(@Nonnull final Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate);
    return (t, value1, value2) -> predicate.test(t);
}

From source file:fr.landel.utils.commons.ObjectUtils.java

/**
 * Returns a default value if the object doesn't match the predicate.
 * //from  w w  w.  ja  va2 s . c  o  m
 * <pre>
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; null)          = null
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; "")            = ""
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; "zz")          = "zz"
 * ObjectUtils.defaultIf(Objects::nonNull, "abc", () -&gt; *)            = "abc"
 * ObjectUtils.defaultIf(Objects::nonNull, Boolean.TRUE, () -&gt; *)     = Boolean.TRUE
 * </pre>
 * 
 * @param predicate
 *            the predicate (cannot be {@code null})
 * @param object
 *            the {@code Object} to test, may be {@code null}
 * @param defaultValueSupplier
 *            the default value supplier, cannot be {@code null}, may supply
 *            {@code null}
 * @param <T>
 *            the type of the object
 * @return {@code object} if it matches the predicate, defaultValue
 *         otherwise
 * @throws NullPointerException
 *             if {@code predicate} or {@code defaultValueSupplier} are
 *             {@code null}
 */
public static <T> T defaultIf(final Predicate<T> predicate, final T object,
        final Supplier<? extends T> defaultValueSupplier) {
    Objects.requireNonNull(predicate, PREDICATE_ERROR);
    Objects.requireNonNull(defaultValueSupplier, SUPPLIER_ERROR);

    return predicate.test(object) ? object : defaultValueSupplier.get();
}

From source file:fr.landel.utils.commons.ObjectUtils.java

/**
 * Returns a default value if the object doesn't match the predicate.
 *
 * <pre>/*from  www. j ava  2  s.  c  om*/
 * ObjectUtils.defaultIf(Objects::nonNull, null, null, o -&gt; o.getTitle())              = null
 * ObjectUtils.defaultIf(Objects::nonNull, null, "",  o -&gt; o.getTitle())               = ""
 * ObjectUtils.defaultIf(Objects::nonNull, null, "zz", o -&gt; o.getTitle())              = "zz"
 * ObjectUtils.defaultIf(Objects::nonNull, "abc", "NO_TEXT", o -&gt; o.toUpperCase())     = "ABC"
 * ObjectUtils.defaultIf(Objects::nonNull, "false", Boolean.TRUE, Boolean::parseBoolean)  = Boolean.TRUE
 * </pre>
 * 
 * @param predicate
 *            the predicate (cannot be {@code null})
 * @param object
 *            the {@code Object} to test, may be {@code null}
 * @param defaultObject
 *            the default value
 * @param transformer
 *            the object mapper
 * @param <T>
 *            the type of the object
 * @param <X>
 *            the type of output
 * @return {@code object} if it matches the predicate, defaultValue
 *         otherwise
 * @throws NullPointerException
 *             if {@code predicate} or {@code transformer} are {@code null}
 */
public static <T, X> X defaultIf(final Predicate<T> predicate, final T object, final X defaultObject,
        final Function<T, X> transformer) {
    Objects.requireNonNull(predicate, PREDICATE_ERROR);
    Objects.requireNonNull(transformer, TRANSFORMER_ERROR);

    return predicate.test(object) ? transformer.apply(object) : defaultObject;
}

From source file:at.gridtec.lambda4j.predicate.tri.obj.ObjBiBytePredicate.java

/**
 * Creates a {@link ObjBiBytePredicate} which uses the {@code first} parameter of this one as argument for the given
 * {@link Predicate}./*  w ww.j a v a2 s .c  o  m*/
 *
 * @param <T> The type of the first argument to the predicate
 * @param predicate The predicate which accepts the {@code first} parameter of this one
 * @return Creates a {@code ObjBiBytePredicate} which uses the {@code first} parameter of this one as argument for
 * the given {@code Predicate}.
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T> ObjBiBytePredicate<T> onlyFirst(@Nonnull final Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate);
    return (t, value1, value2) -> predicate.test(t);
}

From source file:at.gridtec.lambda4j.predicate.tri.obj.ObjBiCharPredicate.java

/**
 * Creates a {@link ObjBiCharPredicate} which uses the {@code first} parameter of this one as argument for the given
 * {@link Predicate}./*from   w  w  w .j  a v  a  2 s  . co  m*/
 *
 * @param <T> The type of the first argument to the predicate
 * @param predicate The predicate which accepts the {@code first} parameter of this one
 * @return Creates a {@code ObjBiCharPredicate} which uses the {@code first} parameter of this one as argument for
 * the given {@code Predicate}.
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T> ObjBiCharPredicate<T> onlyFirst(@Nonnull final Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate);
    return (t, value1, value2) -> predicate.test(t);
}

From source file:at.gridtec.lambda4j.predicate.tri.obj.ObjBiFloatPredicate.java

/**
 * Creates a {@link ObjBiFloatPredicate} which uses the {@code first} parameter of this one as argument for the
 * given {@link Predicate}./*  www.  j a va  2  s  .  c  o  m*/
 *
 * @param <T> The type of the first argument to the predicate
 * @param predicate The predicate which accepts the {@code first} parameter of this one
 * @return Creates a {@code ObjBiFloatPredicate} which uses the {@code first} parameter of this one as argument for
 * the given {@code Predicate}.
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T> ObjBiFloatPredicate<T> onlyFirst(@Nonnull final Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate);
    return (t, value1, value2) -> predicate.test(t);
}

From source file:at.gridtec.lambda4j.predicate.tri.obj.ObjBiIntPredicate.java

/**
 * Creates a {@link ObjBiIntPredicate} which uses the {@code first} parameter of this one as argument for the given
 * {@link Predicate}.//w  w w  .j a v  a2  s.  co  m
 *
 * @param <T> The type of the first argument to the predicate
 * @param predicate The predicate which accepts the {@code first} parameter of this one
 * @return Creates a {@code ObjBiIntPredicate} which uses the {@code first} parameter of this one as argument for
 * the given {@code Predicate}.
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T> ObjBiIntPredicate<T> onlyFirst(@Nonnull final Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate);
    return (t, value1, value2) -> predicate.test(t);
}