Skip to content

Commit

Permalink
Switch from classic for to streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 26, 2023
1 parent a9b1015 commit dc82d44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
41 changes: 19 additions & 22 deletions src/main/java/org/jabref/logic/integrity/AmpersandChecker.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,24 +22,22 @@ public class AmpersandChecker implements EntryChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();

for (Map.Entry<Field, String> 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();
}
}
22 changes: 5 additions & 17 deletions src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -20,18 +16,10 @@ public class HTMLCharacterChecker implements EntryChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();
for (Map.Entry<Field, String> 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();
}
}

0 comments on commit dc82d44

Please sign in to comment.