2.17.2: 2011-06-17

net.sf.basedb.util
Class Values

java.lang.Object
  extended by net.sf.basedb.util.Values

public class Values
extends Object

This class contains a set of static methods that may be useful for handling strings, ints and other values.

Version:
2.0
Author:
Nicklas
Last modified
$Date: 2010-08-13 10:50:27 +0200 (Fri, 13 Aug 2010) $

Field Summary
private static Pattern BYTES_REGEXP
           
private static SimpleDateFormat DATE_FORMAT
           
private static SimpleDateFormat DATETIME_FORMAT
           
private static long GB
           
private static long kB
           
private static long MB
           
private static SimpleDateFormat TIME_FORMAT
           
 
Constructor Summary
Values()
           
 
Method Summary
static String formatBytes(Long bytes)
          Formats a value using units of bytes, kilobytes, megabytes or gigabytes.
static String formatBytes(Long bytes, int decimals)
          Formats a value using units of bytes, kilobytes, megabytes or gigabytes.
static String formatDate(Date date)
          Deprecated. Use a Formatter for dates instead
static String formatDateTime(Date datetime)
          Deprecated. Use a Formatter for dates instead
static String formatMessage(String message, Object... parameters)
          Insert the values of parameters into a template message.
static String formatNumber(Float number, int decimals)
          Formats a decimal number with the specified number of decimals.
static String formatNumber(Float number, int decimals, String unit)
          Formats a decimal number with the specified number of decimals and optionally adding a unit.
static String formatTime(Date time)
          Deprecated. Use a Formatter for dates instead
static String formatTime(long seconds)
          Formats a number of seconds to a more proper time format.
static boolean getBoolean(String value)
          Convert a string to a boolean. '0', 'no', 'false', '' and the null string are converted to FALSE, all other values are converted to TRUE.
static boolean getBoolean(String value, boolean defaultValue)
          Convert a string to a boolean.
static Date getDate(String value)
          Deprecated. Use a Formatter for dates instead
static Date[] getDate(String[] values)
          Deprecated. Use a Formatter for dates instead
static Date[] getDate(String[] values, Formatter<Date> dateFormatter)
           
static Date getDate(String value, Date defaultValue)
          Deprecated. Use a Formatter for dates instead
static double getDouble(String value)
          Convert a string to a double.
static Double[] getDouble(String[] values)
          Convert an array of strings to an array of Double:s.
static double getDouble(String value, double defaultValue)
          Convert a string to a double.
static Double getDouble(String value, Double defaultValue)
           
static float getFloat(String value)
          Convert a string to an float.
static Float[] getFloat(String[] values)
          Convert an array of strings to an array of Float:s.
static float getFloat(String value, float defaultValue)
          Convert a string to an integer.
static Float getFloat(String value, Float defaultValue)
           
static int getInt(String value)
          Convert a string to an integer.
static Integer[] getInt(String[] values)
          Convert an array of strings to an array of Integer:s.
static int getInt(String value, int defaultValue)
          Convert a string to an integer.
static Integer getInteger(String value, Integer defaultValue)
           
static long getLong(String value)
          Convert a string to a long.
static Long[] getLong(String[] values)
          Convert an array of strings to an array of Long:s.
static long getLong(String value, long defaultValue)
          Convert a string to an long.
static Long getLong(String value, Long defaultValue)
          Convert a string to an long.
static String getString(Collection<?> values, String deliminator, boolean skipNull)
           
static
<T> String
getString(Collection<T> values, String deliminator, boolean skipNull, Formatter<? super T> formatter)
           
static String getString(String value)
          Convert a string to a string, and map NULL to the empty string.
static String getString(String value, String defaultValue)
          Convert a string to a string.
static String getStringOrNull(String value)
          Convert the empty string ("") to null after removing leading and trailing whitespace.
static Long parseBytes(String fBytes, Long defaultValue)
          Parses a string containing the number of bytes formatted as the formatBytes(Long) method.
static
<T> String[]
toStrings(Collection<T> values, Formatter<T> formatter)
          Convert a collection of objects to an array of strings.
static String trim(String s, int maxLength)
          Trim a string to a maximum length.
static String trimString(String value, int maxLength)
          Trims long strings to a maximum of maxLength characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GB

private static final long GB
See Also:
Constant Field Values

MB

private static final long MB
See Also:
Constant Field Values

kB

private static final long kB
See Also:
Constant Field Values

BYTES_REGEXP

private static final Pattern BYTES_REGEXP

DATE_FORMAT

private static final SimpleDateFormat DATE_FORMAT

TIME_FORMAT

private static final SimpleDateFormat TIME_FORMAT

