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:de.vandermeer.skb.interfaces.transformers.ClusterElementTransformer.java

/**
 * Converts the input `iterator` to a collection applying a predicate and a transformation for each input element.
 * @param <T1> the from/source of the transformer (also the type for the `iterator`)
 * @param <T2> the to/target of the transformer and the type of objects in the return collection
 * @param <T3> any type that extends T1 to no limit conversion to a single type
 * @param <S> the type of collection that should be returned
 * @param input `iterator` of input elements
 * @param transformer a transformer to apply for each input element before copying to the output
 * @param predicate a predicate to apply before transformation and copy of each input element (ignored if null)
 * @param strategy a strategy determining the type of output collection
 * @return an empty collection of type T2 or a collection of type T2 with transformed objects from the input collection
 * @throws NullPointerException if `input`, `transformer`, or `strategy` was null
 *///from ww  w.  j  a v a2 s  .  c om
default <T1, T2, T3 extends T1, S extends Collection<T2>> S transform(Iterator<T3> input,
        Transformer<T1, T2> transformer, Predicate<T3> predicate, IsCollectionStrategy<S, T2> strategy) {
    Validate.notNull(input);
    Validate.notNull(transformer);
    Validate.notNull(strategy);

    S ret = strategy.get();
    while (input.hasNext()) {
        T3 t3 = input.next();
        if (predicate != null && predicate.test(t3)) {
            ret.add(transformer.transform(t3));
        } else if (predicate == null) {
            ret.add(transformer.transform(t3));
        }
    }
    return ret;
}

From source file:de.codesourcery.luaparser.LuaToJSON.java

private boolean visitPreOrder(ParseTree node, Predicate<ParseTree> visitor) {
    if (!visitor.test(node)) {
        return false;
    }/*  ww  w .j  a v a2s  .  c o m*/
    final int childCount = node.getChildCount();
    for (int i = 0; i < childCount; i++) {
        if (!visitPreOrder(node.getChild(i), visitor)) {
            return false;
        }
    }
    return true;
}

From source file:org.opensingular.form.util.diff.DiffInfo.java

/**
 * Varre a rvore de comparaes em profundidade, retornando, se houver, o primeiro diff que atende ao predicado
 * informado.//from   w w w . j av a2s. c  om
 */
final Optional<DiffInfo> findFirst(Predicate<DiffInfo> predicate) {
    if (predicate.test(this)) {
        return Optional.of(this);
    } else if (children != null) {
        for (DiffInfo info : children) {
            Optional<DiffInfo> result = info.findFirst(predicate);
            if (result.isPresent()) {
                return result;
            }
        }
    }
    return Optional.empty();
}

From source file:org.kitodo.production.services.image.ImageGenerator.java

/**
 * Determines the folders in which a derivative must be created. Because the
 * ModuleLoader does not work when invoked from a parallelStream(), we use a
 * classic loop here./* www . j  a v  a  2s .c o m*/
 *
 * @param canonical
 *            canonical part of the file name, to determine the file names
 *            in the destination folder
 * @return the images to be generated
 */
public List<Subfolder> determineFoldersThatNeedDerivatives(String canonical) {
    List<Subfolder> result = new ArrayList<>(outputs.size());
    Predicate<? super Subfolder> requiresGeneration = mode.getFilter(canonical);
    for (Subfolder folder : outputs) {
        if (requiresGeneration.test(folder)) {
            result.add(folder);
        }
    }
    return result;
}

From source file:blusunrize.immersiveengineering.api.ApiUtils.java

