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:org.briljantframework.array.AbstractArray.java

@Override
public Array<T> filter(Predicate<T> predicate) {
    List<T> list = new ArrayList<>();
    for (int i = 0; i < size(); i++) {
        T v = get(i);//from  w  ww.  j  a  v  a2s .com
        if (predicate.test(v)) {
            list.add(v);
        }
    }
    return convertToArray(list);
}

From source file:local.laer.app.knapsack.support.OffsetBinaryMatrix.java

public boolean matchAll(Predicate<Cell<Integer>> callback) {
    SimpleCell cell = new SimpleCell();
    for (int row = 0; row < rows(); row++) {
        for (int col = 0; col < cols(); col++) {

            cell.col = col;/*from   w  w w. j  av  a  2 s.  c om*/
            cell.row = row;
            cell.value = convertToInt(value[row][col]);

            if (!callback.test(cell)) {
                return false;
            }
        }
    }

    return true;
}

From source file:com.netflix.spectator.controllers.MetricsController.java

/**
 * Internal API for encoding a registry that can be encoded as JSON.
 * This is a helper function for the REST endpoint and to test against.
 *//* w w w  . j  ava 2 s .  c o  m*/
Map<String, MetricValues> encodeRegistry(Registry sourceRegistry, Predicate<Measurement> filter) {
    Map<String, MetricValues> metricMap = new HashMap<String, MetricValues>();

    /**
     * Flatten the meter measurements into a map of measurements keyed by
     * the name and mapped to the different tag variants.
     */
    for (Meter meter : sourceRegistry) {
        String kind = knownMeterKinds.computeIfAbsent(meter.id(), k -> meterToKind(sourceRegistry, meter));

        for (Measurement measurement : meter.measure()) {
            if (!filter.test(measurement)) {
                continue;
            }
            if (Double.isNaN(measurement.value())) {
                continue;
            }

            String measurementName = measurement.id().name();
            MetricValues have = metricMap.get(measurementName);
            if (have == null) {
                metricMap.put(measurementName, new MetricValues(kind, measurement));
            } else {
                have.addMeasurement(measurement);
            }
        }
    }
    return metricMap;
}

From source file:org.briljantframework.array.AbstractArray.java

@Override
public BooleanArray where(Predicate<T> predicate) {
    BooleanArray array = getArrayFactory().newBooleanArray(getShape());
    for (int i = 0; i < size(); i++) {
        array.set(i, predicate.test(get(i)));
    }/*ww  w  . ja  v a 2s.c  o m*/
    return array;
}

From source file:at.gridtec.lambda4j.consumer.tri.obj.BiObjBooleanConsumer.java

/**
 * Returns a composed {@link TriConsumer} that first applies the {@code before} functions to its input, and
 * then applies this consumer to the result.
 * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation.
 *
 * @param <A> The type of the argument to the first given function, and of composed consumer
 * @param <B> The type of the argument to the second given function, and of composed consumer
 * @param <C> The type of the argument to the third given predicate, and of composed consumer
 * @param before1 The first function to apply before this consumer is applied
 * @param before2 The second function to apply before this consumer is applied
 * @param before3 The third predicate to apply before this consumer is applied
 * @return A composed {@code TriConsumer} that first applies the {@code before} functions to its input, and then
 * applies this consumer to the result./*from  w  w w .j av  a  2s .  c  om*/
 * @throws NullPointerException If given argument is {@code null}
 * @implSpec The input argument of this method is able to handle every type.
 */
@Nonnull
default <A, B, C> TriConsumer<A, B, C> compose(@Nonnull final Function<? super A, ? extends T> before1,
        @Nonnull final Function<? super B, ? extends U> before2, @Nonnull final Predicate<? super C> before3) {
    Objects.requireNonNull(before1);
    Objects.requireNonNull(before2);
    Objects.requireNonNull(before3);
    return (a, b, c) -> accept(before1.apply(a), before2.apply(b), before3.test(c));
}

From source file:org.fenixedu.academic.domain.ExecutionYear.java

public Collection<ExecutionDegree> getExecutionDegreesMatching(
        java.util.function.Predicate<DegreeType> predicate) {
    return getExecutionDegreesSet().stream()
            .filter(degree -> predicate.test(degree.getDegreeCurricularPlan().getDegreeType()))
            .collect(Collectors.toList());
}

From source file:org.diorite.impl.entity.diorite.EntityImpl.java

@Override
public <T extends Entity> Collection<? extends T> getNearbyEntities(final double x, final double y,
        final double z, final Class<? extends T> type, final Predicate<Entity> predicate) {
    return this.<T>getNearbyEntities(x, y, z, e -> type.isAssignableFrom(e.getClass()) && predicate.test(e));
}

From source file:it.polimi.diceH2020.SPACE4CloudWS.core.CoarseGrainedOptimizer.java

