-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add TemporalAmountParser for time manipulations
- Loading branch information
Showing
4 changed files
with
421 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
commons-shared/src/main/java/space/bxteam/commons/time/DurationParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package space.bxteam.commons.time; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Map; | ||
|
||
public class DurationParser extends TemporalAmountParser<Duration> { | ||
public DurationParser() { | ||
super(LocalDateTimeProvider.now()); | ||
} | ||
|
||
public DurationParser(LocalDateTimeProvider localDateTimeProvider) { | ||
super(localDateTimeProvider); | ||
} | ||
|
||
private DurationParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { | ||
super(units, baseForTimeEstimation); | ||
} | ||
|
||
public static final TemporalAmountParser<Duration> TIME_UNITS = new DurationParser() | ||
.withUnit("ms", ChronoUnit.MILLIS) | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS); | ||
|
||
public static final TemporalAmountParser<Duration> DATE_TIME_UNITS = new DurationParser() | ||
.withUnit("ns", ChronoUnit.NANOS) | ||
.withUnit("us", ChronoUnit.MICROS) | ||
.withUnit("ms", ChronoUnit.MILLIS) | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS) | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS); | ||
|
||
@Override | ||
protected TemporalAmountParser<Duration> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { | ||
return new DurationParser(units, baseForTimeEstimation); | ||
} | ||
|
||
@Override | ||
protected Duration plus(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount, TemporalEntry temporalEntry) { | ||
if (temporalEntry.unit().isDurationEstimated()) { | ||
LocalDateTime baseDateTime = baseForTimeEstimation.get(); | ||
LocalDateTime estimatedDateTime = baseDateTime.plus(temporalEntry.count(), temporalEntry.unit()); | ||
Duration estimatedDuration = Duration.between(baseDateTime, estimatedDateTime); | ||
|
||
return temporalAmount.plus(estimatedDuration); | ||
} | ||
|
||
return temporalAmount.plus(temporalEntry.count(), temporalEntry.unit()); | ||
} | ||
|
||
|
||
@Override | ||
protected Duration negate(Duration temporalAmount) { | ||
return temporalAmount.negated(); | ||
} | ||
|
||
@Override | ||
protected Duration getZero() { | ||
return Duration.ZERO; | ||
} | ||
|
||
@Override | ||
protected Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount) { | ||
return temporalAmount; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
commons-shared/src/main/java/space/bxteam/commons/time/PeriodParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package space.bxteam.commons.time; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.time.Period; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Map; | ||
|
||
public class PeriodParser extends TemporalAmountParser<Period> { | ||
public PeriodParser() { | ||
super(LocalDateTimeProvider.now()); | ||
} | ||
|
||
public PeriodParser(LocalDateTimeProvider baseForTimeEstimation) { | ||
super(baseForTimeEstimation); | ||
} | ||
|
||
private PeriodParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { | ||
super(units, baseForTimeEstimation); | ||
} | ||
|
||
public static final TemporalAmountParser<Period> DATE_UNITS = new PeriodParser() | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS); | ||
|
||
@Override | ||
protected TemporalAmountParser<Period> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { | ||
return new PeriodParser(units, baseForTimeEstimation); | ||
} | ||
|
||
@Override | ||
protected Period plus(LocalDateTimeProvider baseForTimeEstimation, Period temporalAmount, TemporalEntry temporalEntry) { | ||
int count = (int) temporalEntry.count(); | ||
ChronoUnit unit = temporalEntry.unit(); | ||
|
||
if (unit == ChronoUnit.DAYS) { | ||
return temporalAmount.plus(Period.ofDays(count)); | ||
} | ||
|
||
if (unit == ChronoUnit.WEEKS) { | ||
return temporalAmount.plus(Period.ofWeeks(count)); | ||
} | ||
|
||
if (unit == ChronoUnit.MONTHS) { | ||
return temporalAmount.plus(Period.ofMonths(count)); | ||
} | ||
|
||
if (unit == ChronoUnit.YEARS) { | ||
return temporalAmount.plus(Period.ofYears(count)); | ||
} | ||
|
||
throw new IllegalArgumentException("Unsupported unit: " + unit); | ||
} | ||
|
||
@Override | ||
protected Period negate(Period temporalAmount) { | ||
return temporalAmount.negated(); | ||
} | ||
|
||
@Override | ||
protected Period getZero() { | ||
return Period.ZERO; | ||
} | ||
|
||
@Override | ||
protected Duration toDuration(LocalDateTimeProvider basisEstimation, Period period) { | ||
LocalDateTime localDate = basisEstimation.get(); | ||
LocalDateTime estimatedDate = localDate.plus(period); | ||
|
||
return Duration.between(localDate, estimatedDate); | ||
} | ||
} |
Oops, something went wrong.