private static boolean handlePos(Vec3d pos, BlockPos posB, HashMap<BlockPos, Vec3d> halfScanned,
        HashSet<BlockPos> done, Predicate<Triple<BlockPos, Vec3d, Vec3d>> shouldStop,
        HashSet<Triple<BlockPos, Vec3d, Vec3d>> near) {
    final double DELTA_NEAR = .3;
    if (!done.contains(posB)) {
        if (halfScanned.containsKey(posB) && !pos.equals(halfScanned.get(posB))) {
            Triple<BlockPos, Vec3d, Vec3d> added = new ImmutableTriple<>(posB, halfScanned.get(posB), pos);
            boolean stop = shouldStop.test(added);
            done.add(posB);//from   www .j  a v  a  2  s.  c  o m
            halfScanned.remove(posB);
            near.removeIf((t) -> t.getLeft().equals(posB));
            if (stop)
                return true;
            for (int i = 0; i < 3; i++) {
                double coord = getDim(pos, i);
                double diff = coord - Math.floor(coord);
                if (diff < DELTA_NEAR)
                    near.add(
                            new ImmutableTriple<>(offsetDim(posB, i, -1), added.getMiddle(), added.getRight()));
                diff = Math.ceil(coord) - coord;
                if (diff < DELTA_NEAR)
                    near.add(new ImmutableTriple<>(offsetDim(posB, i, 1), added.getMiddle(), added.getRight()));
            }
        } else {
            halfScanned.put(posB, pos);
        }
    }
    return false;
}

From source file:com.diffplug.gradle.ZipMisc.java

/**
 * Modifies only the specified entries in a zip file. 
 *
 * @param input       a source from a zip file
 * @param output      an output to a zip file
 * @param toModify      a map from path to an input stream for the entries you'd like to change
 * @param toOmit      a set of entries you'd like to leave out of the zip
 * @throws IOException/*from  w ww  .  ja v  a  2 s.c  om*/
 */
public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify,
        Predicate<String> toOmit) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream());
            ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) {
        while (true) {
            // read the next entry
            ZipEntry entry = zipInput.getNextEntry();
            if (entry == null) {
                break;
            }

            Function<byte[], byte[]> replacement = toModify.get(entry.getName());
            if (replacement != null) {
                byte[] clean = ByteStreams.toByteArray(zipInput);
                byte[] modified = replacement.apply(clean);
                // if it's the entry being modified, enter the modified stuff
                try (InputStream replacementStream = new ByteArrayInputStream(modified)) {
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    newEntry.setMethod(entry.getMethod());
                    newEntry.setTime(entry.getTime());

                    zipOutput.putNextEntry(newEntry);
                    copy(replacementStream, zipOutput);
                }
            } else if (!toOmit.test(entry.getName())) {
                // if it isn't being modified, just copy the file stream straight-up
                ZipEntry newEntry = new ZipEntry(entry);
                newEntry.setCompressedSize(-1);
                zipOutput.putNextEntry(newEntry);
                copy(zipInput, zipOutput);
            }

            // close the entries
            zipInput.closeEntry();
            zipOutput.closeEntry();
        }
    }
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCliTest.java

/**
 * {@code --include} multiple patterns./*from   w  ww . j  a va  2  s  .c  om*/
 * @throws Exception if failed
 */