private boolean hillClimbing(SolutionPerJob solPerJob, Technology technology) {
    boolean success = false;
    Pair<Optional<Double>, Long> simulatorResult = dataProcessor.simulateClass(solPerJob);
    Optional<Double> maybeResult = simulatorResult.getLeft();
    if (maybeResult.isPresent()) {
        success = true;//  w ww  . j a va  2  s.c o  m

        PerformanceSolver currentSolver = dataProcessor.getPerformanceSolver();
        Function<Double, Double> fromResult = currentSolver.transformationFromSolverResult(solPerJob,
                technology);
        Predicate<Double> feasibilityCheck = currentSolver.feasibilityCheck(solPerJob, technology);
        Consumer<Double> metricUpdater = currentSolver.metricUpdater(solPerJob, technology);

        final double tolerance = settings.getOptimization().getTolerance();

        BiPredicate<Double, Double> incrementCheck;
        Function<Integer, Integer> updateFunction;
        Predicate<Double> stoppingCondition;
        Predicate<Integer> vmCheck;

        double responseTime = fromResult.apply(maybeResult.get());
        if (feasibilityCheck.test(responseTime)) {
            updateFunction = n -> n - 1;
            stoppingCondition = feasibilityCheck.negate();
            vmCheck = n -> n == 1;
            incrementCheck = (prev, curr) -> false;
        } else {
            updateFunction = n -> n + 1;
            stoppingCondition = feasibilityCheck;
            vmCheck = n -> false;
            incrementCheck = (prev, curr) -> Math.abs((prev - curr) / prev) < tolerance;
        }

        List<Triple<Integer, Optional<Double>, Boolean>> resultsList = alterUntilBreakPoint(solPerJob,
                updateFunction, fromResult, feasibilityCheck, stoppingCondition, incrementCheck, vmCheck);
        Optional<Triple<Integer, Optional<Double>, Boolean>> result = resultsList.parallelStream()
                .filter(t -> t.getRight() && t.getMiddle().isPresent())
                .min(Comparator.comparing(Triple::getLeft));
        result.ifPresent(triple -> triple.getMiddle().ifPresent(output -> {
            int nVM = triple.getLeft();
            switch (technology) {
            case HADOOP:
            case SPARK:
                solPerJob.setThroughput(output);
                break;
            case STORM:
                break;
            default:
                throw new RuntimeException("Unexpected technology");
            }
            solPerJob.updateNumberVM(nVM);
            double metric = fromResult.apply(output);
            metricUpdater.accept(metric);
            logger.info(String.format(
                    "class%s-> MakeFeasible ended, result = %f, other metric = %f, obtained with: %d VMs",
                    solPerJob.getId(), output, metric, nVM));
        }));
    } else {
        logger.info("class" + solPerJob.getId() + "-> MakeFeasible ended with ERROR");
        solPerJob.setFeasible(false);
    }
    return success;
}

From source file:at.gridtec.lambda4j.function.bi.obj.ObjBooleanToByteFunction.java

/**
 * Returns a composed {@link ToByteBiFunction} that first applies the {@code before} functions to its input, and
 * then applies this function to the result.
 * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation.
 *
 * @param <A> The type of the argument to the first given function, and of composed function
 * @param <B> The type of the argument to the second given predicate, and of composed function
 * @param before1 The first function to apply before this function is applied
 * @param before2 The second predicate to apply before this function is applied
 * @return A composed {@code ToByteBiFunction} that first applies the {@code before} functions to its input, and
 * then applies this function to the result.
 * @throws NullPointerException If given argument is {@code null}
 * @implSpec The input argument of this method is able to handle every type.
 *///from www . j ava 2s .  com
@Nonnull
default <A, B> ToByteBiFunction<A, B> compose(@Nonnull final Function<? super A, ? extends T> before1,
        @Nonnull final Predicate<? super B> before2) {
    Objects.requireNonNull(before1);
    Objects.requireNonNull(before2);
    return (a, b) -> applyAsByte(before1.apply(a), before2.test(b));
}

From source file:at.gridtec.lambda4j.function.bi.obj.ObjBooleanToCharFunction.java

/**
 * Returns a composed {@link ToCharBiFunction} that first applies the {@code before} functions to its input, and
 * then applies this function to the result.
 * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation.
 *
 * @param <A> The type of the argument to the first given function, and of composed function
 * @param <B> The type of the argument to the second given predicate, and of composed function
 * @param before1 The first function to apply before this function is applied
 * @param before2 The second predicate to apply before this function is applied
 * @return A composed {@code ToCharBiFunction} that first applies the {@code before} functions to its input, and
 * then applies this function to the result.
 * @throws NullPointerException If given argument is {@code null}
 * @implSpec The input argument of this method is able to handle every type.
 *///from  ww w  .  j a  v  a 2 s. com
@Nonnull
default <A, B> ToCharBiFunction<A, B> compose(@Nonnull final Function<? super A, ? extends T> before1,
        @Nonnull final Predicate<? super B> before2) {
    Objects.requireNonNull(before1);
    Objects.requireNonNull(before2);
    return (a, b) -> applyAsChar(before1.apply(a), before2.test(b));
}