java.lang.Object
org.rrd4j.core.Util
Class defines various utility functions used in Rrd4j.
Nested Class Summary
Nested ClassesField Summary
Modifier and TypeFieldDescription(package private) static final ThreadLocal<NumberFormat> Per-threadNumberFormatapplyingPATTERNand a leading '+' for positives(package private) static final StringThe single date patterngetCalendar(String)accepts and names on failureprivate static longWhen the previous lap of the timer ended.static final doubleConstantMAX_DOUBLE=Double.MAX_VALUEstatic final longConstantMAX_LONG=Long.MAX_VALUEstatic final doubleConstantMIN_DOUBLE=-Double.MAX_VALUEstatic final longConstantMIN_LONG=-Long.MAX_VALUE(package private) static final StringDecimalFormat pattern giving the RRDTool-like "+1.234567E+02" rendering(package private) static final StringName of the directory created under the user home for demo graphsprivate static final PatternMatches a sprintf-style placeholder in a format string.Constructor Summary
ConstructorsMethod Summary
Public MethodsModifier and TypeMethodDescriptionstatic booleanequal(double x, double y) Compares two doubles but treats all NaNs as equal.static booleanfileExists(String filename) Checks if the file with the given file name existsstatic StringformatDouble(double x) Formats double as a string using exponential notation (RRDTool like).(package private) static StringformatDouble(double x, boolean forceExponents) Formats a double value as a string with default NaN string.(package private) static StringformatDouble(double x, String nanString, boolean forceExponents) Formats a double value as a string.static CalendargetCalendar(long timestamp) ReturnsCalendarobject for the given timestamp (in seconds, without milliseconds)static CalendargetCalendar(String timeStr) Creates Calendar object from a string.static CalendargetCalendar(Date date) ReturnsCalendarobject for the given Date objectstatic StringgetCanonicalPath(String path) Returns canonical file path for the given file pathstatic DategetDate(long timestamp) ReturnsDateobject for the given timestamp (in seconds, without milliseconds)static StringReturns file system separator string.static StringFunction used for debugging purposes and performance bottlenecks detection.static longgetLastModifiedTime(String file) Returns last modification time for the given file.(package private) static intgetMatchingArchiveIndex(RrdDb rrd1, int arcIndex, RrdDb rrd2) Looks up inrrd2the archive whose consolidation function and row count match archivearcIndexofrrd1(package private) static intgetMatchingDatasourceIndex(RrdDb rrd1, int dsIndex, RrdDb rrd2) Looks up inrrd2the datasource sharing the name of datasourcedsIndexofrrd1static StringReturns path to directory used for placement of Rrd4j demo graphs and creates it if necessary.static StringgetRrd4jDemoPath(String filename) Returns full path to the file stored in the demo directory of Rrd4jstatic StringReturns the root directory of the Rrd4j distribution.static longgetTime()Returns current timestamp in seconds (without milliseconds).static longJust an alias forgetTime()method.static longgetTimestamp(int year, int month, int day) Returns timestamp (unix epoch) for the given year, month and day.static longgetTimestamp(int year, int month, int day, int hour, int min) Returns timestamp (unix epoch) for the given year, month, day, hour and minute.static longgetTimestamp(String atStyleTimeSpec) Parses at-style time specification and returns the corresponding timestamp.static longgetTimestamp(Calendar gc) Returns timestamp (unix epoch) for the given Calendar objectstatic longgetTimestamp(Date date) Returns timestamp (unix epoch) for the given Date objectstatic long[]getTimestamps(String atStyleTimeSpec1, String atStyleTimeSpec2) Parses two related at-style time specifications and returns corresponding timestamps.(package private) static StringCreates a new empty temporary file with therrd4j_prefix and.tmpsuffixstatic StringReturns path to user's home directory.static booleanChecks if a string can be parsed as double.static doublemax(double[] values) Finds max value for an array of doubles (NaNs are ignored).static doublemax(double x, double y) Returns the greater of two double values, but treats NaN as the smallest possible value.static doublemin(double[] values) Finds min value for an array of doubles (NaNs are ignored).static doublemin(double x, double y) Returns the smaller of two double values, but treats NaN as the greatest possible value.static longnormalize(long timestamp, long step) Rounds the given timestamp to the nearest whole "step".static booleanparseBoolean(String valueStr) Parses input string as a boolean value.static PaintparseColor(String valueStr) Parses input string as color.static doubleparseDouble(String valueStr) Parses input string as a double value.(package private) static booleansameFilePath(String pathname1, String pathname2) Tells whether two paths denote the same existing file, resolving symbolic linksstatic StringEquivalent of the C-style sprintf function.static doublesum(double x, double y) Calculates sum of two doubles, but treats NaNs as zeros.static double[]toDoubleArray(long[] array) Converts an array of long primitives to an array of doubles.
Field Details
MAX_LONG
MIN_LONG
MAX_DOUBLE
public static final double MAX_DOUBLEConstantMAX_DOUBLE=Double.MAX_VALUE- See Also:
MIN_DOUBLE
public static final double MIN_DOUBLEConstantMIN_DOUBLE=-Double.MAX_VALUE- See Also:
PATTERN
DecimalFormat pattern giving the RRDTool-like "+1.234567E+02" rendering- See Also:
RRD4J_DIR
Name of the directory created under the user home for demo graphs- See Also:
df
Per-threadNumberFormatapplyingPATTERNand a leading '+' for positivesSPRINTF_PATTERN
Matches a sprintf-style placeholder in a format string.ISO_DATE_FORMAT
The single date patterngetCalendar(String)accepts and names on failure- See Also:
lastLap
private static long lastLapWhen the previous lap of the timer ended.
Constructor Details
Util
private Util()Utility class, not intended for instantiation.
Method Details
toDoubleArray
public static double[] toDoubleArray(long[] array) Converts an array of long primitives to an array of doubles.- Parameters:
array- input array of long values.- Returns:
- Same array but with all values as double.
getTime
public static long getTime()Returns current timestamp in seconds (without milliseconds). Returned timestamp is obtained with the following expression:(System.currentTimeMillis() + 500L) / 1000L- Returns:
- Current timestamp in seconds, rounded to the nearest whole second
getTimestamp
public static long getTimestamp()Just an alias forgetTime()method.- Returns:
- Current timestamp (without milliseconds)
normalize
public static long normalize(long timestamp, long step) Rounds the given timestamp to the nearest whole "step". Rounded value is obtained from the following expression:timestamp - timestamp % step;- Parameters:
timestamp- Timestamp in secondsstep- Step in seconds- Returns:
- the timestamp floored to a multiple of step
max
public static double max(double x, double y) Returns the greater of two double values, but treats NaN as the smallest possible value. Note thatMath.max()behaves differently for NaN arguments.- Parameters:
x- the first value, which loses to y when it is NaNy- the second value, which loses to x when it is NaN- Returns:
- the lager of arguments
min
public static double min(double x, double y) Returns the smaller of two double values, but treats NaN as the greatest possible value. Note thatMath.min()behaves differently for NaN arguments.- Parameters:
x- the first value, which loses to y when it is NaNy- the second value, which loses to x when it is NaN- Returns:
- the smaller of arguments
sum
public static double sum(double x, double y) Calculates sum of two doubles, but treats NaNs as zeros.- Parameters:
x- First doubley- Second double- Returns:
- Sum(x,y) calculated as
Double.isNaN(x)? y: Double.isNaN(y)? x: x + y;
formatDouble
formatDouble
Formats a double value as a string with default NaN string.- Parameters:
x- value to formatforceExponents- whether to force exponential notation- Returns:
- formatted string
formatDouble
Formats double as a string using exponential notation (RRDTool like). Used for debugging through the project.- Parameters:
x- value to be formatted- Returns:
- string like "+1.234567E+02"
getDate
ReturnsDateobject for the given timestamp (in seconds, without milliseconds)- Parameters:
timestamp- Timestamp in seconds.- Returns:
- Corresponding Date object.
getCalendar
ReturnsCalendarobject for the given timestamp (in seconds, without milliseconds)- Parameters:
timestamp- Timestamp in seconds.- Returns:
- Corresponding Calendar object.
getCalendar
getTimestamp
Returns timestamp (unix epoch) for the given Date object- Parameters:
date- instant to build the calendar from, in the default time zone- Returns:
- Corresponding timestamp (without milliseconds)
getTimestamp
Returns timestamp (unix epoch) for the given Calendar object- Parameters:
gc- Calendar object- Returns:
- Corresponding timestamp (without milliseconds)
getTimestamp
public static long getTimestamp(int year, int month, int day, int hour, int min) Returns timestamp (unix epoch) for the given year, month, day, hour and minute.The date is resolved in the current time zone
- Parameters:
year- full year number, such as 2024month- zero-based month, 0 for January through 11 for Decemberday- Day in monthhour- hour of day, 0-23min- minute within the hour, 0-59- Returns:
- unix epoch timestamp, resolved in the current time zone
getTimestamp
public static long getTimestamp(int year, int month, int day) Returns timestamp (unix epoch) for the given year, month and day.The date is resolved in the current time zone
- Parameters:
year- full year number, such as 2024month- zero-based month, 0 for January through 11 for Decemberday- Day in month- Returns:
- unix epoch timestamp for midnight in the current time zone
getTimestamp
Parses at-style time specification and returns the corresponding timestamp. For example:long t = Util.getTimestamp("now-1d");- Parameters:
atStyleTimeSpec- at-style time specification. For the complete explanation of the syntax allowed see RRDTool'srrdfetchman page.- Returns:
- timestamp in seconds since epoch.
getTimestamps
Parses two related at-style time specifications and returns corresponding timestamps. For example:long[] t = Util.getTimestamps("end-1d","now");- Parameters:
atStyleTimeSpec1- Starting at-style time specification. For the complete explanation of the syntax allowed see RRDTool'srrdfetchman page.atStyleTimeSpec2- Ending at-style time specification. For the complete explanation of the syntax allowed see RRDTool'srrdfetchman page.- Returns:
- An array of two longs representing starting and ending timestamp in seconds since epoch.
parseDouble
Parses input string as a double value. If the value cannot be parsed, Double.NaN is returned (NumberFormatException is never thrown).- Parameters:
valueStr- String representing double value- Returns:
- a double corresponding to the input string
isDouble
Checks if a string can be parsed as double.- Parameters:
s- Input string- Returns:
trueif the string can be parsed as double,falseotherwise
parseBoolean
Parses input string as a boolean value. The parser is case insensitive.- Parameters:
valueStr- String representing boolean value- Returns:
true, if valueStr equals to 'true', 'on', 'yes', 'y' or '1';falsein all other cases.
parseColor
Parses input string as color. The color string should be of the form #RRGGBB (no alpha specified, opaque color) or #RRGGBBAA (alpa specified, transparent colors). Leading character '#' is optional.- Parameters:
valueStr- Input string, for example #FFAA24, #AABBCC33, 010203 or ABC13E4F- Returns:
- Paint object
- Throws:
IllegalArgumentException- If the input string is not 6 or 8 characters long (without optional '#')
getFileSeparator
Returns file system separator string.- Returns:
- File system separator ("/" on Unix, "\" on Windows)
getUserHomeDirectory
Returns path to user's home directory.- Returns:
- Path to users home directory, with file separator appended.
getRrd4jDemoDirectory
Returns path to directory used for placement of Rrd4j demo graphs and creates it if necessary.- Returns:
- Path to demo directory (defaults to $HOME/rrd4j/) if directory exists or was successfully created. Null if such directory could not be created.
getRrd4jDemoPath
sameFilePath
Tells whether two paths denote the same existing file, resolving symbolic links- Parameters:
pathname1- first file path, absolute or relativepathname2- second file path, absolute or relative- Returns:
trueif both paths exist and resolve to one file,falseif their existence differs or neither path exists- Throws:
IOException- Thrown if a path cannot be resolved to its canonical form
getMatchingDatasourceIndex
Looks up inrrd2the datasource sharing the name of datasourcedsIndexofrrd1- Parameters:
rrd1- database to read the datasource name fromdsIndex- zero-based datasource index withinrrd1rrd2- database to search by name- Returns:
- matching datasource index in
rrd2, or -1 if no datasource has that name - Throws:
IOException- Thrown if either database cannot be read
getMatchingArchiveIndex
Looks up inrrd2the archive whose consolidation function and row count match archivearcIndexofrrd1- Parameters:
rrd1- database to read the archive properties fromarcIndex- zero-based archive index withinrrd1rrd2- database to search by consolidation function and step count- Returns:
- matching archive index in
rrd2, or -1 if no archive matches - Throws:
IOException- Thrown if either database cannot be read
getTmpFilename
Creates a new empty temporary file with therrd4j_prefix and.tmpsuffix- Returns:
- canonical path to the newly created file
- Throws:
IOException- Thrown if the file cannot be created or its path not resolved
getCalendar
Creates Calendar object from a string. The string should represent either a long integer (UNIX timestamp in seconds without milliseconds, like "1002354657") or a human readable date string in the format "yyyy-MM-dd HH:mm:ss" (like "2004-02-25 12:23:45").- Parameters:
timeStr- Input string- Returns:
- Calendar object
getLapTime
Function used for debugging purposes and performance bottlenecks detection. Probably of no use for end users of Rrd4j.- Returns:
- String representing time in seconds since last
getLapTime()method call.
getRrd4jHomeDirectory
Returns the root directory of the Rrd4j distribution. Useful in some demo applications, probably of no use anywhere else.The function assumes that all Rrd4j .class files are placed under the <root>/classes subdirectory and that all jars (libraries) are placed in the <root>/lib subdirectory (the original Rrd4j directory structure).
- Returns:
- absolute path to Rrd4j's home directory
equal
public static boolean equal(double x, double y) Compares two doubles but treats all NaNs as equal. In Java (by default) Double.NaN == Double.NaN always returnsfalse- Parameters:
x- the first valuey- the second value- Returns:
trueif x and y are both equal to Double.NaN, or if x == y.falseotherwise
getCanonicalPath
Returns canonical file path for the given file path- Parameters:
path- Absolute or relative file path- Returns:
- Canonical file path
- Throws:
IOException- Thrown if canonical file path could not be resolved
getLastModifiedTime
Returns last modification time for the given file.- Parameters:
file- File object representing file on the disk- Returns:
- Last modification time in seconds (without milliseconds)
- Throws:
IOException- Thrown if the file cannot be read or the path cannot be resolved
fileExists
Checks if the file with the given file name exists- Parameters:
filename- File name- Returns:
trueif file exists,falseotherwise
max
public static double max(double[] values) Finds max value for an array of doubles (NaNs are ignored). If all values in the array are NaNs, NaN is returned.- Parameters:
values- Array of double values- Returns:
- max value in the array (NaNs are ignored)
min
public static double min(double[] values) Finds min value for an array of doubles (NaNs are ignored). If all values in the array are NaNs, NaN is returned.- Parameters:
values- Array of double values- Returns:
- min value in the array (NaNs are ignored)
sprintf