Interface Durationable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Represents an object that exposes a
Duration
, enabling it to participate
in macro substitution using duration-based placeholders.
Objects implementing this interface can provide a duration value via getDuration()
,
and automatically extract localized string representations of that duration through the
extractDuration(MacroKey, ChronoUnit, AdapterContextContainer)
method.
This interface supports usage in message templates via the [OBJECT.DURATION
} macro.
Formatting is handled by the DurationFormatter
implementation provided in the AdapterContextContainer
.
Three utility methods are provided:
extractDuration(MacroKey, ChronoUnit, AdapterContextContainer)
— generates aMacroStringMap
with the localized string representation of the durationformatDuration(Duration, ChronoUnit, DurationFormatter)
— formats a duration using the configuredDurationFormatter
durationUntil(Instant)
— helper for computing theDuration
until a futureInstant
Durations are automatically classified using the DurationType
enum into:
NORMAL
— durations above 1 unitLESS_THAN
— durations under the specifiedChronoUnit
lower boundUNLIMITED
— negative durations signifying no expiration or restriction
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Duration
durationUntil
(Instant instant) Computes the duration from the current moment until the givenInstant
.default MacroStringMap
extractDuration
(MacroKey baseKey, ChronoUnit lowerBound, AdapterContextContainer ctx) Extracts aMacroStringMap
containing a single entry mapping the providedMacroKey
(with.DURATION
appended) to a localized string representation of this object's duration.formatDuration
(Duration duration, ChronoUnit lowerBound, DurationFormatter durationFormatter) Uses the configuredDurationFormatter
to format the givenDuration
according to the specified precision.Returns the duration value associated with this object.
-
Method Details
-
getDuration
Duration getDuration()Returns the duration value associated with this object.- Returns:
- the duration, or
null
if unavailable
-
extractDuration
default MacroStringMap extractDuration(MacroKey baseKey, ChronoUnit lowerBound, AdapterContextContainer ctx) Extracts aMacroStringMap
containing a single entry mapping the providedMacroKey
(with.DURATION
appended) to a localized string representation of this object's duration.- Parameters:
baseKey
- the top-level key to whichDURATION
will be appendedlowerBound
- the smallestChronoUnit
to display (e.g.,MINUTES
,SECONDS
)ctx
- context container providing theDurationFormatter
- Returns:
- a
MacroStringMap
containing the extracted duration string; empty if the key could not be constructed
-
formatDuration
static Optional<String> formatDuration(Duration duration, ChronoUnit lowerBound, DurationFormatter durationFormatter) Uses the configuredDurationFormatter
to format the givenDuration
according to the specified precision.- Parameters:
duration
- the duration to formatlowerBound
- the lowest precision unit to include in the result (e.g., seconds, minutes)durationFormatter
- the formatter to use for localization- Returns:
- an
Optional<String>
containing the formatted result, or empty if input isnull
-
durationUntil
Computes the duration from the current moment until the givenInstant
.- Parameters:
instant
- a future point in time- Returns:
- the computed duration, or
Duration.ZERO
if the instant isnull
-