Switch to ChronoUnit.

This commit is contained in:
Isira Seneviratne 2020-10-18 07:52:28 +05:30
parent 0526a5148d
commit ee3af63c04
4 changed files with 33 additions and 47 deletions

View File

@ -2,9 +2,9 @@ package org.schabi.newpipe.extractor.localization;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
import org.schabi.newpipe.extractor.utils.Parser;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
@ -42,14 +42,14 @@ public class TimeAgoParser {
* @throws ParsingException if the time unit could not be recognized
*/
public DateWrapper parse(String textualDate) throws ParsingException {
for (Map.Entry<TimeAgoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
final TimeAgoUnit timeAgoUnit = caseUnitEntry.getKey();
for (Map.Entry<ChronoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
final ChronoUnit chronoUnit = caseUnitEntry.getKey();
for (Map.Entry<String, Integer> caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
final String caseText = caseMapToAmountEntry.getKey();
final Integer caseAmount = caseMapToAmountEntry.getValue();
if (textualDateMatches(textualDate, caseText)) {
return getResultFor(caseAmount, timeAgoUnit);
return getResultFor(caseAmount, chronoUnit);
}
}
}
@ -63,8 +63,8 @@ public class TimeAgoParser {
timeAgoAmount = 1;
}
final TimeAgoUnit timeAgoUnit = parseTimeAgoUnit(textualDate);
return getResultFor(timeAgoAmount, timeAgoUnit);
final ChronoUnit chronoUnit = parseChronoUnit(textualDate);
return getResultFor(timeAgoAmount, chronoUnit);
}
private int parseTimeAgoAmount(String textualDate) throws NumberFormatException {
@ -72,13 +72,13 @@ public class TimeAgoParser {
return Integer.parseInt(timeValueStr);
}
private TimeAgoUnit parseTimeAgoUnit(String textualDate) throws ParsingException {
for (Map.Entry<TimeAgoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
final TimeAgoUnit timeAgoUnit = entry.getKey();
private ChronoUnit parseChronoUnit(String textualDate) throws ParsingException {
for (Map.Entry<ChronoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
final ChronoUnit chronoUnit = entry.getKey();
for (String agoPhrase : entry.getValue()) {
if (textualDateMatches(textualDate, agoPhrase)) {
return timeAgoUnit;
return chronoUnit;
}
}
}
@ -112,11 +112,11 @@ public class TimeAgoParser {
}
}
private DateWrapper getResultFor(int timeAgoAmount, TimeAgoUnit timeAgoUnit) {
private DateWrapper getResultFor(int timeAgoAmount, ChronoUnit chronoUnit) {
final Calendar calendarTime = getNow();
boolean isApproximation = false;
switch (timeAgoUnit) {
switch (chronoUnit) {
case SECONDS:
calendarTime.add(Calendar.SECOND, -timeAgoAmount);
break;

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.extractor.timeago;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
@ -16,7 +17,7 @@ public abstract class PatternsHolder {
private final Collection<String> months;
private final Collection<String> years;
private final Map<TimeAgoUnit, Map<String, Integer>> specialCases = new LinkedHashMap<>();
private final Map<ChronoUnit, Map<String, Integer>> specialCases = new LinkedHashMap<>();
protected PatternsHolder(String wordSeparator, Collection<String> seconds, Collection<String> minutes,
Collection<String> hours, Collection<String> days,
@ -69,30 +70,25 @@ public abstract class PatternsHolder {
return years;
}
public Map<TimeAgoUnit, Map<String, Integer>> specialCases() {
public Map<ChronoUnit, Map<String, Integer>> specialCases() {
return specialCases;
}
protected void putSpecialCase(TimeAgoUnit unit, String caseText, int caseAmount) {
Map<String, Integer> item = specialCases.get(unit);
if (item == null) {
item = new LinkedHashMap<>();
specialCases.put(unit, item);
}
protected void putSpecialCase(ChronoUnit unit, String caseText, int caseAmount) {
Map<String, Integer> item = specialCases.computeIfAbsent(unit, k -> new LinkedHashMap<>());
item.put(caseText, caseAmount);
}
public Map<TimeAgoUnit, Collection<String>> asMap() {
final Map<TimeAgoUnit, Collection<String>> returnMap = new LinkedHashMap<>();
returnMap.put(TimeAgoUnit.SECONDS, seconds());
returnMap.put(TimeAgoUnit.MINUTES, minutes());
returnMap.put(TimeAgoUnit.HOURS, hours());
returnMap.put(TimeAgoUnit.DAYS, days());
returnMap.put(TimeAgoUnit.WEEKS, weeks());
returnMap.put(TimeAgoUnit.MONTHS, months());
returnMap.put(TimeAgoUnit.YEARS, years());
public Map<ChronoUnit, Collection<String>> asMap() {
final Map<ChronoUnit, Collection<String>> returnMap = new LinkedHashMap<>();
returnMap.put(ChronoUnit.SECONDS, seconds());
returnMap.put(ChronoUnit.MINUTES, minutes());
returnMap.put(ChronoUnit.HOURS, hours());
returnMap.put(ChronoUnit.DAYS, days());
returnMap.put(ChronoUnit.WEEKS, weeks());
returnMap.put(ChronoUnit.MONTHS, months());
returnMap.put(ChronoUnit.YEARS, years());
return returnMap;
}

View File

@ -1,11 +0,0 @@
package org.schabi.newpipe.extractor.timeago;
public enum TimeAgoUnit {
SECONDS,
MINUTES,
HOURS,
DAYS,
WEEKS,
MONTHS,
YEARS
}

View File

@ -5,7 +5,8 @@
package org.schabi.newpipe.extractor.timeago.patterns;
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
import java.time.temporal.ChronoUnit;
public class iw extends PatternsHolder {
private static final String WORD_SEPARATOR = " ";
@ -26,10 +27,10 @@ public class iw extends PatternsHolder {
private iw() {
super(WORD_SEPARATOR, SECONDS, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS);
putSpecialCase(TimeAgoUnit.HOURS, "שעתיים", 2);
putSpecialCase(TimeAgoUnit.DAYS, "יומיים", 2);
putSpecialCase(TimeAgoUnit.WEEKS, "שבועיים", 2);
putSpecialCase(TimeAgoUnit.MONTHS, "חודשיים", 2);
putSpecialCase(TimeAgoUnit.YEARS, "שנתיים", 2);
putSpecialCase(ChronoUnit.HOURS, "שעתיים", 2);
putSpecialCase(ChronoUnit.DAYS, "יומיים", 2);
putSpecialCase(ChronoUnit.WEEKS, "שבועיים", 2);
putSpecialCase(ChronoUnit.MONTHS, "חודשיים", 2);
putSpecialCase(ChronoUnit.YEARS, "שנתיים", 2);
}
}