重构模式匹配的顺序
|
我有以下代码(为示例简化):
while (reader.ready()) {
String line = reader.readLine();
Matcher responseCodeMatcher = responseCodePattern.matcher(line);
if (responseCodeMatcher.matches()) {
responseCode = Integer.parseInt(responseCodeMatcher.group(1));
continue;
}
Matcher cacheControlMatcher = cacheControlPattern.matcher(line);
if (cacheControlMatcher.matches()) {
cacheControl = CacheControl.parseString(responseCodeMatcher.group(1));
continue;
}
...
}
这些模式都是该类的静态最终成员。
所以我有很多模式,我想为每行找出是否与它们中的一个匹配,如果有,那就做点什么(随模式而变化)。您能想到一种以某种方式很好地重构此方法的方法吗?也许是我浏览过的一组模式(然后,如果匹配则如何知道该怎么办?)或其他一些想法。
没有找到相关结果
已邀请:
3 个回复
缔恃钨
我不知道,这对Java专家来说是否有意义,尤其是使用lambda语法时。。。
舜辉
:
我这样使用它:
我不喜欢我必须抛弃所有获得的东西,但这是到目前为止找到的最好的解决方案。
骨酚柯