Remove unused method in TimeAgoParser
This commit is contained in:
parent
6e3a4a6d9d
commit
c06b8731f1
|
@ -7,21 +7,14 @@ import org.schabi.newpipe.extractor.utils.Parser;
|
|||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.MatchResult;
|
||||
|
||||
/**
|
||||
* A helper class that is meant to be used by services that need to parse durations such as
|
||||
* {@code 23 seconds} and/or upload dates in the format {@code 2 days ago} or similar.
|
||||
*/
|
||||
public class TimeAgoParser {
|
||||
|
||||
private static final Pattern DURATION_PATTERN = Pattern.compile("(?:(\\d+) )?([A-z]+)");
|
||||
|
||||
private final PatternsHolder patternsHolder;
|
||||
private final OffsetDateTime now;
|
||||
|
||||
|
@ -50,13 +43,11 @@ public class TimeAgoParser {
|
|||
* @throws ParsingException if the time unit could not be recognized
|
||||
*/
|
||||
public DateWrapper parse(final String textualDate) throws ParsingException {
|
||||
for (final Map.Entry<ChronoUnit, Map<String, Integer>> caseUnitEntry
|
||||
: patternsHolder.specialCases().entrySet()) {
|
||||
for (final var caseUnitEntry : patternsHolder.specialCases().entrySet()) {
|
||||
final ChronoUnit chronoUnit = caseUnitEntry.getKey();
|
||||
for (final Map.Entry<String, Integer> caseMapToAmountEntry
|
||||
: caseUnitEntry.getValue().entrySet()) {
|
||||
for (final var caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
|
||||
final String caseText = caseMapToAmountEntry.getKey();
|
||||
final Integer caseAmount = caseMapToAmountEntry.getValue();
|
||||
final int caseAmount = caseMapToAmountEntry.getValue();
|
||||
|
||||
if (textualDateMatches(textualDate, caseText)) {
|
||||
return getResultFor(caseAmount, chronoUnit);
|
||||
|
@ -67,48 +58,6 @@ public class TimeAgoParser {
|
|||
return getResultFor(parseTimeAgoAmount(textualDate), parseChronoUnit(textualDate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a textual duration into a duration computer number.
|
||||
*
|
||||
* @param textualDuration the textual duration to parse
|
||||
* @return the textual duration parsed, as a primitive {@code long}
|
||||
* @throws ParsingException if the textual duration could not be parsed
|
||||
*/
|
||||
public long parseDuration(final String textualDuration) throws ParsingException {
|
||||
// We can't use Matcher.results, as it is only available on Android 14 and above
|
||||
final Matcher matcher = DURATION_PATTERN.matcher(textualDuration);
|
||||
final List<MatchResult> results = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
results.add(matcher.toMatchResult());
|
||||
}
|
||||
|
||||
return results.stream()
|
||||
.map(match -> {
|
||||
final String digits = match.group(1);
|
||||
final String word = match.group(2);
|
||||
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(digits);
|
||||
} catch (final NumberFormatException ignored) {
|
||||
amount = 1;
|
||||
}
|
||||
|
||||
final ChronoUnit unit;
|
||||
try {
|
||||
unit = parseChronoUnit(word);
|
||||
} catch (final ParsingException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
|
||||
return amount * unit.getDuration().getSeconds();
|
||||
})
|
||||
.filter(n -> n > 0)
|
||||
.reduce(Long::sum)
|
||||
.orElseThrow(() -> new ParsingException(
|
||||
"Could not parse duration \"" + textualDuration + "\""));
|
||||
}
|
||||
|
||||
private int parseTimeAgoAmount(final String textualDate) {
|
||||
try {
|
||||
return Integer.parseInt(textualDate.replaceAll("\\D+", ""));
|
||||
|
|
|
@ -1,31 +1,151 @@
|
|||
package org.schabi.newpipe.extractor.localization;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
class TimeAgoParserTest {
|
||||
private static TimeAgoParser timeAgoParser;
|
||||
public class TimeAgoParserTest {
|
||||
private static TimeAgoParser parser;
|
||||
private static OffsetDateTime now;
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
timeAgoParser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
|
||||
public static void setUp() {
|
||||
parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
|
||||
now = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDuration() throws ParsingException {
|
||||
assertEquals(1, timeAgoParser.parseDuration("one second"));
|
||||
assertEquals(1, timeAgoParser.parseDuration("second"));
|
||||
assertEquals(49, timeAgoParser.parseDuration("49 seconds"));
|
||||
assertEquals(61, timeAgoParser.parseDuration("1 minute, 1 second"));
|
||||
void parseTimeAgo() throws ParsingException {
|
||||
assertTimeWithin1s(
|
||||
now.minusSeconds(1),
|
||||
parser.parse("1 second ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusSeconds(12),
|
||||
parser.parse("12 second ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusMinutes(1),
|
||||
parser.parse("1 minute ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusMinutes(23),
|
||||
parser.parse("23 minutes ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusHours(1),
|
||||
parser.parse("1 hour ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusHours(8),
|
||||
parser.parse("8 hours ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 day ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusDays(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 days ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 week ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 weeks ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusMonths(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 month ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusMonths(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 months ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 year ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 years ago").offsetDateTime()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDurationError() {
|
||||
assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("abcd"));
|
||||
assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("12 abcd"));
|
||||
void parseTimeAgoShort() throws ParsingException {
|
||||
final TimeAgoParser parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
|
||||
final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
|
||||
assertTimeWithin1s(
|
||||
now.minusSeconds(1),
|
||||
parser.parse("1 sec ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusSeconds(12),
|
||||
parser.parse("12 sec ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusMinutes(1),
|
||||
parser.parse("1 min ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusMinutes(23),
|
||||
parser.parse("23 min ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusHours(1),
|
||||
parser.parse("1 hr ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minusHours(8),
|
||||
parser.parse("8 hr ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 day ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusDays(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 days ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 wk ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 wk ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusMonths(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 mo ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusMonths(3).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 mo ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 yr ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 yr ago").offsetDateTime()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void assertTimeWithin1s(final OffsetDateTime expected, final OffsetDateTime actual) {
|
||||
final long delta = Math.abs(expected.toEpochSecond() - actual.toEpochSecond());
|
||||
assertTrue(delta <= 1, String.format("Expected: %s\nActual: %s", expected, actual));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,154 +0,0 @@
|
|||
package org.schabi.newpipe.extractor.utils;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||
import org.schabi.newpipe.extractor.localization.Localization;
|
||||
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
||||
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TimeagoTest {
|
||||
private static TimeAgoParser parser;
|
||||
private static OffsetDateTime now;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
|
||||
now = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseTimeago() throws ParsingException {
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.SECONDS),
|
||||
parser.parse("1 second ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(12, ChronoUnit.SECONDS),
|
||||
parser.parse("12 second ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.MINUTES),
|
||||
parser.parse("1 minute ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(23, ChronoUnit.MINUTES),
|
||||
parser.parse("23 minutes ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.HOURS),
|
||||
parser.parse("1 hour ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(8, ChronoUnit.HOURS),
|
||||
parser.parse("8 hours ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 day ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 days ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 week ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 weeks ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 month ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 months ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 year ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 years ago").offsetDateTime()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseTimeagoShort() throws ParsingException {
|
||||
final TimeAgoParser parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
|
||||
final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.SECONDS),
|
||||
parser.parse("1 sec ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(12, ChronoUnit.SECONDS),
|
||||
parser.parse("12 sec ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.MINUTES),
|
||||
parser.parse("1 min ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(23, ChronoUnit.MINUTES),
|
||||
parser.parse("23 min ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(1, ChronoUnit.HOURS),
|
||||
parser.parse("1 hr ago").offsetDateTime()
|
||||
);
|
||||
assertTimeWithin1s(
|
||||
now.minus(8, ChronoUnit.HOURS),
|
||||
parser.parse("8 hr ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 day ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 days ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 wk ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 wk ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 mo ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 mo ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(1, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("1 yr ago").offsetDateTime()
|
||||
);
|
||||
assertEquals(
|
||||
now.minus(3, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS),
|
||||
parser.parse("3 yr ago").offsetDateTime()
|
||||
);
|
||||
}
|
||||
|
||||
void assertTimeWithin1s(final OffsetDateTime expected, final OffsetDateTime actual) {
|
||||
final long delta = Math.abs(expected.toEpochSecond() - actual.toEpochSecond());
|
||||
assertTrue(delta <= 1, String.format("Expected: %s\nActual: %s", expected, actual));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue