[英]Writing a filter that goes between a charstreams inputsupplier and outputsupplier
I would like to write a grep type filter that takes a Guava Charstreams InputSupplier as an input and uses a Charstreams OutputSupplier as its output. 我想编写一个grep类型的过滤器,该过滤器将Guava Charstreams InputSupplier作为输入,并使用Charstreams OutputSupplier作为其输出。 It should only pass lines from the inputsupplier to the outputsupplier if they satisfy a particular regular expression. 如果它们满足特定的正则表达式,则仅应将行从输入供应商传递到输出供应商。
What is the correct design pattern/paradim for doing this? 正确的设计模式/范式是什么?
I would guess you would do the line filter like this: 我猜你会做这样的行过滤器:
InputSupplier<InputStreamReader> ris = CharStreams.newReaderSupplier(....
CharStreams.readLines(ris, new LineProcessor<....
and implementing the LineProcessor methods. 并实现LineProcessor方法。
But what should the LineProcessor.getResult() return - just a succcess of failure? 但是LineProcessor.getResult()应该返回什么-只是失败的成功? Should I be using a 'final' outputsupplier in the surrounding function? 我是否应该在周围的功能中使用“最终”输出供应商?
Or am I using completely the wrong api/approach!! 还是我使用的API /方法完全错误!
A bit of pseudocode to demonstrate the best way would be much appreciated. 一些伪代码来演示最佳方法将不胜感激。
Thanks for your suggestions. 感谢您的建议。
CharStreams.readLines returns a List<String>
. CharStreams.readLines返回一个List<String>
。 So according to source code it is asking LineProcessor to accumulate lines and then return the result. 因此,根据源代码,它要求LineProcessor累积行,然后返回结果。 In my opinion your LineReader can be something as below 我认为您的LineReader可以如下所示
new LineProcessor<List<String>>() {
List<String> result = Lists.newArrayList();
public boolean processLine(String line) {
if (line matches regex){
result.add(line.trim());
}
return true;//continue processing
}
public List<String> getResult() {return result;}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.