Example usage for java.util.function BiPredicate test

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

Introduction

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

Prototype

boolean test(T t, U u);

Source Link

Document

Evaluates this predicate on the given arguments.

Usage

From source file:org.springframework.kafka.support.SeekUtils.java

/**
 * Seek records to earliest position, optionally skipping the first.
 * @param records the records.// ww  w .j  a  va 2s  .co m
 * @param consumer the consumer.
 * @param exception the exception
 * @param recoverable true if skipping the first record is allowed.
 * @param skipper function to determine whether or not to skip seeking the first.
 * @param logger a {@link Log} for seek errors.
 * @return true if the failed record was skipped.
 */
public static boolean doSeeks(List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer, Exception exception,
        boolean recoverable, BiPredicate<ConsumerRecord<?, ?>, Exception> skipper, Log logger) {
    Map<TopicPartition, Long> partitions = new LinkedHashMap<>();
    AtomicBoolean first = new AtomicBoolean(true);
    AtomicBoolean skipped = new AtomicBoolean();
    records.forEach(record -> {
        if (recoverable && first.get()) {
            skipped.set(skipper.test(record, exception));
            if (skipped.get() && logger.isDebugEnabled()) {
                logger.debug("Skipping seek of: " + record);
            }
        }
        if (!recoverable || !first.get() || !skipped.get()) {
            partitions.computeIfAbsent(new TopicPartition(record.topic(), record.partition()),
                    offset -> record.offset());
        }
        first.set(false);
    });
    boolean tracing = logger.isTraceEnabled();
    partitions.forEach((topicPartition, offset) -> {
        try {
            if (tracing) {
                logger.trace("Seeking: " + topicPartition + " to: " + offset);
            }
            consumer.seek(topicPartition, offset);
        } catch (Exception e) {
            logger.error("Failed to seek " + topicPartition + " to " + offset, e);
        }
    });
    return skipped.get();
}

From source file:com.codelanx.codelanxlib.util.Players.java

/**
 * Determines whether or not a location is harmful if a player was to be
 * located there in the current instant of time (such as a teleport)
 * /*  ww w. j  a  v  a2s . c o m*/
 * @param in The {@link Location} to check
 * @return {@code true} if the location is safe
 */
public static boolean isSafeLocation(final Location in) {
    Location l = in.clone();
    boolean hole = false;
    BiPredicate<Integer, Material> fallDmg = (i, m) -> i > 3 && m.isBlock();
    int count = 0;
    while (l.getBlockY() > 0) {
        l.add(0, -1, 0);
        count++;
        Material type = l.getBlock().getType();
        if (fallDmg.test(count, type)) {
            return false;
        }
        if (Blocks.isHarmful(type)) {
            return false;
        }
        if (type != Material.AIR
                && (type.isBlock() || type == Material.WATER || type == Material.STATIONARY_WATER)) {
            break;
        }
    }
    l = in.clone();
    for (int i = 0; i < 2; i++) {
        Material type = l.getBlock().getType();
        if (Blocks.isHarmful(type) || type.isBlock() || Blocks.isLiquid(type)) {
            return false;
        }
        l.add(0, 1, 0);
    }
    while (l.getBlockY() < 255) {
        Material type = l.getBlock().getType();
        if (Blocks.isDangerousFromAbove(type)) {
            return false;
        } else if (type.isBlock()) {
            break;
        }
        l.add(0, 1, 0);
    }
    return true;
}

From source file:Main.java

