org.cactoos.scalar.And Java Examples

The following examples show how to use org.cactoos.scalar.And. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapEnvelope.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Indicates whether contents of an other {@code Map} is the same
 * as contents of this one on entry-by-entry basis.
 * @param other Another instance of {@code Map} to compare with
 * @return True if contents are equal false otherwise
 */
private Boolean contentsEqual(final Map<?, ?> other) {
    return new Unchecked<>(
        new And(
            entry -> {
                return new And(
                    () -> other.containsKey(entry.getKey()),
                    () -> new EqualsNullable(
                        () -> other.get(entry.getKey()),
                        entry.getValue()
                    ).value()
                ).value();
            }, this.entrySet()
        )
    ).value();
}
 
Example #2
Source File: RsPrettyXml.java    From takes with MIT License 6 votes vote down vote up
@Override
@SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object that) {
    return new Unchecked<>(
        new Or(
            () -> this == that,
            new And(
                () -> that != null,
                () -> RsPrettyXml.class.equals(that.getClass()),
                () -> {
                    final RsPrettyXml other = (RsPrettyXml) that;
                    return new And(
                        () -> this.origin.equals(other.origin),
                        () -> this.transformed.equals(other.transformed)
                    ).value();
                }
            )
        )
    ).value();
}
 
Example #3
Source File: MapEnvelope.java    From cactoos with MIT License 5 votes vote down vote up
@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
public final boolean equals(final Object other) {
    return new Unchecked<>(
        new Or(
            () -> this == other,
            new And(
                () -> Map.class.isAssignableFrom(other.getClass()),
                () -> this.size() == ((Map<?, ?>) other).size(),
                () -> this.contentsEqual((Map<?, ?>) other)
            )
        )
    ).value();
}
 
Example #4
Source File: TextEnvelope.java    From cactoos with MIT License 5 votes vote down vote up
@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
public final boolean equals(final Object obj) {
    return new Unchecked<>(
        new Or(
            () -> this == obj,
            new And(
                () -> obj instanceof Text,
                () -> new UncheckedText(this)
                    .asString()
                    .equals(new UncheckedText((Text) obj).asString())
            )
        )
    ).value();
}
 
Example #5
Source File: TimedFuncTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test(expected = TimeoutException.class)
public void functionGetsInterrupted() throws Exception {
    final long period = 100L;
    new Timed<Boolean, Boolean>(
        input -> {
            return new And(
                new Endless<>(() -> input)
            ).value();
        },
        period
    ).apply(true);
}
 
Example #6
Source File: ResponseOf.java    From takes with MIT License 5 votes vote down vote up
@Override
@SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object that) {
    return new Unchecked<>(
        new Or(
            () -> this == that,
            new And(
                () -> that != null,
                () -> ResponseOf.class.equals(that.getClass()),
                () -> {
                    final ResponseOf other = (ResponseOf) that;
                    return new And(
                        () -> {
                            final Iterator<String> iter = other.head()
                                .iterator();
                            return new And(
                                (String hdr) -> hdr.equals(iter.next()),
                                this.head()
                            ).value();
                        },
                        () -> new Equality<>(
                            new BytesOf(this.body()),
                            new BytesOf(other.body())
                        ).value() == 0
                    ).value();
                }
            )
        )
    ).value();
}
 
Example #7
Source File: RequestOf.java    From takes with MIT License 5 votes vote down vote up
@Override
@SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object that) {
    return new Unchecked<>(
        new Or(
            () -> this == that,
            new And(
                () -> that != null,
                () -> RequestOf.class.equals(that.getClass()),
                () -> {
                    final RequestOf other = (RequestOf) that;
                    return new And(
                        () -> {
                            final Iterator<String> iter = other.head()
                                .iterator();
                            return new And(
                                (String hdr) -> hdr.equals(iter.next()),
                                this.head()
                            ).value();
                        },
                        () -> new Equality<>(
                            new BytesOf(this.body()),
                            new BytesOf(other.body())
                        ).value() == 0
                    ).value();
                }
            )
        )
    ).value();
}
 
Example #8
Source File: ForEach.java    From cactoos with MIT License 4 votes vote down vote up
@Override
public void exec(final Iterable<X> input) throws Exception {
    new And(
        this.func, input
    ).value();
}
 
