Switch to ChronoUnit.
This commit is contained in:
parent
0526a5148d
commit
ee3af63c04
|
@ -2,9 +2,9 @@ package org.schabi.newpipe.extractor.localization;
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
|
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
|
||||||
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
|
|
||||||
import org.schabi.newpipe.extractor.utils.Parser;
|
import org.schabi.newpipe.extractor.utils.Parser;
|
||||||
|
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -42,14 +42,14 @@ public class TimeAgoParser {
|
||||||
* @throws ParsingException if the time unit could not be recognized
|
* @throws ParsingException if the time unit could not be recognized
|
||||||
*/
|
*/
|
||||||
public DateWrapper parse(String textualDate) throws ParsingException {
|
public DateWrapper parse(String textualDate) throws ParsingException {
|
||||||
for (Map.Entry<TimeAgoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
|
for (Map.Entry<ChronoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
|
||||||
final TimeAgoUnit timeAgoUnit = caseUnitEntry.getKey();
|
final ChronoUnit chronoUnit = caseUnitEntry.getKey();
|
||||||
for (Map.Entry<String, Integer> caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
|
for (Map.Entry<String, Integer> caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
|
||||||
final String caseText = caseMapToAmountEntry.getKey();
|
final String caseText = caseMapToAmountEntry.getKey();
|
||||||
final Integer caseAmount = caseMapToAmountEntry.getValue();
|
final Integer caseAmount = caseMapToAmountEntry.getValue();
|
||||||
|
|
||||||
if (textualDateMatches(textualDate, caseText)) {
|
if (textualDateMatches(textualDate, caseText)) {
|
||||||
return getResultFor(caseAmount, timeAgoUnit);
|
return getResultFor(caseAmount, chronoUnit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,8 +63,8 @@ public class TimeAgoParser {
|
||||||
timeAgoAmount = 1;
|
timeAgoAmount = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
final TimeAgoUnit timeAgoUnit = parseTimeAgoUnit(textualDate);
|
final ChronoUnit chronoUnit = parseChronoUnit(textualDate);
|
||||||
return getResultFor(timeAgoAmount, timeAgoUnit);
|
return getResultFor(timeAgoAmount, chronoUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int parseTimeAgoAmount(String textualDate) throws NumberFormatException {
|
private int parseTimeAgoAmount(String textualDate) throws NumberFormatException {
|
||||||
|
@ -72,13 +72,13 @@ public class TimeAgoParser {
|
||||||
return Integer.parseInt(timeValueStr);
|
return Integer.parseInt(timeValueStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TimeAgoUnit parseTimeAgoUnit(String textualDate) throws ParsingException {
|
private ChronoUnit parseChronoUnit(String textualDate) throws ParsingException {
|
||||||
for (Map.Entry<TimeAgoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
|
for (Map.Entry<ChronoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
|
||||||
final TimeAgoUnit timeAgoUnit = entry.getKey();
|
final ChronoUnit chronoUnit = entry.getKey();
|
||||||
|
|
||||||
for (String agoPhrase : entry.getValue()) {
|
for (String agoPhrase : entry.getValue()) {
|
||||||
if (textualDateMatches(textualDate, agoPhrase)) {
|
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();
|
final Calendar calendarTime = getNow();
|
||||||
boolean isApproximation = false;
|
boolean isApproximation = false;
|
||||||
|
|
||||||
switch (timeAgoUnit) {
|
switch (chronoUnit) {
|
||||||
case SECONDS:
|
case SECONDS:
|
||||||
calendarTime.add(Calendar.SECOND, -timeAgoAmount);
|
calendarTime.add(Calendar.SECOND, -timeAgoAmount);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.schabi.newpipe.extractor.timeago;
|
package org.schabi.newpipe.extractor.timeago;
|
||||||
|
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -16,7 +17,7 @@ public abstract class PatternsHolder {
|
||||||
private final Collection<String> months;
|
private final Collection<String> months;
|
||||||
private final Collection<String> years;
|
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,
|
protected PatternsHolder(String wordSeparator, Collection<String> seconds, Collection<String> minutes,
|
||||||
Collection<String> hours, Collection<String> days,
|
Collection<String> hours, Collection<String> days,
|
||||||
|
@ -69,30 +70,25 @@ public abstract class PatternsHolder {
|
||||||
return years;
|
return years;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<TimeAgoUnit, Map<String, Integer>> specialCases() {
|
public Map<ChronoUnit, Map<String, Integer>> specialCases() {
|
||||||
return specialCases;
|
return specialCases;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void putSpecialCase(TimeAgoUnit unit, String caseText, int caseAmount) {
|
protected void putSpecialCase(ChronoUnit unit, String caseText, int caseAmount) {
|
||||||
Map<String, Integer> item = specialCases.get(unit);
|
Map<String, Integer> item = specialCases.computeIfAbsent(unit, k -> new LinkedHashMap<>());
|
||||||
|
|
||||||
if (item == null) {
|
|
||||||
item = new LinkedHashMap<>();
|
|
||||||
specialCases.put(unit, item);
|
|
||||||
}
|
|
||||||
|
|
||||||
item.put(caseText, caseAmount);
|
item.put(caseText, caseAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<TimeAgoUnit, Collection<String>> asMap() {
|
public Map<ChronoUnit, Collection<String>> asMap() {
|
||||||
final Map<TimeAgoUnit, Collection<String>> returnMap = new LinkedHashMap<>();
|
final Map<ChronoUnit, Collection<String>> returnMap = new LinkedHashMap<>();
|
||||||
returnMap.put(TimeAgoUnit.SECONDS, seconds());
|
returnMap.put(ChronoUnit.SECONDS, seconds());
|
||||||
returnMap.put(TimeAgoUnit.MINUTES, minutes());
|
returnMap.put(ChronoUnit.MINUTES, minutes());
|
||||||
returnMap.put(TimeAgoUnit.HOURS, hours());
|
returnMap.put(ChronoUnit.HOURS, hours());
|
||||||
returnMap.put(TimeAgoUnit.DAYS, days());
|
returnMap.put(ChronoUnit.DAYS, days());
|
||||||
returnMap.put(TimeAgoUnit.WEEKS, weeks());
|
returnMap.put(ChronoUnit.WEEKS, weeks());
|
||||||
returnMap.put(TimeAgoUnit.MONTHS, months());
|
returnMap.put(ChronoUnit.MONTHS, months());
|
||||||
returnMap.put(TimeAgoUnit.YEARS, years());
|
returnMap.put(ChronoUnit.YEARS, years());
|
||||||
|
|
||||||
return returnMap;
|
return returnMap;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
package org.schabi.newpipe.extractor.timeago;
|
|
||||||
|
|
||||||
public enum TimeAgoUnit {
|
|
||||||
SECONDS,
|
|
||||||
MINUTES,
|
|
||||||
HOURS,
|
|
||||||
DAYS,
|
|
||||||
WEEKS,
|
|
||||||
MONTHS,
|
|
||||||
YEARS
|
|
||||||
}
|
|
|
@ -5,7 +5,8 @@
|
||||||
package org.schabi.newpipe.extractor.timeago.patterns;
|
package org.schabi.newpipe.extractor.timeago.patterns;
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
|
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
|
||||||
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
|
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
public class iw extends PatternsHolder {
|
public class iw extends PatternsHolder {
|
||||||
private static final String WORD_SEPARATOR = " ";
|
private static final String WORD_SEPARATOR = " ";
|
||||||
|
@ -26,10 +27,10 @@ public class iw extends PatternsHolder {
|
||||||
|
|
||||||
private iw() {
|
private iw() {
|
||||||
super(WORD_SEPARATOR, SECONDS, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS);
|
super(WORD_SEPARATOR, SECONDS, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS);
|
||||||
putSpecialCase(TimeAgoUnit.HOURS, "שעתיים", 2);
|
putSpecialCase(ChronoUnit.HOURS, "שעתיים", 2);
|
||||||
putSpecialCase(TimeAgoUnit.DAYS, "יומיים", 2);
|
putSpecialCase(ChronoUnit.DAYS, "יומיים", 2);
|
||||||
putSpecialCase(TimeAgoUnit.WEEKS, "שבועיים", 2);
|
putSpecialCase(ChronoUnit.WEEKS, "שבועיים", 2);
|
||||||
putSpecialCase(TimeAgoUnit.MONTHS, "חודשיים", 2);
|
putSpecialCase(ChronoUnit.MONTHS, "חודשיים", 2);
|
||||||
putSpecialCase(TimeAgoUnit.YEARS, "שנתיים", 2);
|
putSpecialCase(ChronoUnit.YEARS, "שנתיים", 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue