Skip to content

Commit 7c78c39

Browse files
authored
Merge pull request TeamNewPipe#821 from litetex/cleanup-TimeAgoParser-java
Cleanup ``TimeAgoParser``
2 parents ac1c22d + 3bf7aa3 commit 7c78c39

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.time.OffsetDateTime;
88
import java.time.ZoneOffset;
99
import java.time.temporal.ChronoUnit;
10-
import java.util.Collection;
1110
import java.util.Map;
1211
import java.util.regex.Pattern;
1312

@@ -58,36 +57,27 @@ public DateWrapper parse(final String textualDate) throws ParsingException {
5857
}
5958
}
6059

61-
int timeAgoAmount;
60+
return getResultFor(parseTimeAgoAmount(textualDate), parseChronoUnit(textualDate));
61+
}
62+
63+
private int parseTimeAgoAmount(final String textualDate) {
6264
try {
63-
timeAgoAmount = parseTimeAgoAmount(textualDate);
64-
} catch (final NumberFormatException e) {
65+
return Integer.parseInt(textualDate.replaceAll("\\D+", ""));
66+
} catch (final NumberFormatException ignored) {
6567
// If there is no valid number in the textual date,
6668
// assume it is 1 (as in 'a second ago').
67-
timeAgoAmount = 1;
69+
return 1;
6870
}
69-
70-
final ChronoUnit chronoUnit = parseChronoUnit(textualDate);
71-
return getResultFor(timeAgoAmount, chronoUnit);
72-
}
73-
74-
private int parseTimeAgoAmount(final String textualDate) throws NumberFormatException {
75-
return Integer.parseInt(textualDate.replaceAll("\\D+", ""));
7671
}
7772

7873
private ChronoUnit parseChronoUnit(final String textualDate) throws ParsingException {
79-
for (final Map.Entry<ChronoUnit, Collection<String>> entry
80-
: patternsHolder.asMap().entrySet()) {
81-
final ChronoUnit chronoUnit = entry.getKey();
82-
83-
for (final String agoPhrase : entry.getValue()) {
84-
if (textualDateMatches(textualDate, agoPhrase)) {
85-
return chronoUnit;
86-
}
87-
}
88-
}
89-
90-
throw new ParsingException("Unable to parse the date: " + textualDate);
74+
return patternsHolder.asMap().entrySet().stream()
75+
.filter(e -> e.getValue().stream()
76+
.anyMatch(agoPhrase -> textualDateMatches(textualDate, agoPhrase)))
77+
.map(Map.Entry::getKey)
78+
.findFirst()
79+
.orElseThrow(() ->
80+
new ParsingException("Unable to parse the date: " + textualDate));
9181
}
9282

9383
private boolean textualDateMatches(final String textualDate, final String agoPhrase) {
@@ -97,24 +87,21 @@ private boolean textualDateMatches(final String textualDate, final String agoPhr
9787

9888
if (patternsHolder.wordSeparator().isEmpty()) {
9989
return textualDate.toLowerCase().contains(agoPhrase.toLowerCase());
100-
} else {
101-
final String escapedPhrase = Pattern.quote(agoPhrase.toLowerCase());
102-
final String escapedSeparator;
103-
if (patternsHolder.wordSeparator().equals(" ")) {
90+
}
91+
92+
final String escapedPhrase = Pattern.quote(agoPhrase.toLowerCase());
93+
final String escapedSeparator = patternsHolder.wordSeparator().equals(" ")
10494
// From JDK8 → \h - Treat horizontal spaces as a normal one
10595
// (non-breaking space, thin space, etc.)
106-
escapedSeparator = "[ \\t\\xA0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]";
107-
} else {
108-
escapedSeparator = Pattern.quote(patternsHolder.wordSeparator());
109-
}
96+
? "[ \\t\\xA0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]"
97+
: Pattern.quote(patternsHolder.wordSeparator());
11098

111-
// (^|separator)pattern($|separator)
112-
// Check if the pattern is surrounded by separators or start/end of the string.
113-
final String pattern =
114-
"(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")";
99+
// (^|separator)pattern($|separator)
100+
// Check if the pattern is surrounded by separators or start/end of the string.
101+
final String pattern =
102+
"(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")";
115103

116-
return Parser.isMatch(pattern, textualDate.toLowerCase());
117-
}
104+
return Parser.isMatch(pattern, textualDate.toLowerCase());
118105
}
119106

120107
private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) {

0 commit comments

Comments
 (0)