Extract removal of trailing whitespace parts into its own method.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index 4f9900a..78d9bdc 100644 (file)
@@ -194,8 +194,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
                                        lineComplete = false;
 
-                                       Matcher matcher = whitespacePattern.matcher(line);
-                                       int nextSpace = matcher.find(0) ? matcher.start() : line.length();
+                                       int nextSpace = findNextWhitespace(line);
                                        String link = line.substring(0, nextSpace);
                                        String name = link;
                                        logger.log(Level.FINER, String.format("Found link: %s", link));
@@ -299,6 +298,11 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                Closer.close(bufferedReader);
                        }
                }
+               removeTrailingWhitespaceParts(parts);
+               return parts;
+       }
+
+       private void removeTrailingWhitespaceParts(PartContainer parts) {
                for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
                        Part part = parts.getPart(partIndex);
                        if (!(part instanceof PlainTextPart) || !"\n".equals(part.getText())) {
@@ -306,7 +310,11 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                        }
                        parts.removePart(partIndex);
                }
-               return parts;
+       }
+
+       private int findNextWhitespace(String line) {
+               Matcher matcher = whitespacePattern.matcher(line);
+               return matcher.find(0) ? matcher.start() : line.length();
        }
 
        private Optional<NextLink> findNextLink(String line) {