可选地用Java返回一些东西

use*_*148 1 java

我有一段时间没有编程,所以我有点生疏了.我已经被困在这个问题上几个小时了,我想知道有什么选择可以选择在java中返回一些东西(或者当满足某个条件时跳回到调用方法 - 基本上doBranch方法使用parseStatementList等等.我在过去几个小时内以各种方式更改了parseStatementList,但这对于它可能无效的原因没有意义.

呼叫代码:[基本上是END,BLAH,BOO]在SKIP_START_SET中.然而,parseStatement将这些作为普通单词处理,因此如果BOO在字符串或某种类型的列表中,它将在第一个doBranch上丢失.

    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);       
}
Run Code Online (Sandbox Code Playgroud)

mes*_*azs 6

在Java 8中,您可以使用Optional <T>.在早期版本中,您可以使用第三方库(如Guava)实现类似的功能.