com.evanmclean.evlib.misc
Class Conv

java.lang.Object
  extended by com.evanmclean.evlib.misc.Conv

public final class Conv
extends Object

Functions to convert strings to numbers, returning default values on errors.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Method Summary
static long sizeToLong(String numstr, long def)
           Converts a string representing a size such as "1.5MB" to a raw value of bytes.
static double toDouble(String str, double def)
          Converts the (trimmed) string to a double.
static Double toDoubleObj(String str, Double def)
          Converts the (trimmed) string to a Double, an empty string translating to null.
static int toInt(String str, int def)
          Converts the (trimmed) string to a integer.
static long toLong(String str, long def)
          Converts the (trimmed) string to a long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sizeToLong

public static long sizeToLong(String numstr,
                              long def)

Converts a string representing a size such as "1.5MB" to a raw value of bytes.

Size suffixes follow SI and IEC conventions as can be seen on the table on Wikipedia. So "KB" equals a multiplier of 1000, while "KiB" equals a multiplier of 1024.

The allowed (case-insensitive) multiplier suffixes are:

Allowed suffixes and their multiplier values
Name Suffix Multiplier Name Suffix Multiplier
(none) 1
kilobyte KB 103 kibibyte KiB 210
megabyte MB 106 mebibyte MiB 220
gigabyte GB 109 gibibyte GiB 230
terabyte TB 1012 tebibyte TiB 240
petabyte PB 1015 pebibyte PiB 250
exabyte EB 1018 exbibyte EiB 260
zettabyte ZB 1021 zebibyte ZiB 270
yottabyte YB 1024 yobibyte YiB 280

If the final value, converted from a double to a long would be negative, or greater than Long.MAX_VALUE then this is considered an error and the default value is returned.

Parameters:
numstr - The string to convert.
def - The default value to return in case of an error.
Returns:
The converted value, or the default value if case of an error.

toDouble

public static double toDouble(String str,
                              double def)
Converts the (trimmed) string to a double. If the conversion fails for any reason whatsoever then the specified default value is returned.

Parameters:
str - The string to convert.
def - The default to return if the string parameter cannot be converted to a number.
Returns:
The converted number or the default.

toDoubleObj

public static Double toDoubleObj(String str,
                                 Double def)
Converts the (trimmed) string to a Double, an empty string translating to null. If the conversion fails for any other reason whatsoever then the specified default value is returned.

Parameters:
str - The string to convert.
def - The default to return if the string parameter cannot be converted to a number.
Returns:
The converted number or the default.

toInt

public static int toInt(String str,
                        int def)
Converts the (trimmed) string to a integer. If the conversion fails for any reason whatsoever (such as the string being null or not a valid base 10 number) then the specified default value is returned.

Parameters:
str - The string to convert.
def - The default to return if the string parameter cannot be converted to a number.
Returns:
The converted number or the default.

toLong

public static long toLong(String str,
                          long def)
Converts the (trimmed) string to a long. If the conversion fails for any reason whatsoever (such as the string being null or not a valid base 10 number) then the specified default value is returned.

Parameters:
str - The string to convert.
def - The default to return if the string parameter cannot be converted to a number.
Returns:
The converted number or the default.