1
categoryCheck: {
        for (String allowedCategory : allowedCategories) {
            if (evt.getLoggerName().startsWith(allowedCategory)) {
                break categoryCheck;
            }
        }
        return false;
    }

是否有任何想法如何在不使用标签且不显着增加标签的情况下重写此代码?

4

4 回答 4

6

我可能会将其放入自己的方法中:

// I've guessed at the types...
public boolean isCategoryAllowed(Event evt, Iterable<String> allowedCategories) {
    for (String allowedCategory : allowedCategories) {
        if (evt.getLoggerName().startsWith(allowedCategory)) {
            return true;
        }
    }
    return false;
}

然后将调用代码更改为简单地调用该方法:

if (!isCategoryAllowed(evt, allowedCategories)) {
    return false;
}
于 2011-10-21T09:28:58.113 回答
1

这是使用布尔标志的直接等价物:

    boolean found = false;
    for (String allowedCategory : allowedCategories) {
        if (evt.getLoggerName().startsWith(allowedCategory)) {
            found = true;
            break;
        }
    }
    if (!found) {
        return false;
    }
    // ...the rest of the method's code...
于 2011-10-21T09:28:24.940 回答
1
boolean matched = false;

for (String allowedCategory : allowedCategories) {
    if (evt.getLoggerName().startsWith(allowedCategory)) {
        matched = true;
        break;
    }
}

if (!matched)
    return false;

// else continue with the rest of the code
于 2011-10-21T09:29:53.090 回答
1

您可以使用标志。

boolean found = false;
for (String allowedCategory : allowedCategories) {
    if (evt.getLoggerName().startsWith(allowedCategory)) {
        found = true;
        break;
    }
}
if(!found) 
    return false;
于 2011-10-21T09:30:00.797 回答