categoryCheck: {
for (String allowedCategory : allowedCategories) {
if (evt.getLoggerName().startsWith(allowedCategory)) {
break categoryCheck;
}
}
return false;
}
是否有任何想法如何在不使用标签的情况下重写此代码并且不会显着增加它?
答案 0 :(得分: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;
}
答案 1 :(得分: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...
答案 2 :(得分: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
答案 3 :(得分:1)
您可以使用旗帜。
boolean found = false;
for (String allowedCategory : allowedCategories) {
if (evt.getLoggerName().startsWith(allowedCategory)) {
found = true;
break;
}
}
if(!found)
return false;