Java Reference
In-Depth Information
public String peek() {
explodeIfException();
if (nextElement != null) return nextElement;
if (closed) return null;
return nextLine();
}
}
### ShakespeareAnthologyParser.java
import java.io.IOException;
import java.util.*;
import java.util.function.*;
import java.util.regex.*;
/**
* Responsible for parsing a Shakespeare anthology into texts.
*/
public class ShakespeareAnthologyParser {
private static final Predicate<String> IS_YEAR =
Pattern.compile("^1(5|6)\\d\\d$").asPredicate();
private static final Predicate<String> IS_AUTHOR =
Pattern.compile("^by\\s+William\\s+Shakespeare$").asPredicate();
private static final Predicate<String> IS_THE_END =
Pattern.compile("^THE\\s+END$").asPredicate();
private static final Predicate<String> IS_DIGITS =
Pattern.compile("^\\d+$").asPredicate();
private static final String SONNETS_TITLE = "THE SONNETS";
/**
* Parses the texts based on the in-memory Shakespeare anthology.
*
* @return The parsed texts
* @throws IOException If an exception occurs in reading the texts
*/
public Collection<ShakespeareText> parseTexts() throws IOException {
List<ShakespeareText> texts = new ArrayList<>();
ShakespeareLineIterator lines = new ShakespeareLineIterator();
while (lines.hasNext()) {
int year = parseYear(lines);
String title = parseTitle(lines);
parseAuthor(lines);
if (SONNETS_TITLE.equalsIgnoreCase(title)) {
texts.addAll(parseSonnets(lines));
} else {
texts.add(parseText(title, year, lines));
}
}
return texts;
}
Search WWH ::




Custom Search