Module org.tquadrat.foundation.util
Class DurationStringConverter
java.lang.Object
org.tquadrat.foundation.util.stringconverter.DurationStringConverter
- All Implemented Interfaces:
Serializable,StringConverter<Duration>
@ClassVersion(sourceVersion="$Id: DurationStringConverter.java 1060 2023-09-24 19:21:40Z tquadrat $")
@API(status=STABLE,
since="0.0.7")
public final class DurationStringConverter
extends Object
implements StringConverter<Duration>
An implementation of
While the method
The string starts with an optional sign, denoted by the ASCII negative ('-') or positive ('+') symbol. If negative, the whole period is negated.
The ASCII letter "P" is next in upper or lower case. There are then four sections, each consisting of a number and a suffix.
The sections have suffixes in ASCII of "D", "H", "M" and "S" for days, hours, minutes and seconds, accepted in upper or lower case.
The suffixes must occur in order. The ASCII letter "T" must occur before the first occurrence, if any, of an hour, minute or second section.
At least one of the four sections must be present, and if "T" is present there must be at least one section after the "T".
The number part of each section must consist of one or more ASCII digits. The number may be prefixed by the ASCII negative or positive symbol. The number of days, hours and minutes must parse to a
StringConverter
for
Duration
values.While the method
toString(Duration)
simply uses
Duration.toString(),
the method
fromString(CharSequence)
uses
Duration.parse(CharSequence)
to create the Duration instance for the given value. The formats
accepted are based on the ISO-8601 duration format PnDTnHnMn.nS
with days considered to be exactly 24 hours.The string starts with an optional sign, denoted by the ASCII negative ('-') or positive ('+') symbol. If negative, the whole period is negated.
The ASCII letter "P" is next in upper or lower case. There are then four sections, each consisting of a number and a suffix.
The sections have suffixes in ASCII of "D", "H", "M" and "S" for days, hours, minutes and seconds, accepted in upper or lower case.
The suffixes must occur in order. The ASCII letter "T" must occur before the first occurrence, if any, of an hour, minute or second section.
At least one of the four sections must be present, and if "T" is present there must be at least one section after the "T".
The number part of each section must consist of one or more ASCII digits. The number may be prefixed by the ASCII negative or positive symbol. The number of days, hours and minutes must parse to a
long.
The number of seconds must parse to a long with optional fraction.
The decimal point may be either a dot or a comma. The fractional part may
have from zero to 9 digits.- Note:
-
- The leading plus/minus sign, and negative values for other units are not originally part of the ISO-8601 standard.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: DurationStringConverter.java 1060 2023-09-24 19:21:40Z tquadrat $
- Since:
- 0.0.7
- See Also:
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.util.stringconverter.DurationStringConverter"
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationStringConverterAn instance of this class.static final StringThe error message for an invalid duration String: "\'%1$s\' cannot be parsed as a valid duration".Fields inherited from interface org.tquadrat.foundation.lang.StringConverter
METHOD_NAME_GetSubjectClass, METHOD_NAME_Provider -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofDurationStringConverter. -
Method Summary
Modifier and TypeMethodDescriptionfinal DurationfromString(CharSequence source) static final DurationStringConverterprovider()This method is used by theServiceLoaderto obtain the instance for thisStringConverterimplementation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.tquadrat.foundation.lang.StringConverter
toString
-
Field Details
-
MSG_InvalidDuration
The error message for an invalid duration String: "\'%1$s\' cannot be parsed as a valid duration".- See Also:
-
INSTANCE
An instance of this class.
-
-
Constructor Details
-
DurationStringConverter
public DurationStringConverter()Creates a new instance ofDurationStringConverter.
-
-
Method Details
-
fromString
- Specified by:
fromStringin interfaceStringConverter<Duration>- Throws:
IllegalArgumentException
-
provider
This method is used by theServiceLoaderto obtain the instance for thisStringConverterimplementation.- Returns:
- The instance for this
StringConverterimplementation.
-
