+ }
+
+ private boolean linkMatchesPostingSone(SoneTextParserContext context, String link) {
+ return (context != null) && (context.getPostingSone() != null) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
+ }
+
+ private int findNextWhitespace(String line) {
+ Matcher matcher = whitespacePattern.matcher(line);
+ return matcher.find(0) ? matcher.start() : line.length();
+ }
+
+ private Optional<NextLink> findNextLink(String line) {
+ EnumMap<LinkType, Integer> linkTypeIndexes = new EnumMap<LinkType, Integer>(LinkType.class);
+ for (LinkType linkType : LinkType.values()) {
+ int index = line.indexOf(linkType.getScheme());
+ if (index != -1) {
+ linkTypeIndexes.put(linkType, index);
+ }
+ }
+ if (linkTypeIndexes.isEmpty()) {
+ return absent();
+ }
+ Entry<LinkType, Integer> smallestEntry = from(linkTypeIndexes.entrySet()).toSortedList(locateSmallestIndex()).get(0);
+ return of(new NextLink(smallestEntry.getValue(), smallestEntry.getKey()));
+ }
+
+ private Comparator<Entry<LinkType, Integer>> locateSmallestIndex() {
+ return new Comparator<Entry<LinkType, Integer>>() {
+ @Override
+ public int compare(Entry<LinkType, Integer> leftEntry, Entry<LinkType, Integer> rightEntry) {
+ return leftEntry.getValue() - rightEntry.getValue();
+ }
+ };
+ }
+
+ /**
+ * Container for position and type of the next link in a line.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ private static class NextLink {
+
+ private final int nextIndex;
+ private final LinkType linkType;
+
+ private NextLink(int nextIndex, LinkType linkType) {
+ this.nextIndex = nextIndex;
+ this.linkType = linkType;
+ }
+
+ private int getNextIndex() {
+ return nextIndex;
+ }
+
+ private LinkType getLinkType() {
+ return linkType;
+ }
+