@Test
public void parse_include() throws Exception {
    Configuration conf = BatchCompilerCli.parse(strings(new Object[] { "--explore", deployer.newFolder(),
            "--output", deployer.newFolder(), "--include", "*.String*,*Buffer", }));
    Predicate<? super Class<?>> p = predicate(conf.sourcePredicate);
    assertThat(p.test(String.class), is(true));
    assertThat(p.test(StringBuilder.class), is(true));
    assertThat(p.test(ByteBuffer.class), is(true));
    assertThat(p.test(Integer.class), is(false));
    assertThat(p.test(ByteChannel.class), is(false));
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCliTest.java

/**
 * {@code --exclude} multiple patterns.// ww  w.  j ava 2 s.  c om
 * @throws Exception if failed
 */
@Test
public void parse_exclude() throws Exception {
    Configuration conf = BatchCompilerCli.parse(strings(new Object[] { "--explore", deployer.newFolder(),
            "--output", deployer.newFolder(), "--exclude", "*.String*,*Buffer", }));
    Predicate<? super Class<?>> p = predicate(conf.sourcePredicate);
    assertThat(p.test(String.class), is(false));
    assertThat(p.test(StringBuilder.class), is(false));
    assertThat(p.test(ByteBuffer.class), is(false));
    assertThat(p.test(Integer.class), is(true));
    assertThat(p.test(ByteChannel.class), is(true));
}

From source file:org.fenixedu.academic.domain.phd.PhdIndividualProgramProcess.java

private static <T> List<T> filter(Collection<T> collection, Predicate<T> predicate) {
    final List<T> result = new ArrayList<T>();
    for (final T each : collection) {
        if (predicate.test(each)) {
            result.add(each);//w w  w .  j  av  a2 s . c  om
        }
    }
    return result;
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCliTest.java

/**
 * full args./*from  w w w .j  av  a 2 s  .c o m*/
 * @throws Exception if failed
 */
@Test
public void parse_full() throws Exception {
    File input = deployer.newFolder();
    File output = deployer.newFolder();
    File external1 = deployer.newFolder();
    File external2 = deployer.newFolder();
    File embed1 = deployer.newFolder();
    File embed2 = deployer.newFolder();
    File attach1 = deployer.newFolder();
    File attach2 = deployer.newFolder();
    Configuration conf = BatchCompilerCli.parse(strings(new Object[] { "--explore", input, "--output", output,
            "--classAnalyzer", classes(DummyClassAnalyzer.class), "--batchCompiler",
            classes(DummyBatchCompiler.class), "--external", files(external1, external2), "--embed",
            files(embed1, embed2), "--attach", files(attach1, attach2), "--include", "*Buffer", "--exclude",
            "java.lang.*", "--dataModelProcessors", classes(DummyDataModelProcessor.class),
            "--externalPortProcessors", classes(DummyExternalPortProcessor.class), "--batchProcessors",
            classes(DummyBatchProcessor.class), "--jobflowProcessors", classes(DummyJobflowProcessor.class),
            "--participants", classes(DummyCompilerParticipant.class), "--runtimeWorkingDirectory",
            "testRuntimeWorkingDirectory", "--failOnError", true, "--batchIdPrefix", "prefix.", "-P", "a=b",
            "-property", "c=d", }));
    assertThat(conf.classAnalyzer, containsInAnyOrder(classOf(DummyClassAnalyzer.class)));
    assertThat(conf.batchCompiler, containsInAnyOrder(classOf(DummyBatchCompiler.class)));
    assertThat(conf.output, contains(output));
    assertThat(conf.explore, contains(input));
    assertThat(conf.external, containsInAnyOrder(external1, external2));
    assertThat(conf.embed, containsInAnyOrder(embed1, embed2));
    assertThat(conf.attach, containsInAnyOrder(attach1, attach2));
    assertThat(conf.dataModelProcessors, containsInAnyOrder(classOf(DummyDataModelProcessor.class)));
    assertThat(conf.externalPortProcessors, containsInAnyOrder(classOf(DummyExternalPortProcessor.class)));
    assertThat(conf.batchProcessors, containsInAnyOrder(classOf(DummyBatchProcessor.class)));
    assertThat(conf.jobflowProcessors, containsInAnyOrder(classOf(DummyJobflowProcessor.class)));
    assertThat(conf.compilerParticipants, containsInAnyOrder(classOf(DummyCompilerParticipant.class)));
    assertThat(conf.sourcePredicate, not(isEmpty()));
    assertThat(conf.runtimeWorkingDirectory, contains("testRuntimeWorkingDirectory"));
    assertThat(conf.properties.entrySet(), hasSize(2));
    assertThat(conf.failOnError, contains(true));
    assertThat(conf.batchIdPrefix, contains("prefix."));

    Predicate<? super Class<?>> p = predicate(conf.sourcePredicate);
    assertThat(p.test(ByteBuffer.class), is(true));
    assertThat(p.test(ByteChannel.class), is(false));
    assertThat(p.test(StringBuffer.class), is(false));

    assertThat(conf.properties, hasEntry("a", "b"));
    assertThat(conf.properties, hasEntry("c", "d"));
}