Class DurationFormat
- java.lang.Object
-
- com.pervasivecode.utils.time.DurationFormat
-
@Immutable public abstract class DurationFormat extends Object
This object holds configuration information for aDurationFormatter
instance.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DurationFormat.Builder
This object will build aDurationFormat
instance.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static DurationFormat.Builder
builder()
Create an object that will build aDurationFormatter
instance.static DurationFormat.Builder
builder(DurationFormat format)
Create an object that will build aDurationFormatter
instance.abstract ChronoUnit
largestUnit()
The largest allowable unit of time to use when formatting theDuration
value.abstract NumberFormat
numberFormat()
The NumberFormat which the DurationFormatter will use when formatting individual parts of Duration values.abstract Integer
numFractionalDigits()
Set the maximum number of digits to use to represent a fractional amount of the smallest allowed unit.abstract String
partDelimiter()
The delimiter to use to separate each part of the formatted value.abstract DurationRemainderHandling
remainderHandling()
Specify how values that are smaller than the smallest allowed unit with its fractional digits are handled.abstract ChronoUnit
smallestUnit()
The smallest allowable unit of time to use when formatting theDuration
value.abstract Set<ChronoUnit>
suppressedUnits()
Get the set of units that should not be included in the list of allowed units, even if they fall within the range described by the smallestUnit and largestUnit properties.abstract ChronoUnit
unitForZeroDuration()
The unit of time to use when formatting aDuration
value of zero.List<ChronoUnit>
units()
Get a list of units which are greater than or equal to smallestUnit and less than or equal to largestUnit, without the units specified bysuppressedUnits()
.abstract UnitSuffixProvider
unitSuffixProvider()
The provider of unit suffixes that theDurationFormatter
will use when formatting each part of theDuration
.
-
-
-
Method Detail
-
unitSuffixProvider
public abstract UnitSuffixProvider unitSuffixProvider()
The provider of unit suffixes that theDurationFormatter
will use when formatting each part of theDuration
.- Returns:
- the
UnitSuffixProvider
.
-
partDelimiter
public abstract String partDelimiter()
The delimiter to use to separate each part of the formatted value.- Returns:
- the formatted-part delimiter.
-
numberFormat
public abstract NumberFormat numberFormat()
The NumberFormat which the DurationFormatter will use when formatting individual parts of Duration values.Note: the rounding mode and number of fraction digits properties of this NumberFormat instance will be ignored.
Instead, the DurationFormatter will use a
minimumFractionDigits
of zero and amaximumFractionDigits
value of zero for all parts of the value other than the part corresponding to thesmallestUnit
. For the value corresponding to thesmallestUnit
, themaximumFractionDigits
will be set tonumFractionalDigits()
, rounding mode will be set based onremainderHandling()
, andminimumFractionDigits
will be to zero.- Returns:
- the NumberFormat.
-
largestUnit
public abstract ChronoUnit largestUnit()
The largest allowable unit of time to use when formatting theDuration
value.- Returns:
- The largest allowable unit to use.
-
smallestUnit
public abstract ChronoUnit smallestUnit()
The smallest allowable unit of time to use when formatting theDuration
value. Leading and trailing parts of the formatted value with a quantity of zero will not be shown.- Returns:
- The smallest allowable unit to use.
-
unitForZeroDuration
public abstract ChronoUnit unitForZeroDuration()
The unit of time to use when formatting aDuration
value of zero.- Returns:
- The largest allowable unit to use.
-
suppressedUnits
public abstract Set<ChronoUnit> suppressedUnits()
Get the set of units that should not be included in the list of allowed units, even if they fall within the range described by the smallestUnit and largestUnit properties.- Returns:
- The set of units which should not be used when formatting duration values.
-
numFractionalDigits
public abstract Integer numFractionalDigits()
Set the maximum number of digits to use to represent a fractional amount of the smallest allowed unit. This value is used instead of the value ofNumberFormat.getMaximumIntegerDigits()
innumberFormat()
.Example: 36 hours formatted in units of days with a numFractionalDigits value of 2 will be represented as 1.5 days.
- Returns:
- The maximum number of fractional digits to use when formatting the value of the smallest allowed unit.
-
remainderHandling
public abstract DurationRemainderHandling remainderHandling()
Specify how values that are smaller than the smallest allowed unit with its fractional digits are handled.Example: A duration of 19 microseconds is formatted in units of hours, minutes, seconds, and milliseconds, with a numFractionalDigits value of 2. 19 microseconds is equal to 0.019 milliseconds, but using only two digits to the right of the decimal, should that be formatted as 0.01 or 0.02?
DurationRemainderHandling.TRUNCATE
will drop the 0.009 milliseconds;DurationRemainderHandling.ROUND_HALF_EVEN
will round the 0.009 milliseconds up to 0.01, for a formatted value of 0.02 milliseconds.- Returns:
- The DurationRemainderHandling value that will control how rounding is performed.
-
units
@Memoized public List<ChronoUnit> units()
Get a list of units which are greater than or equal to smallestUnit and less than or equal to largestUnit, without the units specified bysuppressedUnits()
. These units are the ones that will be treated as available for formatting values. Units not in this list will not be used by theDurationFormatter
.- Returns:
- The list of available units for formatting.
-
builder
public static DurationFormat.Builder builder()
Create an object that will build aDurationFormatter
instance.This builder is mostly unconfigured, except for the following default values:
- suppressedUnits = { ChronoUnit.HALF_DAYS }
- remainderHandling = DurationRemainderHandling.TRUNCATE
- Returns:
- a new
DurationFormat.Builder
instance.
-
builder
public static DurationFormat.Builder builder(DurationFormat format)
Create an object that will build aDurationFormatter
instance.This new builder is configured with the values taken from the
format
parameter. In effect, it is a mutable copy of that instance.- Parameters:
format
- The format to use to configure the new Builder instance.- Returns:
- a new
DurationFormat.Builder
instance, with the same values as the instance passed via theformat
parameter.
-
-