Example #9
Source File: Matrix.java    From jpeek with MIT License 4 votes vote down vote up
@Override
public Iterator<Directive> iterator() {
    final SortedMap<String, Map<String, String>> matrix = new TreeMap<>();
    new Unchecked<>(
        new And(
            path -> {
                new And(
                    node -> {
                        final String name = String.format(
                            "%s.%s",
                            node.xpath("../../package/@id").get(0),
                            node.xpath("@id").get(0)
                        );
                        matrix.putIfAbsent(name, new TreeMap<>());
                        matrix.get(name).put(
                            node.xpath("/metric/title/text()").get(0),
                            node.xpath("@color").get(0)
                        );
                    },
                    new XMLDocument(path.toFile()).nodes("//class")
                ).value();
            },
            new Filtered<>(
                path -> path.getFileName().toString().matches(
                    "^[A-Z].+\\.xml$"
                ),
                new Directory(this.output)
            )
        )
    ).value();
    return new Directives()
        .add("matrix")
        .append(new Header())
        .append(
            () -> new Directives()
                .attr(
                    "xmlns:xsi",
                    "http://www.w3.org/2001/XMLSchema-instance"
                )
                .attr(
                    "xsi:noNamespaceSchemaLocation",
                    "xsd/matrix.xsd"
                )
                .iterator())
        .add("classes")
        .append(
            new Joined<Directive>(
                new Mapped<>(
                    ent -> new Directives().add("class").append(
                        new Joined<Directive>(
                            new Mapped<>(
                                mtd -> new Directives()
                                    .add("metric")
                                    .attr("name", mtd.getKey())
                                    .attr("color", mtd.getValue())
                                    .attr(
                                        "rank",
                                        Matrix.rank(mtd.getValue())
                                    )
                                    .up(),
                                ent.getValue().entrySet()
                            )
                        )
                    ).attr("id", ent.getKey()).up(),
                    matrix.entrySet()
                )
            )
        )
        .iterator();
}
 
Example #10
Source File: FakeBase.java    From jpeek with MIT License 4 votes vote down vote up
@Override
public Iterable<Path> files() throws IOException {
    final Path temp = Files.createTempDirectory("jpeek");
    final Iterable<String> sources = new Mapped<>(
        cls -> String.format("%s.java", cls),
        this.classes
    );
    new IoChecked<>(
        new And(
            java -> {
                new LengthOf(
                    new TeeInput(
                        new ResourceOf(
                            String.format("org/jpeek/samples/%s", java)
                        ),
                        temp.resolve(java)
                    )
                ).value();
            },
            sources
        )
    ).value();
    if (sources.iterator().hasNext()) {
        final int exit;
        try {
            exit = new ProcessBuilder()
                .redirectOutput(ProcessBuilder.Redirect.INHERIT)
                .redirectInput(ProcessBuilder.Redirect.INHERIT)
                .redirectError(ProcessBuilder.Redirect.INHERIT)
                .directory(temp.toFile())
                .command(
                    new ListOf<>(
                        new Joined<String>(
                            new ListOf<>("javac"),
                            sources
                        )
                    )
                )
                .start()
                .waitFor();
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException(ex);
        }
        if (exit != 0) {
            throw new IllegalStateException(
                String.format("javac failed with exit code %d", exit)
            );
        }
    }
    return new DefaultBase(temp).files();
}
 
Example #11
Source File: Program.java    From eo with MIT License 4 votes vote down vote up
/**
 * Compile it to Java and save.
 *
 * @throws IOException If fails
 */
public void compile() throws IOException {
    final String[] lines = new TextOf(this.input).asString().split("\n");
    final ANTLRErrorListener errors = new BaseErrorListener() {
        // @checkstyle ParameterNumberCheck (10 lines)
        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer,
            final Object symbol, final int line,
            final int position, final String msg,
            final RecognitionException error) {
            throw new CompileException(
                String.format(
                    "[%d:%d] %s: \"%s\"",
                    line, position, msg, lines[line - 1]
                ),
                error
            );
        }
    };
    final org.eolang.compiler.ProgramLexer lexer =
        new org.eolang.compiler.ProgramLexer(
            CharStreams.fromStream(this.input.stream())
        );
    lexer.removeErrorListeners();
    lexer.addErrorListener(errors);
    final org.eolang.compiler.ProgramParser parser =
        new org.eolang.compiler.ProgramParser(
            new CommonTokenStream(lexer)
        );
    parser.removeErrorListeners();
    parser.addErrorListener(errors);
    final Tree tree = parser.program().ret;
    new IoCheckedScalar<>(
        new And(
            path -> {
                new LengthOf(
                    new TeeInput(
                        path.getValue(),
                        this.target.apply(path.getKey())
                    )
                ).value();
            },
            tree.java().entrySet()
        )
    ).value();
}
 
Example #12
Source File: CactoosCollectionUtils.java    From tutorials with MIT License 4 votes vote down vote up
public void iterateCollection(List<String> strings) throws Exception {
	new And((String input) -> LOGGER.info(new FormattedText("%s\n", input).asString()), strings).value();
}