英文:
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<String> correct, Consumer<String> incorrect){
if (args.length == 0) {
incorrect.accept("Sorry I cannot understand you?");
return;
}
correct.accept("Your account detail is $account_detail.");
}
which can be called like this
private void reply(String[] args){
processQuery(args, (
reply -> System.out.println("Success!, " + reply)
),
(
reply -> System.out.println("Fail, " + reply)
)
);
}
or create variables for the different functions
Consumer<String> badAnswer = reply -> System.out.println("Fail, " + reply);
Consumer<String> goodAnswer = reply -> System.out.println("Success!, " + 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<String, String> pair = Pair.with("Blah", "Blee");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论