DATETIME_FORMAT

private static final SimpleDateFormat DATETIME_FORMAT
Constructor Detail

Values

public Values()
Method Detail

getInt

public static final int getInt(String value)
Convert a string to an integer.

Parameters:
value - The string to convert
Returns:
The converted value, or 0 if the string could not be converted

getInt

public static final int getInt(String value,
                               int defaultValue)
Convert a string to an integer.

Parameters:
value - The string to convert
defaultValue - The value to return if the string cannot be converted
Returns:
The converted value or the default value

getInteger

public static final Integer getInteger(String value,
                                       Integer defaultValue)

getInt

public static final Integer[] getInt(String[] values)
Convert an array of strings to an array of Integer:s. Typically used when you have an array of item id:s and need to use them in a query. Positions with unconvertable values are skipped.

Parameters:
values - The array of strings
Returns:
The converted values

getLong

public static final long getLong(String value)
Convert a string to a long.

Parameters:
value - The string to convert
Returns:
The converted value, or 0 if the string could not be converted

getLong

public static final long getLong(String value,
                                 long defaultValue)
Convert a string to an long.

Parameters:
value - The string to convert
defaultValue - The value to return if the string cannot be converted
Returns:
The converted value or the default value

getLong

public static final Long getLong(String value,
                                 Long defaultValue)
Convert a string to an long.

Parameters:
value - The string to convert
defaultValue - The value to return if the string cannot be converted
Returns:
The converted value or the default value

getLong

public static final Long[] getLong(String[] values)
Convert an array of strings to an array of Long:s. Positions with unconvertable values are skipped.

Parameters:
values - The array of strings
Returns:
The converted values

getFloat

public static final float getFloat(String value)
Convert a string to an float.

Parameters:
value - The string to convert
Returns:
The converted value, or 0 if the string could not be converted

getFloat

public static final float getFloat(String value,
                                   float defaultValue)
Convert a string to an integer.

Parameters:
value - The string to convert
defaultValue - The value to return if the string cannot be converted
Returns:
The converted value or the default value

getFloat

public static final Float getFloat(String value,
                                   Float defaultValue)

getFloat

public static final Float[] getFloat(String[] values)
Convert an array of strings to an array of Float:s.

Parameters:
values - The array of strings
Returns:
The converted values

getDouble

public static final double getDouble(String value)
Convert a string to a double.

Parameters:
value - The string to convert
Returns:
The converted value, or 0 if the string could not be converted

getDouble

public static final double getDouble(String value,
                                     double defaultValue)
Convert a string to a double.

Parameters:
value - The string to convert
defaultValue - The value to return if the string cannot be converted
Returns:
The converted value or the default value

getDouble

public static final Double getDouble(String value,
                                     Double defaultValue)

getDouble

public static final Double[] getDouble(String[] values)
Convert an array of strings to an array of Double:s.

Parameters:
values - The array of strings
Returns:
The converted values

getString

public static final String getString(String value)
Convert a string to a string, and map NULL to the empty string.

Parameters:
value - The string to convert
Returns:
The original string, or the empty string ("") if the original is null

getString

public static final String getString(String value,
                                     String defaultValue)
Convert a string to a string.

Parameters:
value - The string to convert
defaultValue - The value to return if the string is null
Returns:
The original string, or the default value if the original is null

getStringOrNull

public static final String getStringOrNull(String value)
Convert the empty string ("") to null after removing leading and trailing whitespace.

Parameters:
value - The string to convert
Returns:
The original string with leading and trailing whitespace has been removed or null if the value is the empty string ("")

getString

public static final String getString(Collection<?> values,
                                     String deliminator,
                                     boolean skipNull)

getString

public static final <T> String getString(Collection<T> values,
                                         String deliminator,
                                         boolean skipNull,
                                         Formatter<? super T> formatter)

toStrings

public static final <T> String[] toStrings(Collection<T> values,
                                           Formatter<T> formatter)
Convert a collection of objects to an array of strings. If a formatter is specified, the Formatter.format(Object) method is used, otherwise Object.toString().

Parameters:
values - Collection to be converted.
formatter -
Returns:
A String array

trimString

public static final String trimString(String value,
                                      int maxLength)
Trims long strings to a maximum of maxLength characters.

Parameters:
value - The string to trim
maxLength -
Returns:
A string or null if value is null.

getBoolean

public static final boolean getBoolean(String value)
Convert a string to a boolean. '0', 'no', 'false', '' and the null string are converted to FALSE, all other values are converted to TRUE. Case is ignored.

Parameters:
value - The value to convert
Returns:
TRUE or FALSE

