org.rrd4j.core

## Class Util

- java.lang.Object
- - org.rrd4j.core.Util

-     public class Util
      extends Object

  Class defines various utility functions used in Rrd4j.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Util.Xml` Various DOM utility functions.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) static ThreadLocal<NumberFormat>` | `df`
    `(package private) static String` | `ISO_DATE_FORMAT`
    `static double` | `MAX_DOUBLE` Constant `MAX_DOUBLE=Double.MAX_VALUE`
    `static long` | `MAX_LONG` Constant `MAX_LONG=Long.MAX_VALUE`
    `static double` | `MIN_DOUBLE` Constant `MIN_DOUBLE=-Double.MAX_VALUE`
    `static long` | `MIN_LONG` Constant `MIN_LONG=-Long.MAX_VALUE`
    `(package private) static String` | `PATTERN`
    `(package private) static String` | `RRD4J_DIR`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `equal(double x, double y)` Compares two doubles but treats all NaNs as equal.
    `static boolean` | `fileExists(String filename)` Checks if the file with the given file name exists
    `static String` | `formatDouble(double x)` Formats double as a string using exponential notation (RRDTool like).
    `(package private) static String` | `formatDouble(double x, boolean forceExponents)` Formats a double value as a string with default NaN string.
    `(package private) static String` | `formatDouble(double x, String nanString, boolean forceExponents)` Formats a double value as a string.
    `static Calendar` | `getCalendar(Date date)` Returns `Calendar` object for the given Date object
    `static Calendar` | `getCalendar(long timestamp)` Returns `Calendar` object for the given timestamp (in seconds, without milliseconds)
    `static Calendar` | `getCalendar(String timeStr)` Creates Calendar object from a string.
    `static String` | `getCanonicalPath(String path)` Returns canonical file path for the given file path
    `static Date` | `getDate(long timestamp)` Returns `Date` object for the given timestamp (in seconds, without milliseconds)
    `static String` | `getFileSeparator()` Returns file system separator string.
    `static String` | `getLapTime()` Function used for debugging purposes and performance bottlenecks detection.
    `static long` | `getLastModifiedTime(String file)` Returns last modification time for the given file.
    `(package private) static int` | `getMatchingArchiveIndex(RrdDb rrd1, int arcIndex, RrdDb rrd2)`
    `(package private) static int` | `getMatchingDatasourceIndex(RrdDb rrd1, int dsIndex, RrdDb rrd2)`
    `static String` | `getRrd4jDemoDirectory()` Returns path to directory used for placement of Rrd4j demo graphs and creates it if necessary.
    `static String` | `getRrd4jDemoPath(String filename)` Returns full path to the file stored in the demo directory of Rrd4j
    `static String` | `getRrd4jHomeDirectory()` Returns the root directory of the Rrd4j distribution.
    `static long` | `getTime()` Returns current timestamp in seconds (without milliseconds).
    `static long` | `getTimestamp()` Just an alias for `getTime()` method.
    `static long` | `getTimestamp(Calendar gc)` Returns timestamp (unix epoch) for the given Calendar object
    `static long` | `getTimestamp(Date date)` Returns timestamp (unix epoch) for the given Date object
    `static long` | `getTimestamp(int year, int month, int day)` Returns timestamp (unix epoch) for the given year, month and day.
    `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.
    `static long` | `getTimestamp(String atStyleTimeSpec)` Parses at-style time specification and returns the corresponding timestamp.
    `static long[]` | `getTimestamps(String atStyleTimeSpec1, String atStyleTimeSpec2)` Parses two related at-style time specifications and returns corresponding timestamps.
    `(package private) static String` | `getTmpFilename()`
    `static String` | `getUserHomeDirectory()` Returns path to user's home directory.
    `static boolean` | `isDouble(String s)` Checks if a string can be parsed as double.
    `static double` | `max(double[] values)` Finds max value for an array of doubles (NaNs are ignored).
    `static double` | `max(double x, double y)` Returns the greater of two double values, but treats NaN as the smallest possible value.
    `static double` | `min(double[] values)` Finds min value for an array of doubles (NaNs are ignored).
    `static double` | `min(double x, double y)` Returns the smaller of two double values, but treats NaN as the greatest possible value.
    `static long` | `normalize(long timestamp, long step)` Rounds the given timestamp to the nearest whole "step".
    `static boolean` | `parseBoolean(String valueStr)` Parses input string as a boolean value.
    `static Paint` | `parseColor(String valueStr)` Parses input string as color.
    `static double` | `parseDouble(String valueStr)` Parses input string as a double value.
    `(package private) static boolean` | `sameFilePath(String pathname1, String pathname2)`
    `static String` | `sprintf(Locale l, String format, Object... args)` Equivalent of the C-style sprintf function.
    `static double` | `sum(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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### df

          static final ThreadLocal<NumberFormat> df

    - #### ISO_DATE_FORMAT

          static final String ISO_DATE_FORMAT

      See Also:
        Constant Field Values

    - #### MAX_DOUBLE

          public static final double MAX_DOUBLE

      Constant `MAX_DOUBLE=Double.MAX_VALUE`
      See Also:
        Constant Field Values

    - #### MAX_LONG

          public static final long MAX_LONG

      Constant `MAX_LONG=Long.MAX_VALUE`
      See Also:
        Constant Field Values

    - #### MIN_DOUBLE

          public static final double MIN_DOUBLE

      Constant `MIN_DOUBLE=-Double.MAX_VALUE`
      See Also:
        Constant Field Values

    - #### MIN_LONG

          public static final long MIN_LONG

      Constant `MIN_LONG=-Long.MAX_VALUE`
      See Also:
        Constant Field Values

    - #### PATTERN

          static final String PATTERN

      See Also:
        Constant Field Values

    - #### RRD4J_DIR

          static final String RRD4J_DIR

      See Also:
        Constant Field Values

  - ### Method Detail

    - #### 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 returns `false`
      Parameters:
        `x` - the first value
        `y` - the second value
      Returns:
        `true` if x and y are both equal to Double.NaN, or if x == y. `false` otherwise

    - #### fileExists

          public static boolean fileExists(String filename)

      Checks if the file with the given file name exists
      Parameters:
        `filename` - File name
      Returns:
        `true` if file exists, `false` otherwise

    - #### formatDouble

          public static String formatDouble(double x)

      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"

    - #### formatDouble

          static String formatDouble(double x,
                                     boolean forceExponents)

      Formats a double value as a string with default NaN string.
      Parameters:
        `x` - value to format
        `forceExponents` - whether to force exponential notation
      Returns:
        formatted string

    - #### formatDouble

          static String formatDouble(double x,
                                     String nanString,
                                     boolean forceExponents)

      Formats a double value as a string.
      Parameters:
        `x` - value to format
        `nanString` - string to use for NaN values
        `forceExponents` - whether to force exponential notation
      Returns:
        formatted string

    - #### getCalendar

          public static Calendar getCalendar(Date date)

      Returns `Calendar` object for the given Date object
      Parameters:
        `date` - Date object
      Returns:
        Corresponding Calendar object.

    - #### getCalendar

          public static Calendar getCalendar(long timestamp)

      Returns `Calendar` object for the given timestamp (in seconds, without milliseconds)
      Parameters:
        `timestamp` - Timestamp in seconds.
      Returns:
        Corresponding Calendar object.

    - #### getCalendar

          public static Calendar getCalendar(String timeStr)

      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

    - #### getCanonicalPath

          public static String getCanonicalPath(String path)
                                         throws IOException

      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

    - #### getDate

          public static Date getDate(long timestamp)

      Returns `Date` object for the given timestamp (in seconds, without milliseconds)
      Parameters:
        `timestamp` - Timestamp in seconds.
      Returns:
        Corresponding Date object.

    - #### getFileSeparator

          public static String getFileSeparator()

      Returns file system separator string.
      Returns:
        File system separator ("/" on Unix, "\" on Windows)

    - #### getLapTime

          public static String 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.

    - #### getLastModifiedTime

          public static long getLastModifiedTime(String file)
                                          throws IOException

      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`

    - #### getMatchingArchiveIndex

          static int getMatchingArchiveIndex(RrdDb rrd1,
                                             int arcIndex,
                                             RrdDb rrd2)
                                      throws IOException

      Throws:
        `IOException`

    - #### getMatchingDatasourceIndex

          static int getMatchingDatasourceIndex(RrdDb rrd1,
                                                int dsIndex,
                                                RrdDb rrd2)
                                         throws IOException

      Throws:
        `IOException`

    - #### getRrd4jDemoDirectory

          public static String 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

          public static String getRrd4jDemoPath(String filename)

      Returns full path to the file stored in the demo directory of Rrd4j
      Parameters:
        `filename` - Partial path to the file stored in the demo directory of Rrd4j (just name and extension, without parent directories)
      Returns:
        Full path to the file

    - #### getRrd4jHomeDirectory

          public static String 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

    - #### 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

    - #### getTimestamp

          public static long getTimestamp()

      Just an alias for `getTime()` method.
      Returns:
        Current timestamp (without milliseconds)

    - #### getTimestamp

          public static long getTimestamp(Calendar gc)

      Returns timestamp (unix epoch) for the given Calendar object
      Parameters:
        `gc` - Calendar object
      Returns:
        Corresponding timestamp (without milliseconds)

    - #### getTimestamp

          public static long getTimestamp(Date date)

      Returns timestamp (unix epoch) for the given Date object
      Parameters:
        `date` - Date object
      Returns:
        Corresponding timestamp (without milliseconds)

    - #### 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` - Year
        `month` - Month (zero-based)
        `day` - Day in month
      Returns:
        Corresponding timestamp

    - #### 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` - Year
        `month` - Month (zero-based)
        `day` - Day in month
        `hour` - Hour
        `min` - Minute
      Returns:
        Corresponding timestamp

    - #### getTimestamp

          public static long getTimestamp(String atStyleTimeSpec)

      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's `rrdfetch` man page.

      Returns:
        timestamp in seconds since epoch.

    - #### getTimestamps

          public static long[] getTimestamps(String atStyleTimeSpec1,
                                             String atStyleTimeSpec2)

      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's `rrdfetch` man page.

        `atStyleTimeSpec2` - Ending at-style time specification. For the complete explanation of the syntax allowed see RRDTool's `rrdfetch` man page.

      Returns:
        An array of two longs representing starting and ending timestamp in seconds since epoch.

    - #### getTmpFilename

          static String getTmpFilename()
                                throws IOException

      Throws:
        `IOException`

    - #### getUserHomeDirectory

          public static String getUserHomeDirectory()

      Returns path to user's home directory.
      Returns:
        Path to users home directory, with file separator appended.

    - #### isDouble

          public static boolean isDouble(String s)

      Checks if a string can be parsed as double.
      Parameters:
        `s` - Input string
      Returns:
        `true` if the string can be parsed as double, `false` otherwise

    - #### 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)

    - #### 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 that `Math.max()` behaves differently for NaN arguments.
      Parameters:
        `x` - an argument
        `y` - another argument
      Returns:
        the lager of arguments

    - #### 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)

    - #### 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 that `Math.min()` behaves differently for NaN arguments.
      Parameters:
        `x` - an argument
        `y` - another argument
      Returns:
        the smaller of arguments

    - #### 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 seconds
        `step` - Step in seconds
      Returns:
        "Rounded" timestamp

    - #### parseBoolean

          public static boolean parseBoolean(String valueStr)

      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'; `false` in all other cases.

    - #### parseColor

          public static Paint parseColor(String valueStr)

      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 '#')

    - #### parseDouble

          public static double parseDouble(String valueStr)

      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

    - #### sameFilePath

          static boolean sameFilePath(String pathname1,
                                      String pathname2)
                               throws IOException

      Throws:
        `IOException`

    - #### sprintf

          public static String sprintf(Locale l,
                                       String format,
                                       Object... args)

      Equivalent of the C-style sprintf function.
      Parameters:
        `format` - Format string
        `args` - Arbitrary list of arguments
        `l` - a `Locale` object.
      Returns:
        Formatted string

    - #### sum

          public static double sum(double x,
                                   double y)

      Calculates sum of two doubles, but treats NaNs as zeros.
      Parameters:
        `x` - First double
        `y` - Second double
      Returns:
        Sum(x,y) calculated as `Double.isNaN(x)? y: Double.isNaN(y)? x: x + y;`

    - #### 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.