private static <E> void updateList(List<E> origList, Collection<? extends E> updateList, boolean add,
        boolean replace, boolean remove, BiPredicate<? super E, ? super E> equalTester,
        BiConsumer<List<E>, Collection<? extends E>> adder) {
    List<E> itemsToRemove = null;
    List<E> itemsToAdd = null;
    for (E update : updateList) {
        boolean origListContainsUpdate = false;
        ListIterator<E> origIter = origList.listIterator();
        while (origIter.hasNext()) {
            E orig = origIter.next();/*w w  w  . j  ava2  s. co  m*/
            if (equalTester.test(orig, update)) {
                origListContainsUpdate = true;
                if (remove) {
                    if (itemsToRemove == null) {
                        itemsToRemove = new ArrayList<>(origList);
                    }
                    itemsToRemove.remove(orig);
                }
                if (replace) {
                    origIter.set(update);
                }
                break;
            }
        }
        if (!origListContainsUpdate && add) {
            if (itemsToAdd == null) {
                itemsToAdd = new ArrayList<>();
            }
            itemsToAdd.add(update);
        }
    }
    if (remove) {
        if (itemsToRemove != null) {
            origList.removeAll(itemsToRemove);
        } else {
            origList.clear();
        }
    }
    if (itemsToAdd != null) {
        adder.accept(origList, itemsToAdd);
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiPredicate2.java

/**
 * Calls the given {@link BiPredicate} with the given arguments and returns its result.
 *
 * @param <T> The type of the first argument to the predicate
 * @param <U> The type of the second argument to the predicate
 * @param predicate The predicate to be called
 * @param t The first argument to the predicate
 * @param u The second argument to the predicate
 * @return The result from the given {@code BiPredicate2}.
 * @throws NullPointerException If given argument is {@code null}
 *//*  www  .  ja  v a 2s . c o m*/
static <T, U> boolean call(@Nonnull final BiPredicate<? super T, ? super U> predicate, T t, U u) {
    Objects.requireNonNull(predicate);
    return predicate.test(t, u);
}

From source file:org.openecomp.core.validation.types.GlobalValidationContext.java

public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
    return fileContextMap.keySet().stream().filter(t -> func.test(t, this)).collect(Collectors.toList());
}

From source file:com.github.rutledgepaulv.qbuilders.visitors.PredicateVisitor.java

private boolean resolveSingleField(Object root, String field, ComparisonNode node,
        BiPredicate<Object, Object> func) {
    if (root == null || node.getField() == null) {
        return func.test(null, node.getValues().iterator().next());
    } else {/*w w  w .  j  a  va  2  s  .c o m*/
        String[] splitField = field.split("\\.", 2);
        Object currentField = getFieldValueFromString(root, splitField[0]);
        if (splitField.length == 1) {
            return func.test(currentField, node.getValues().iterator().next());
        } else {
            return recurseSingle(currentField, splitField[1], node, func);
        }
    }
}

From source file:com.github.rutledgepaulv.qbuilders.visitors.PredicateVisitor.java

private boolean resolveMultiField(Object root, String field, ComparisonNode node,
        BiPredicate<Object, Collection<?>> func) {
    if (root == null || node.getField() == null) {
        return func.test(null, node.getValues());
    } else {//w  w w .j av  a 2  s  . c  om
        String[] splitField = field.split("\\.", 2);
        Object currentField = getFieldValueFromString(root, splitField[0]);
        if (splitField.length == 1) {
            return func.test(currentField, node.getValues());
        } else {
            return recurseMulti(currentField, splitField[1], node, func);
        }
    }
}

From source file:org.apache.bookkeeper.util.collections.SynchronizedHashMultiMap.java

public synchronized int removeIf(BiPredicate<K, V> predicate) {
    int removedSum = map.values().stream().mapToInt(pairs -> {
        int removed = 0;
        // Can't use removeIf because we need the count
        Iterator<Pair<K, V>> iter = pairs.iterator();
        while (iter.hasNext()) {
            Pair<K, V> kv = iter.next();
            if (predicate.test(kv.getLeft(), kv.getRight())) {
                iter.remove();//w  w  w  . ja va  2 s.  co  m
                removed++;
            }
        }
        return removed;
    }).sum();
    map.values().removeIf((s) -> s.isEmpty());
    return removedSum;
}

From source file:fr.landel.utils.commons.builder.EqualsBuilder.java

/**
 * Test if both {@link Object} returned by the {@code getter} function are
 * equal using their {@code equals} method. The {@code getter} method is
 * only applied if both {@link Object} are not {@code null}. The predicate
 * function is only applied if both values are not {@code null}.
 * //from www  . java  2s  . c  om
 * @param lhs
 *            the first object
 * @param rhs
 *            the second object
 * @param getter
 *            the function to apply if both objects are not {@code null}
 *            (required, throws a {@link NullPointerException} if
 *            {@code null})
 * @param predicate
 *            the function to check, if get parameter are equals (if
 *            {@code null}, use the default {@code equals} method)
 * @param <T>
 *            the check object type
 * @param <X>
 *            the sub type
 * @return the current builder
 */
public <T, X> EqualsBuilder append(final T lhs, final T rhs, final Function<T, X> getter,
        final BiPredicate<X, X> predicate) {
    Objects.requireNonNull(getter);

    if (!this.isEquals()) {
        return this;
    }

    if (lhs != null && rhs != null) {
        final X v1 = getter.apply(lhs);
        final X v2 = getter.apply(rhs);
        if (predicate != null) {
            if (v1 != null && v2 != null) {
                this.setEquals(predicate.test(v1, v2));
            } else {
                // check if both values are null
                this.setEquals(v1 == v2);
            }
        } else {
            this.append(v1, v2);
        }
    } else {
        this.setEquals(lhs == null && rhs == null);
    }

    return this;
}