From dc82d445c8bbc1ac5126947cb895e062bf6bf6b5 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 26 Sep 2023 16:44:13 +0200 Subject: [PATCH] Switch from classic for to streaming --- .../logic/integrity/AmpersandChecker.java | 41 +++++++++---------- .../logic/integrity/HTMLCharacterChecker.java | 22 +++------- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/jabref/logic/integrity/AmpersandChecker.java b/src/main/java/org/jabref/logic/integrity/AmpersandChecker.java index 560e77fbb13..48df6ddfcf6 100644 --- a/src/main/java/org/jabref/logic/integrity/AmpersandChecker.java +++ b/src/main/java/org/jabref/logic/integrity/AmpersandChecker.java @@ -1,17 +1,16 @@ package org.jabref.logic.integrity; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.regex.MatchResult; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.BibEntry; -import org.jabref.model.entry.field.Field; import org.jabref.model.entry.field.FieldProperty; import com.google.common.base.CharMatcher; +import org.jooq.lambda.tuple.Tuple2; /** * Checks if the BibEntry contains unescaped ampersands. @@ -23,24 +22,22 @@ public class AmpersandChecker implements EntryChecker { @Override public List check(BibEntry entry) { - List results = new ArrayList<>(); - - for (Map.Entry field : entry.getFieldMap().entrySet()) { - if (field.getKey().getProperties().contains(FieldProperty.VERBATIM)) { - continue; - } - // counts the number of even \ occurrences preceding an & - long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(field.getValue()) - .results() - .map(MatchResult::group) - .filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0) - .count(); - - if (unescapedAmpersands > 0) { - results.add(new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", unescapedAmpersands), entry, field.getKey())); - // note: when changing the message - also do so in tests - } - } - return results; + return entry.getFieldMap().entrySet().stream() + .filter(field -> !field.getKey().getProperties().contains(FieldProperty.VERBATIM)) + // We use "flatMap" instead of filtering later, because we assume there won't be that much error messages - and construction of Stream.empty() is faster than construction of a new Tuple2 (including lifting long to Long) + .flatMap(field -> { + // counts the number of even \ occurrences preceding an & + long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(field.getValue()) + .results() + .map(MatchResult::group) + .filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0) + .count(); + if (unescapedAmpersands == 0) { + return Stream.empty(); + } + return Stream.of(new Tuple2<>(field.getKey(), unescapedAmpersands)); + }) + .map(tuple -> new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", tuple.v2()), entry, tuple.v1())) + .toList(); } } diff --git a/src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java b/src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java index 267cf2435b4..874a9c79c8d 100644 --- a/src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java +++ b/src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java @@ -1,14 +1,10 @@ package org.jabref.logic.integrity; -import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.BibEntry; -import org.jabref.model.entry.field.Field; import org.jabref.model.entry.field.FieldProperty; /** @@ -20,18 +16,10 @@ public class HTMLCharacterChecker implements EntryChecker { @Override public List check(BibEntry entry) { - List results = new ArrayList<>(); - for (Map.Entry field : entry.getFieldMap().entrySet()) { - if (field.getKey().getProperties().contains(FieldProperty.VERBATIM)) { - continue; - } - - Matcher characterMatcher = HTML_CHARACTER_PATTERN.matcher(field.getValue()); - if (characterMatcher.find()) { - results.add( - new IntegrityMessage(Localization.lang("HTML encoded character found"), entry, field.getKey())); - } - } - return results; + return entry.getFieldMap().entrySet().stream() + .filter(field -> !field.getKey().getProperties().contains(FieldProperty.VERBATIM)) + .filter(field -> HTML_CHARACTER_PATTERN.matcher(field.getValue()).find()) + .map(field -> new IntegrityMessage(Localization.lang("HTML encoded character found"), entry, field.getKey())) + .toList(); } }