我如何在Java中实现这个,而不使用Either?

huangapple go评论77阅读模式
英文:

How can I do this in java without using Either?

问题

private String processQuery(String[] args){
    //code logic
}

private String processQueryResponse(String[] args){
    //code logic
}

private String reply(String[] args){
    String result = processQuery(args);
    if (result.startsWith("(Your account detail is ")) {
        // Handle answer
    } else if (result.startsWith("(Sorry I cannot understand you")) {
        // Handle response
    }
    return result;
}
英文:

I have a function that returns String.

private String processQuery(String[] args){
    //code logic
}

Returned result can either be a answer (Your account detail is $account_detail.) or response (Sorry I cannot understand you?). Depending upon the result, code will do separate things.
What I came up with is to user Either<String, String>.

private Either<String,String> processQuery(String[] args){
    //code logic
}

private void reply(String[] args){
    //code logic
    var either = processQuery(args);
    return either.fold((l){
       //returned result is answer

    },(r){
       //returned result is response

    });
}

If it returns left then it is answer, if it returns right it is response. But since there is not Either type in java so I tried passing AtomicBoolean around.

What is the better solution for this only using java stl?

答案1

得分: 1

一种解决方案是使该方法接受两个对应于正确和错误答案的 lambda 函数,然后仅调用适当的函数。

private void processQuery(String[] args, Consumer<String> correct, Consumer<String> incorrect){
    if (args.length == 0) {
        incorrect.accept("抱歉,我无法理解您的问题。");
        return;
    }

    correct.accept("您的账户详情为 $account_detail。");
}

可以像这样调用:

private void reply(String[] args){
    processQuery(args, 
        reply -> System.out.println("成功!" + reply),
        reply -> System.out.println("失败," + reply)
    );
}

或者为不同的函数创建变量:

Consumer<String> badAnswer = reply -> System.out.println("失败," + reply);
Consumer<String> goodAnswer = reply -> System.out.println("成功!" + reply);
private void reply(String[] args){
    processQuery(args, goodAnswer, badAnswer);
}
英文:

One solution is to make the method take two lambda functions that corresponds to a correct and an incorrect answer and then call only the appropriate one

private void processQuery(String[] args, Consumer&lt;String&gt; correct, Consumer&lt;String&gt; incorrect){
    if (args.length == 0) {
        incorrect.accept(&quot;Sorry I cannot understand you?&quot;);
        return;
    }

    correct.accept(&quot;Your account detail is $account_detail.&quot;);
}

which can be called like this

private void reply(String[] args){
    processQuery(args, (
        reply -&gt; System.out.println(&quot;Success!, &quot; + reply)
    ),
    (
        reply -&gt; System.out.println(&quot;Fail, &quot; + reply)
    )
    );
}

or create variables for the different functions

Consumer&lt;String&gt; badAnswer = reply -&gt; System.out.println(&quot;Fail, &quot; + reply);
Consumer&lt;String&gt; goodAnswer = reply -&gt; System.out.println(&quot;Success!, &quot; + reply);
private void reply(String[] args){
    processQuery(args, goodAnswer, badAnswer);
}

答案2

得分: 0

你可以使用 Pair

Pair<String, String> pair = Pair.with("Blah", "Blee");

在这里查看一些示例 here

如果你的 responses 实际上表示某种错误,一个更好的方法是抛出某种异常,将 String 类型的返回值保留用于表示“良好”的流程。

英文:

You can use Pair:

Pair&lt;String, String&gt; pair = Pair.with(&quot;Blah&quot;, &quot;Blee&quot;);

See some example here

A better approach, if your responses actually represent some kind of an error, will be to throw an exception of some kind, and to keep the return value of String to represent a "good" flow.

huangapple
  • 本文由 发表于 2020年9月21日 19:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63991242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定