Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1019) Simplified conditional statement with EO way #1045

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions src/main/java/org/takes/facets/fork/FkHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
package org.takes.facets.fork;

import lombok.EqualsAndHashCode;
import org.cactoos.Text;
import org.cactoos.scalar.EqualsNullable;
import org.cactoos.scalar.Ternary;
import org.cactoos.text.Lowered;
import org.cactoos.text.UncheckedText;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Opt;
Expand All @@ -48,11 +47,6 @@
*
* @see TkFork
* @since 0.32
* @todo #998:30min Use {@link org.cactoos.scalar.Ternary} scalar here for
* conditionally evaluated result. Please refere to
* https://github.com/yegor256/cactoos/blob/master/src/main/java/org/cactoos/
* scalar/Ternary.java class for javadoc on how to use this scalar in an
* elegant way.
*/
@EqualsAndHashCode(callSuper = true)
public final class FkHost extends FkWrap {
Expand All @@ -73,23 +67,17 @@ public FkHost(final String host, final Take take) {
* @return Fork
*/
private static Fork fork(final String host, final Take take) {
// @checkstyle AnonInnerLengthCheck (50 lines)
return new Fork() {
@Override
public Opt<Response> route(final Request req) throws Exception {
final String hst = new RqHeaders.Smart(
new RqHeaders.Base(req)
).single("host");
final Opt<Response> rsp;
final Text lowhost = new UncheckedText(new Lowered(host));
final Text lowhst = new UncheckedText(new Lowered(hst));
if (lowhost.asString().equals(lowhst.asString())) {
rsp = new Opt.Single<>(take.act(req));
} else {
rsp = new Opt.Empty<>();
}
return rsp;
}
return req -> {
final String hst = new RqHeaders.Smart(
new RqHeaders.Base(req)
).single("host");
return new Ternary<Opt<Response>>(
new EqualsNullable(
new Lowered(host), new Lowered(hst)
),
new Opt.Single<>(take.act(req)),
new Opt.Empty<>()
).value();
};
}

Expand Down