Package adams.core
Class SecondFormat
- java.lang.Object
-
- adams.core.SecondFormat
-
- All Implemented Interfaces:
Serializable
public class SecondFormat extends Object implements Serializable
Formatting class for turning byte amounts into kb, mb, etc.
Format: {t|T}[.N]{s|m|h|d|w}
- b|B
"t" outputs the amount without thousand separators, "T" includes them. - .N
Prints "N" decimal places - S|M|H|D|W
Specifies the unit to use: s=seconds, m=minutes, h=hours, d=days, w=weeks
Lower case does not add a specifier like "ms", upper case does.
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SecondFormat.Unit
The available units.
-
Field Summary
Fields Modifier and Type Field Description protected boolean
m_AddUnit
whether to add a unit specifier at the end.protected String
m_Format
the format string.protected int
m_NumDecimals
the number of decimals to use.protected SecondFormat.Unit
m_Unit
the conversion unit.protected boolean
m_UseThousandSeparators
whether to use thousand separators or not.
-
Constructor Summary
Constructors Constructor Description SecondFormat(String format)
Initializes the formatter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
format(double seconds)
Formats the given second amount according to the format.String
format(long seconds)
Formats the given second amount according to the format.String
getFormat()
Returns the format being used.static String
insertThousandSeparators(String number)
Inserts thousand separators into the number string (integer or float).boolean
isValid(String format)
Tests the format.protected boolean
parseFormat(String format, boolean justTest)
Parses the format.static String
toBestFit(double seconds, int decimals)
Turns the number of seconds in the shortest representation (sec, min, ...).static String
toDays(double seconds, int decimals)
Turns the number of seconds into days.static String
toHours(double seconds, int decimals)
Turns the number of seconds into hours.static String
toMinutes(double seconds, int decimals)
Turns the number of seconds into minutes.static String
toMixed(double seconds)
Turns the number of seconds into a mixed representation (1w 2d 3h 4m 5s).String
toString()
Returns a short string description.static String
toWeeks(double seconds, int decimals)
Turns the number of seconds into weeks.
-
-
-
Field Detail
-
m_Format
protected String m_Format
the format string.
-
m_UseThousandSeparators
protected boolean m_UseThousandSeparators
whether to use thousand separators or not.
-
m_NumDecimals
protected int m_NumDecimals
the number of decimals to use.
-
m_Unit
protected SecondFormat.Unit m_Unit
the conversion unit.
-
m_AddUnit
protected boolean m_AddUnit
whether to add a unit specifier at the end.
-
-
Constructor Detail
-
SecondFormat
public SecondFormat(String format)
Initializes the formatter.- Parameters:
format
- the format to use
-
-
Method Detail
-
parseFormat
protected boolean parseFormat(String format, boolean justTest)
Parses the format.- Parameters:
format
- the format to parsejustTest
- if true then parameters derived won't be set
-
isValid
public boolean isValid(String format)
Tests the format.- Parameters:
format
- the format to test- Returns:
- true if valid
-
getFormat
public String getFormat()
Returns the format being used.- Returns:
- the format, null if invalid one provided
-
format
public String format(long seconds)
Formats the given second amount according to the format.- Parameters:
seconds
- the amount to format- Returns:
- the formatted amount
-
format
public String format(double seconds)
Formats the given second amount according to the format.- Parameters:
seconds
- the amount to format- Returns:
- the formatted amount
-
toString
public String toString()
Returns a short string description.
-
insertThousandSeparators
public static String insertThousandSeparators(String number)
Inserts thousand separators into the number string (integer or float).- Parameters:
number
- the string to inject with separators- Returns:
- the processed string
-
toMinutes
public static String toMinutes(double seconds, int decimals)
Turns the number of seconds into minutes.- Parameters:
seconds
- the number of seconds to formatdecimals
- the number of decimals after the decimal point- Returns:
- the minute string
-
toHours
public static String toHours(double seconds, int decimals)
Turns the number of seconds into hours.- Parameters:
seconds
- the number of seconds to formatdecimals
- the number of decimals after the decimal point- Returns:
- the hour string
-
toDays
public static String toDays(double seconds, int decimals)
Turns the number of seconds into days.- Parameters:
seconds
- the number of seconds to formatdecimals
- the number of decimals after the decimal point- Returns:
- the day string
-
toWeeks
public static String toWeeks(double seconds, int decimals)
Turns the number of seconds into weeks.- Parameters:
seconds
- the number of seconds to formatdecimals
- the number of decimals after the decimal point- Returns:
- the week string
-
toBestFit
public static String toBestFit(double seconds, int decimals)
Turns the number of seconds in the shortest representation (sec, min, ...).- Parameters:
seconds
- the seconds to convertdecimals
- the number of decimals after the decimal point- Returns:
- the optimal number string
-
toMixed
public static String toMixed(double seconds)
Turns the number of seconds into a mixed representation (1w 2d 3h 4m 5s).- Parameters:
seconds
- the seconds to convert- Returns:
- the mixed string
-
-