Skip to content

Commit

Permalink
More "old tyle" Java
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 28, 2023
1 parent 812ee1a commit 9ec1112
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/main/java/org/jabref/logic/integrity/AmpersandChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,21 @@ public List<IntegrityMessage> check(BibEntry entry) {
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(getUnescapedAmpersandsWithCount())
.map(tuple -> new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", tuple.getValue()), entry, tuple.getKey()))
.flatMap(AmpersandChecker::getUnescapedAmpersandsWithCount)
.map(pair -> new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", pair.getValue()), entry, pair.getKey()))
.toList();
}

private static Function<Map.Entry<Field, String>, Stream<? extends Pair<Field, Long>>> getUnescapedAmpersandsWithCount() {
return 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 Pair<>(field.getKey(), unescapedAmpersands));
};
private static Stream<Pair<Field, Long>> getUnescapedAmpersandsWithCount(Map.Entry<Field, String> entry) {
// counts the number of even \ occurrences preceding an &
long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(entry.getValue())
.results()
.map(MatchResult::group)
.filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0)
.count();
if (unescapedAmpersands == 0) {
return Stream.empty();
}
return Stream.of(new Pair<>(entry.getKey(), unescapedAmpersands));
}
}

0 comments on commit 9ec1112

Please sign in to comment.