java.lang.Object
com.winterhavenmc.library.messagebuilder.pipeline.formatters.duration.Time4jDurationFormatter
All Implemented Interfaces:
DurationFormatter

public final class Time4jDurationFormatter extends Object implements DurationFormatter
A DurationFormatter implementation that uses the Time4J library's PrettyTime to render Duration values as human-readable, localized strings.

This formatter decomposes the Duration into calendar and clock units and uses Time4J’s formatting capabilities to generate grammatically correct, locale-aware strings with wide text style. For example, a duration of 2 days, 3 hours, and 4 minutes might be rendered as: "2 days, 3 hours, and 4 minutes" in English, or a culturally appropriate equivalent in other locales.

If the provided Duration or ChronoUnit is null, this class falls back to a default duration of 0 and a default lower bound of ChronoUnit.MINUTES, respectively, with warnings logged through the configured logging strategy.

The lowerBound determines the level of truncation applied before formatting. For example, a lower bound of ChronoUnit.MINUTES would discard all precision below minutes.

See Also:
  • Constructor Details

    • Time4jDurationFormatter

      public Time4jDurationFormatter(LocaleProvider localeProvider)
      Constructs a Time4jDurationFormatter with the provided LocaleProvider.
      Parameters:
      localeProvider - supplies the Locale to use for formatting
  • Method Details

    • format

      public String format(Duration duration, ChronoUnit lowerBound)
      Formats a Duration using Time4J's PrettyTime engine. The duration is truncated to the given lowerBound, and formatted into a locale-sensitive string using TextWidth.WIDE.

      The method maps Java Duration to Time4J's Duration classes by separating calendar and clock units before composing them for formatting.

      Specified by:
      format in interface DurationFormatter
      Parameters:
      duration - the duration to format; null defaults to Duration.ZERO
      lowerBound - the lowest time unit to preserve; null defaults to ChronoUnit.MINUTES
      Returns:
      a localized, human-friendly string representation of the duration