Package org.cip4.jdflib.util
Class JDFDuration
- java.lang.Object
-
- org.cip4.jdflib.util.JDFDuration
-
- All Implemented Interfaces:
java.lang.Comparable<JDFDuration>
public class JDFDuration extends java.lang.Object implements java.lang.Comparable<JDFDuration>
-
-
Constructor Summary
Constructors Constructor Description JDFDuration()Allocates aJDFDurationobject and initializes it with 0JDFDuration(double s)Allocates aJDFDurationobject and initializes it with 's'JDFDuration(int s, int m, int h, int d)Allocates aJDFDurationobject and initializes it with days, hours, minutes, secondsJDFDuration(long s)Allocates aJDFDurationobject and initializes it with 's'JDFDuration(java.lang.String strDuration)Allocates aJDFDurationobject and initializes it with a value ofstrDuration, represented as a formatted duration string.JDFDuration(JDFDate start, JDFDate end)creates a duration from two dates; may be negative if start later endJDFDuration(JDFDuration d)Makes a copy of theJDFDurationobject 'd'
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JDFDurationaddOffset(int seconds, int minutes, int hours, int days)add a given offset to this
note: multiple calls stackdoubleaddSeconds(double seconds)add seconds to a durationintcompareTo(JDFDuration arg0)static JDFDurationcreateDuration(java.lang.String duration)does some heuristics to create a duration if duration is purely numeric, we guess daysbooleanequals(java.lang.Object other)Compares two JDFDuration objects for equality.
The result istrueif and only if the argument is notnulland is aJDFDurationobject that represents the same duration, as this object.longgetDuration()the duration in secondsjava.lang.StringgetDurationISO()Format and return the duration set by 'setDuration(int i)' or 'setDurationString(String a_aDuration)' as an ISO conforming String.
For example: 'P1Y2M3DT10H30M'longgetDurationMillis()the duration in millisecondsinthashCode()hashCode: complements equals() to fulfill the equals/hashCode contractbooleanisLonger(JDFDuration x)isLess - tests if the duration of this JDFDuration is longer than the duration of the specified JDFDuration.booleanisShorter(JDFDuration x)isShorter - tests if the duration of this JDFDuration is less than the duration of the specified JDFDuration.voidsetDuration(double seconds)setDuration: sets a duration forthisin seconds, including fractions.voidsetDuration(long seconds)setDuration: sets a duration forthisin seconds.booleansetDurationISO(java.lang.String a_aDuration)Set a duration.java.lang.StringtoString()for debug purposes
-
-
-
Constructor Detail
-
JDFDuration
public JDFDuration()
Allocates aJDFDurationobject and initializes it with 0
-
JDFDuration
public JDFDuration(JDFDuration d)
Makes a copy of theJDFDurationobject 'd'- Parameters:
d- the duration
-
JDFDuration
public JDFDuration(JDFDate start, JDFDate end)
creates a duration from two dates; may be negative if start later end- Parameters:
start- the starting pointend- the end point
-
JDFDuration
public JDFDuration(double s)
Allocates aJDFDurationobject and initializes it with 's'- Parameters:
s- duration in seconds s may be fractional
-
JDFDuration
public JDFDuration(long s)
Allocates aJDFDurationobject and initializes it with 's'- Parameters:
s- duration in seconds s
-
JDFDuration
public JDFDuration(int s, int m, int h, int d)Allocates aJDFDurationobject and initializes it with days, hours, minutes, seconds- Parameters:
d-h-m-s-
-
JDFDuration
public JDFDuration(java.lang.String strDuration) throws java.util.zip.DataFormatExceptionAllocates aJDFDurationobject and initializes it with a value ofstrDuration, represented as a formatted duration string.
Duration examples:- "P1Y2M3DT10H30M"
- "P8MT12M"
Durations with overflows, e.g. P13M (13 Months) are also handled and correctly output, in this case P1Y1M- Parameters:
strDuration- - formatted duration- Throws:
java.util.zip.DataFormatException- if strDuration is not a valid string representation of JDFDuration
-
-
Method Detail
-
createDuration
public static JDFDuration createDuration(java.lang.String duration)
does some heuristics to create a duration if duration is purely numeric, we guess days- Parameters:
duration-- Returns:
-
addSeconds
public double addSeconds(double seconds)
add seconds to a duration- Parameters:
seconds- number of seconds to add- Returns:
- the new duration in seconds
-
addOffset
public JDFDuration addOffset(int seconds, int minutes, int hours, int days)
add a given offset to this
note: multiple calls stack- Parameters:
seconds- seconds to add to thisminutes- minutes to add to thishours- hours to add to thisdays- days to add to this
-
toString
public java.lang.String toString()
for debug purposes- Overrides:
toStringin classjava.lang.Object- Returns:
- Object informations
-
getDurationISO
public java.lang.String getDurationISO()
Format and return the duration set by 'setDuration(int i)' or 'setDurationString(String a_aDuration)' as an ISO conforming String.
For example: 'P1Y2M3DT10H30M'- Returns:
- String - the duration formatted as an ISO 8601 conforming String if duration is '0' return value is 'PT00M'
-
setDurationISO
public boolean setDurationISO(java.lang.String a_aDuration)
Set a duration. Durations are not bound to time or date and can be set independently- Parameters:
a_aDuration- formatted duration string 'P1Y2M3DT10H30M'- Returns:
- true - the duration was set
false - the duration was not set, because a NumberFormatException was thrown (-> parseInt())
-
setDuration
public void setDuration(long seconds)
setDuration: sets a duration forthisin seconds. This duration is used in multiple classes of the JDF (e.g. Heating time).- Parameters:
seconds- the duration in seconds.
-
setDuration
public void setDuration(double seconds)
setDuration: sets a duration forthisin seconds, including fractions. This duration is used in multiple classes of the JDF (e.g. Heating time).- Parameters:
seconds- the duration in seconds.
-
getDuration
public long getDuration()
the duration in seconds- Returns:
- duration in seconds; '0' default
-
getDurationMillis
public long getDurationMillis()
the duration in milliseconds- Returns:
- duration in seconds; '0' default
-
isLonger
public boolean isLonger(JDFDuration x)
isLess - tests if the duration of this JDFDuration is longer than the duration of the specified JDFDuration. Compares the integer durations, thus -PT15S is shorter than -PT5S- Parameters:
x- the JDFDuration object to compare tothis- Returns:
- boolean - true if the duration of this JDFDuration is longer than the duration of the JDFDuration 'x'.
-
isShorter
public boolean isShorter(JDFDuration x)
isShorter - tests if the duration of this JDFDuration is less than the duration of the specified JDFDuration. Compares the integer durations, thus -PT15S is shorter than -PT5S- Parameters:
x- the JDFDuration object to compare tothis- Returns:
- boolean - true if the duration of this JDFDuration is shorter than the duration of the JDFDuration 'x'.
-
equals
public boolean equals(java.lang.Object other)
Compares two JDFDuration objects for equality.
The result istrueif and only if the argument is notnulland is aJDFDurationobject that represents the same duration, as this object.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
hashCode: complements equals() to fulfill the equals/hashCode contract- Overrides:
hashCodein classjava.lang.Object
-
compareTo
public int compareTo(JDFDuration arg0)
- Specified by:
compareToin interfacejava.lang.Comparable<JDFDuration>- Parameters:
arg0-- Returns:
- See Also:
Comparable.compareTo(java.lang.Object)
-
-