getBoolean

public static final boolean getBoolean(String value,
                                       boolean defaultValue)
Convert a string to a boolean. If the string is null, the default value is returned. '0', 'no', 'false' and '' are converted to FALSE, all other values are converted to TRUE.

Parameters:
value - The value to convert
defaultValue - The value to return if the string is null
Returns:
TRUE or FALSE

getDate

public static final Date getDate(String value)
Deprecated. Use a Formatter for dates instead

Convert a string to a date. The string must be formatted as yyyy-mm-dd.

Parameters:
value - The value to convert
Returns:
The parsed date or null if the value can't be converted

getDate

public static final Date getDate(String value,
                                 Date defaultValue)
Deprecated. Use a Formatter for dates instead

Convert a string to a date. The string must be formatted as yyyy-mm-dd.

Parameters:
value - The value to convert
defaultValue - The value to return if the string can't be converted
Returns:
The parsed date or the default value

getDate

public static final Date[] getDate(String[] values)
Deprecated. Use a Formatter for dates instead

Convert an array of strings to an array of Date:s.

Parameters:
values - The array of strings
Returns:
The converted values

getDate

public static final Date[] getDate(String[] values,
                                   Formatter<Date> dateFormatter)

formatNumber

public static final String formatNumber(Float number,
                                        int decimals)
Formats a decimal number with the specified number of decimals.

Parameters:
number - The number to be formatted
decimals - The number of decimals to display, use a negative value to display all decimals
Returns:
A string with the formatted number, or an empty string if the number is null

formatNumber

public static final String formatNumber(Float number,
                                        int decimals,
                                        String unit)
Formats a decimal number with the specified number of decimals and optionally adding a unit.

Parameters:
number - The number to be formatted
decimals - The number of decimals to display, use a negative value to display all decimals
unit - A string that is added to the end of the formatted number, or null to not add anything
Returns:
A string with the formatted number, or an empty string if the number is null

formatBytes

public static final String formatBytes(Long bytes)
Formats a value using units of bytes, kilobytes, megabytes or gigabytes.

Parameters:
bytes - The value to format
Returns:
A string with a decimal value follwed by bytes, Kb, Mb or Gb or an empty string if the bytes value is null

formatBytes

public static final String formatBytes(Long bytes,
                                       int decimals)
Formats a value using units of bytes, kilobytes, megabytes or gigabytes.

Parameters:
bytes - The value to format, -1 = unknown
decimals - The number of decimals to display in KB, MB, and GB values
Since:
2.6.2

parseBytes

public static final Long parseBytes(String fBytes,
                                    Long defaultValue)
Parses a string containing the number of bytes formatted as the formatBytes(Long) method.

Parameters:
fBytes - The string containing the value to parse
defaultValue - This value is returned if the string cannot be parsed
Returns:
Bytes as a Long object.

formatDate

public static final String formatDate(Date date)
Deprecated. Use a Formatter for dates instead

Formats a date as four-digit year, two-digit month and two-digit day separated with a hyphen. Example: 2004-03-25

Parameters:
date - The date to format
Returns:
The formatted date

formatTime

public static final String formatTime(Date time)
Deprecated. Use a Formatter for dates instead

Formats a time as two-digit hour, two-digit minute and two-digit seconds separated with a colon. Example: 14:42:15

Parameters:
time - The time to format
Returns:
The formatted time

formatDateTime

public static final String formatDateTime(Date datetime)
Deprecated. Use a Formatter for dates instead

Formats a date as four-digit year, two-digit month and two-digit day separated with a hyphen and a time as two-digit hour, minute and seconds separated with a colon. Example: 2004-03-25 14:42:13

Parameters:
datetime - The date and time to format
Returns:
The formatted date with time

formatTime

public static final String formatTime(long seconds)
Formats a number of seconds to a more proper time format. Example: 7925 -> 2 hours 12 minutes 5 seconds

Parameters:
seconds - The number of seconds
Returns:
The formatted string

formatMessage

public static final String formatMessage(String message,
                                         Object... parameters)
Insert the values of parameters into a template message.

Parameters:
message - The message template, with placeholders of the form {1}, {2}, etc. that will be replaced with the values of the parameters
parameters - The parameters values that will replace the placeholders in the template message
Returns:
The message with the placeholders replaced by the parameters

trim

public static String trim(String s,
                          int maxLength)
Trim a string to a maximum length. If the input string is longer than maxLength the string is cut of at the end and ... is added to it.

Parameters:
s - String to be trimmed.
maxLength - The maximum length the string is allowed to be.
Returns:
A string or null if s is null.

2.17.2: 2011-06-17