|
| 1 | +package com.eternalcode.commons.shared.time; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.time.LocalDateTime; |
| 5 | +import java.time.temporal.ChronoUnit; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +public class DurationParser extends TemporalAmountParser<Duration> { |
| 9 | + |
| 10 | + public static final TemporalAmountParser<Duration> TIME_UNITS = new DurationParser() |
| 11 | + .withUnit("ms", ChronoUnit.MILLIS) |
| 12 | + .withUnit("s", ChronoUnit.SECONDS) |
| 13 | + .withUnit("m", ChronoUnit.MINUTES) |
| 14 | + .withUnit("h", ChronoUnit.HOURS); |
| 15 | + |
| 16 | + public static final TemporalAmountParser<Duration> DATE_TIME_UNITS = new DurationParser() |
| 17 | + .withUnit("ns", ChronoUnit.NANOS) |
| 18 | + .withUnit("us", ChronoUnit.MICROS) |
| 19 | + .withUnit("ms", ChronoUnit.MILLIS) |
| 20 | + .withUnit("s", ChronoUnit.SECONDS) |
| 21 | + .withUnit("m", ChronoUnit.MINUTES) |
| 22 | + .withUnit("h", ChronoUnit.HOURS) |
| 23 | + .withUnit("d", ChronoUnit.DAYS) |
| 24 | + .withUnit("w", ChronoUnit.WEEKS) |
| 25 | + .withUnit("mo", ChronoUnit.MONTHS) |
| 26 | + .withUnit("y", ChronoUnit.YEARS); |
| 27 | + |
| 28 | + public DurationParser() { |
| 29 | + super(LocalDateTimeProvider.now()); |
| 30 | + } |
| 31 | + |
| 32 | + public DurationParser(LocalDateTimeProvider localDateTimeProvider) { |
| 33 | + super(localDateTimeProvider); |
| 34 | + } |
| 35 | + |
| 36 | + private DurationParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { |
| 37 | + super(units, baseForTimeEstimation); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected TemporalAmountParser<Duration> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) { |
| 42 | + return new DurationParser(units, baseForTimeEstimation); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Duration plus(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount, TemporalEntry temporalEntry) { |
| 47 | + if (temporalEntry.getUnit().isDurationEstimated()) { |
| 48 | + LocalDateTime baseDateTime = baseForTimeEstimation.get(); |
| 49 | + LocalDateTime estimatedDateTime = baseDateTime.plus(temporalEntry.getCount(), temporalEntry.getUnit()); |
| 50 | + Duration estimatedDuration = Duration.between(baseDateTime, estimatedDateTime); |
| 51 | + |
| 52 | + return temporalAmount.plus(estimatedDuration); |
| 53 | + } |
| 54 | + |
| 55 | + return temporalAmount.plus(temporalEntry.getCount(), temporalEntry.getUnit()); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Duration negate(Duration temporalAmount) { |
| 61 | + return temporalAmount.negated(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected Duration getZero() { |
| 66 | + return Duration.ZERO; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount) { |
| 71 | + return temporalAmount; |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments