Java Reference
In-Depth Information
/**
* Source for a Shakespeare Text
*/
public class ShakespeareTextSource {
private static final String SOURCE = "shakespeare.txt";
/**
* Provides a reader of the in-memory anthology. No filtering is performed
* on this reader.
*
* @return A reader of the in-memory anthology; never {@code null}
* @throws IOException If there is an exception retrieving the
* in-memory anthology
*/
public BufferedReader getReader() throws IOException {
InputStream inputStream =
this.getClass()
.getClassLoader()
.getResourceAsStream(SOURCE);
return new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
}
private static UnaryOperator<String> filterBadChars() {
Pattern BAD_CHARS =
Pattern.compile("\uFEFF|\\p{Cntrl}"); // BOMs and control characters
return string -> BAD_CHARS.matcher(string).replaceAll("");
}
private static Predicate<String> notInComment() {
final Predicate<String> IS_COMMENT_START =
Pattern.compile("^<<").asPredicate();
final Predicate<String> IS_COMMENT_END =
Pattern.compile(">>$").asPredicate();
final AtomicBoolean inComment = new AtomicBoolean(false);
return line -> {
if (IS_COMMENT_START.test(line)) {
inComment.set(true);
return false;
} else if (inComment.get() && IS_COMMENT_END.test(line)) {
inComment.set(false);
return false;
} else {
return !inComment.get();
}
};
}
Search WWH ::




Custom Search