Class TemporalAmountParser<T extends TemporalAmount>

java.lang.Object
dev.rollczi.litecommands.time.TemporalAmountParser<T>
Type Parameters:
T - type of temporal amount (must be Duration or Period or subclass of TemporalAmount)
Direct Known Subclasses:
DurationParser, PeriodParser

public abstract class TemporalAmountParser<T extends TemporalAmount> extends Object
This class is used to parse temporal amount from string and format it to string. It can parse both Period and Duration.

It can parse both estimated and exact temporal amount. Estimated temporal amount is a temporal amount that is not exact. For example, if you want to parse 1 month, you can write 1mo or 30d. If you write 1mo, it can be estimated to 28, 29, 30 or 31 days. If you write 1y, it can be estimated to 365 or 366 days.

This class is immutable. You can create new instance using constructor PeriodParser() or DurationParser(). You can also use DurationParser.DATE_TIME_UNITS, PeriodParser.DATE_UNITS and DurationParser.TIME_UNITS. You can add new units using withUnit(String, ChronoUnit).

Use parse(String) to parse temporal amount from string. Use format(TemporalAmount) to format temporal amount to string.

  • Constructor Details

  • Method Details

    • withUnit

      public TemporalAmountParser<T> withUnit(String symbol, ChronoUnit chronoUnit)
    • withLocalDateTimeProvider

      public TemporalAmountParser<T> withLocalDateTimeProvider(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
    • clone

      protected abstract TemporalAmountParser<T> clone(Map<String,ChronoUnit> units, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
    • parse

      public T parse(String input)
      Parses the given string and returns the estimated temporal amount.]

      Examples:

      • 1ns - 1 nanosecond
      • 1us - 1 microsecond
      • 1ms - 1 millisecond
      • 1s - 1 second
      • 1m - 1 minute
      • 1h - 1 hour
      • 1d - 1 day
      • 1w - 1 week
      • 1mo - 1 month
      • 1y - 1 year
      • 1d2h - 1 day and 2 hours
      • 1d2h3m - 1 day, 2 hours and 3 minutes
      • 1d2h3m4s - 1 day, 2 hours, 3 minutes and 4 seconds
      • 1y2mo3w4d5h6m7s - 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds
      Parameters:
      input - the input to parse. Must not be null or empty. Must be in the format of <number><unit>. The string can not contain any characters that are not part of the number or the unit. The only exception is the minus sign, which is allowed at the start of the string. The unit must be one of the units that are configured for this parser. The number must be a valid number that can be parsed by Long.parseLong(String).
      Returns:
      the estimated temporal amount
      Throws:
      IllegalArgumentException - if the input is null or empty, if the input is not in the format of <number><unit>, if the unit is not a valid unit, if the number is not a valid number, if the number is decimal
    • plus

      protected abstract T plus(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount, TemporalAmountParser.TemporalEntry temporalEntry)
    • negate

      protected abstract T negate(T temporalAmount)
    • getZero

      protected abstract T getZero()
    • format

      public String format(T temporalAmount)
      Formats the given estimated temporal amount to a string.

      Examples:

      • Duration of 30 seconds: 30s
      • Duration of 25 hours: 1d1h
      • Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds: 1y2mo3w4d5h6m7s
      • Duration of 1 hours and 61 minutes: 2h1m
      • Past duration of 1 hours and 61 minutes: -2h1m
      • Period of 1 year, 2 months, 4 days: 1y2mo4d
      • Past period of 1 year, 2 months, 4 days: -1y2mo4d
      Parameters:
      temporalAmount - the temporal amount to format. Must not be null.
      Returns:
      the formatted string
    • toDuration

      protected abstract Duration toDuration(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount)