首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可选地在Java中返回某些内容

可选地在Java中返回某些内容
EN

Stack Overflow用户
提问于 2015-04-13 21:39:14
回答 4查看 128关注 0票数 1

我已经有一段时间没有编程了,所以有点生疏了。我已经被这个问题困扰了几个小时,我想知道在java中有哪些选项可以选择性地返回一些东西(或者在满足特定条件时跳回调用方法-基本上doBranch方法使用parseStatementList处理语句以及其他事情。在过去的几个小时里,我以不同的方式更改了parseStatementList,但它没有任何意义,这可能就是它不能工作的原因。

调用代码:基本上结束,诸如此类,BOO在SKIP_START_SET中。然而,parseStatement将这些作为普通单词来处理,因此如果BOO在字符串或某种类型的列表中,它会在第一个doBranch中丢失。

代码语言:javascript
运行
复制
    private C1 parseSelectStatement( TokenSet recoverSet) {

    C2 selectCond = doBranch( recoverSet );

    while (tokens.isMatch(BLAH)) {          
        match(BLAH)         
        C2 result = doBranch ( recoverSet );
    } 


    if (tokens.isMatch(BOO)) {
        match(boo)
        result1 = doBranch( recoverSet );
    }

    match( END )

    return new StatementNode.SelectNode(selectCond, result, result1);

}

private C2 doBranch(TokenSet recoverSet) {
    ExpNode cond = parseCondition(recoverSet.union(Token.KW_THEN));

    // The code jumps to below if words in SKIP_START_SET are found in the statement,
    // otherwise it will complete and return the new object.
    StatementNode result = parseStatementList(recoverSet.union(SKIP_START_SET));

    if (tokens.isIn(SKIP_START_SET)) {
        return null;
    }

    return new C2(pos, cond, result);       
}
EN

回答 4

Stack Overflow用户

发布于 2015-04-13 21:48:08

在Java8中,您可以使用Optional。在早期版本中,您可以使用第三方库来实现类似的功能,比如Guava。

票数 6
EN

Stack Overflow用户

发布于 2015-04-13 21:53:32

在某种程度上,你肯定想检查结果是否存在?你不能简单地在这里做一个空检查吗?或者,您可以将内置检查添加到ClassOfSomeSort

代码语言:javascript
运行
复制
public class ClassOfSomeSort {
    public long pos;
    public String cond;
    public StatementNode result = null;

    public boolean isEmpty() {
        // Return true if no result is set...
        return result != null; 
    }

    public ClassOfSomeSort(long pos, String cond) {
        this.pos = pos;
        this.cond = cond;
        this.result = null;
    }

    public ClassOfSomeSort(long pos, String cond, StatementNode result) {
        this.pos = pos;
        this.cond = cond;
        this.result = result;
    }
}

private ClassOfSomeSort doBranch(TokenSet recoverSet) {
    ExpNode cond = parseCondition(recoverSet.union(Token.KW_THEN));

    // The code jumps to below if words in SKIP_START_SET are found in the statement,
    // otherwise it will complete and return the new object.
    StatementNode result = parseStatementList(recoverSet.union(SKIP_START_SET));

    if (tokens.isIn(SKIP_START_SET)) {
        return new ClassOfSomeSort(pos, cond);
    }

    return new ClassOfSomeSort(pos, cond, result);       
}

调用该方法时,可以在处理结果之前对其进行检查:

代码语言:javascript
运行
复制
ClassOfSomeSort result = doBranch(...);
if (!result.isEmpty()) {
    // Do something with result
}
票数 0
EN

Stack Overflow用户

发布于 2015-04-13 22:09:40

你可以使用注释和其他答案中声明的,或者如果你想避免返回null,你可以返回一个对象列表,如果你没有返回任何结果,这个列表将是空的:

代码语言:javascript
运行
复制
private List<ClassOfSomeSort> doBranch(TokenSet recoverSet) {
    ExpNode cond = parseCondition(recoverSet.union(Token.KW_THEN));

    List<ClassOfSomeSort> myResult= new ArrayList<ClassOfSomeSort>();
    StatementNode result = parseStatementList(recoverSet.union(SKIP_START_SET));

    //if the condition is met you will have a result otherwise it will be an empty list
    if (!tokens.isIn(SKIP_START_SET)) { 
        myResult.add(new ClassOfSomeSort(pos, cond, result));
    }

    return myResult; //always return this list      
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29606768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档