Package net.sf.basedb.util
Class Values
java.lang.Object
net.sf.basedb.util.Values
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 Summary
Modifier and TypeFieldDescriptionprivate static final Pattern
static final long
static final long
static final long
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final String
formatBytes
(Long bytes) Formats a value using units of bytes, kilobytes, megabytes or gigabytes.static final String
formatBytes
(Long bytes, int decimals) Formats a value using units of bytes, kilobytes, megabytes or gigabytes.static final String
formatMessage
(String message, Object... parameters) Insert the values of parameters into a template message.static final String
formatNumber
(Float number, int decimals) Formats a decimal number with the specified number of decimals.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.static final String
formatTime
(long seconds) Formats a number of seconds to a more proper time format.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.static final boolean
getBoolean
(String value, boolean defaultValue) Convert a string to a boolean.static final Date[]
static final double
Convert a string to a double.static final Double[]
Convert an array of strings to an array of Double:s.static final double
Convert a string to a double.static final Double
static final float
Convert a string to an float.static final Float[]
Convert an array of strings to an array of Float:s.static final float
Convert a string to an integer.static final Float
static final int
Convert a string to an integer.static final Integer[]
Convert an array of strings to an array of Integer:s.static final int
Convert a string to an integer.static final Integer
getInteger
(String value, Integer defaultValue) static final long
Convert a string to a long.static final Long[]
Convert an array of strings to an array of Long:s.static final long
Convert a string to an long.static final Long
Convert a string to an long.static final String
Convert a string to a string, and map NULL to the empty string.static final String
Convert a string to a string.static final String
getString
(Collection<?> values, String deliminator, boolean skipNull) static final <T> String
getString
(Collection<T> values, String deliminator, boolean skipNull, Formatter<? super T> formatter) static final String
getStringOrNull
(String value) Convert the empty string ("") to null after removing leading and trailing whitespace.static final Long
parseBytes
(String fBytes, Long defaultValue) Parses a string containing the number of bytes formatted as theformatBytes(Long)
method.static final <T> String[]
toStrings
(Collection<T> values, Formatter<T> formatter) Convert a collection of objects to an array of strings.static String
Trim a string to a maximum length.static final String
trimString
(String value, int maxLength) Trims long strings to a maximum ofmaxLength
characters.
-
Field Details
-
GB
public static final long GB- See Also:
-
MB
public static final long MB- See Also:
-
kB
public static final long kB- See Also:
-
BYTES_REGEXP
-
-
Constructor Details
-
Values
public Values()
-
-
Method Details
-
getInt
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
Convert a string to an integer.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string cannot be converted- Returns:
- The converted value or the default value
-
getInteger
-
getInt
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
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
Convert a string to an long.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string cannot be converted- Returns:
- The converted value or the default value
-
getLong
Convert a string to an long.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string cannot be converted- Returns:
- The converted value or the default value
-
getLong
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
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
Convert a string to an integer.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string cannot be converted- Returns:
- The converted value or the default value
-
getFloat
-
getFloat
Convert an array of strings to an array of Float:s.- Parameters:
values
- The array of strings- Returns:
- The converted values
-
getDouble
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
Convert a string to a double.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string cannot be converted- Returns:
- The converted value or the default value
-
getDouble
-
getDouble
Convert an array of strings to an array of Double:s.- Parameters:
values
- The array of strings- Returns:
- The converted values
-
getString
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
Convert a string to a string.- Parameters:
value
- The string to convertdefaultValue
- The value to return if the string is null- Returns:
- The original string, or the default value if the original is null
-
getStringOrNull
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
-
getString
public static final <T> String getString(Collection<T> values, String deliminator, boolean skipNull, Formatter<? super T> formatter) -
toStrings
Convert a collection of objects to an array of strings. If a formatter is specified, theFormatter.format(Object)
method is used, otherwiseObject.toString()
.- Parameters:
values
- Collection to be converted.- Returns:
- A String array
-
trimString
Trims long strings to a maximum ofmaxLength
characters.- Parameters:
value
- The string to trim- Returns:
- A string or null if value is null.
-
getBoolean
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
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 convertdefaultValue
- The value to return if the string is null- Returns:
- TRUE or FALSE
-
getDate
-
formatNumber
Formats a decimal number with the specified number of decimals.- Parameters:
number
- The number to be formatteddecimals
- 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
Formats a decimal number with the specified number of decimals and optionally adding a unit.- Parameters:
number
- The number to be formatteddecimals
- The number of decimals to display, use a negative value to display all decimalsunit
- 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
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
Formats a value using units of bytes, kilobytes, megabytes or gigabytes.- Parameters:
bytes
- The value to format, -1 = unknowndecimals
- The number of decimals to display in KB, MB, and GB values- Since:
- 2.6.2
-
parseBytes
Parses a string containing the number of bytes formatted as theformatBytes(Long)
method.- Parameters:
fBytes
- The string containing the value to parsedefaultValue
- This value is returned if the string cannot be parsed- Returns:
- Bytes as a Long object.
-
formatTime
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
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 parametersparameters
- The parameters values that will replace the placeholders in the template message- Returns:
- The message with the placeholders replaced by the parameters
-
trim
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.
-