org.cactoos.iterable.Filtered Java Examples

The following examples show how to use org.cactoos.iterable.Filtered. 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: FormParams.java    From verano-http with MIT License 6 votes vote down vote up
@Override
public final String asString() {
    return new UncheckedText(
        new JoinedText(
            "&",
            new Mapped<>(
                input -> String.format(
                    "%s=%s", input.key(),
                    URLEncoder.encode(
                        input.value(),
                        "UTF-8"
                    )
                ),
                new Mapped<>(
                    in -> new KvpOf(in.key().substring(2), in.value()),
                    new Filtered<>(
                        input -> input.key().startsWith("f."),
                        this.dict
                    )
                )
            )
        )
    ).asString();
}
 
Example #2
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withoutIterable() throws Exception {
    new Assertion<>(
        "Must work without iterable",
        new LengthOf(
            new Filtered<>(
                input -> input.endsWith("12"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        input -> input.concat("2")
                    ),
                    new IterableOf<>("public", "final", "class")
                )
            )
        ).intValue(),
        new IsEqual<>(3)
    ).affirm();
}
 
Example #3
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withIterable() throws Exception {
    new Assertion<>(
        "Must work with iterable",
        new LengthOf(
            new Filtered<>(
                input -> !input.startsWith("st"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        new IterableOf<Func<String, String>>(
                            input -> input.concat("2"),
                            input -> input.replaceAll("a", "b")
                        ),
                        String::trim
                    ),
                    new IterableOf<>("private", "static", "String")
                )
            )
        ).intValue(),
        new IsEqual<>(2)
    ).affirm();
}
 
Example #4
Source File: RsWithStatus.java    From takes with MIT License 6 votes vote down vote up
/**
 * Make head.
 * @param origin Original response
 * @param status Status
 * @param reason Reason
 * @return Head
 * @throws IOException If fails
 */
private static Iterable<String> head(final Response origin,
    final int status, final CharSequence reason) throws IOException {
    // @checkstyle MagicNumber (1 line)
    if (status < 100 || status > 999) {
        throw new IllegalArgumentException(
            String.format(
                // @checkstyle LineLength (1 line)
                "according to RFC 7230 HTTP status code must have three digits: %d",
                status
            )
        );
    }
    return new Joined<>(
        new FormattedText(
            "HTTP/1.1 %d %s", status, reason
        ).asString(),
        new Filtered<>(
            item -> new Not(
                new StartsWith(item, "HTTP/")
            ).value(),
            origin.head()
        )
    );
}
 
Example #5
Source File: RsWithoutHeader.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param res Original response
 * @param name Header name
 */
public RsWithoutHeader(final Response res, final CharSequence name) {
    super(
        new ResponseOf(
            () -> new Filtered<>(
                header -> new Not(
                    new StartsWith(
                        new Lowered(header),
                        new FormattedText(
                            "%s:",
                            new Lowered(name.toString())
                        )
                    )
                ).value(),
                res.head()
            ),
            res::body
        )
    );
}
 
Example #6
Source File: RqWithoutHeader.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param req Original request
 * @param name Header name
 */
public RqWithoutHeader(final Request req, final CharSequence name) {
    super(
        // @checkstyle AnonInnerLengthCheck (50 lines)
        new RequestOf(
            () -> new Filtered<>(
                header -> new Not(
                    new StartsWith(
                        new Lowered(header),
                        new FormattedText(
                            "%s:",
                            new Lowered(name.toString())
                        )
                    )
                ).value(),
                req.head()
            ),
            req::body
        )
    );
}
 
Example #7
Source File: FbStatusTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FbStatus can react to Condition.
 * @throws Exception If some problem inside
 */
@Test
public void reactsToCondition() throws Exception {
    final RqFallback req = new RqFallback.Fake(
        HttpURLConnection.HTTP_MOVED_PERM
    );
    MatcherAssert.assertThat(
        new RsPrint(
            new FbStatus(
                new Filtered<>(
                    status -> {
                        return status == HttpURLConnection.HTTP_MOVED_PERM
                            || status == HttpURLConnection.HTTP_MOVED_TEMP;
                    },
                    new ListOf<>(
                        HttpURLConnection.HTTP_MOVED_PERM,
                        HttpURLConnection.HTTP_MOVED_TEMP
                    )
                ),
                new FbFixed(new RsText("response text"))
            ).route(req).get()
        ).printBody(),
        Matchers.startsWith("response")
    );
}
 
Example #8
Source File: QueryParams.java    From verano-http with MIT License 5 votes vote down vote up
@Override
public final String asString() {
    return QueryParams.queryString(
        new Mapped<>(
            in -> new KvpOf(in.key().substring(2), in.value()),
            new Filtered<>(
                input -> input.key().startsWith("q."),
                this.response
            )
        )
    );
}
 
Example #9
Source File: Headers.java    From verano-http with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param dict Dictionary
 */
public Of(final Dict dict) {
    super(() -> new Mapped<>(
        in -> new KvpOf(in.key().substring(2), in.value()),
        new Filtered<>(
            input -> input.key().startsWith("h."),
            dict
        )
    ));
}
 
Example #10
Source File: FirstOf.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public T value() throws Exception {
    return new ItemAt<>(
        0,
        new FuncOf<>(this.fallback),
        new Filtered<>(this.condition, this.source)
    ).value();
}
 
Example #11
Source File: FbStatus.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param code HTTP status code
 * @param fallback Fallback
 */
public FbStatus(final int code, final Fallback fallback) {
    this(
        new Filtered<>(status -> code == status.intValue(), code),
        fallback
    );
}
 
Example #12
Source File: FbStatus.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param code HTTP status code
 * @param fallback Fallback
 */
public FbStatus(final int code, final Scalar<Fallback> fallback) {
    this(
        new Filtered<>(status -> code == status.intValue(), code),
        fallback
    );
}
 
Example #13
Source File: FbStatus.java    From takes with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param code HTTP status code
 * @since 0.16.10
 */
public FbStatus(final int code) {
    this(new Filtered<>(value -> code == value.intValue(), code));
}