Class Values

java.lang.Object
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: 2020-06-24 09:41:43 +0200 (Wed, 24 Jun 2020) $
  • Field Details

  • Constructor Details

    • Values

      public Values()
  • Method Details

    • 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.
      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
      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[] 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.
    • 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.