com.buildforge.services.common.text
Class TextUtils

java.lang.Object
  extended by com.buildforge.services.common.text.TextUtils

public class TextUtils
extends java.lang.Object

Some text formatting and covnersion utilities.


Method Summary
static java.lang.StringBuilder appendFixed(java.lang.StringBuilder sb, java.lang.String s, int width)
          Forces a string to an exact length.
static java.lang.String bf_encrypt(java.lang.String pt)
          Java implementation of the bf_encrypt algorithm.
static java.lang.String fixedHex(long value, int width)
          Represents an integer value as hex and pads it to be at least the requested number of hex digits.
static boolean isEmpty(java.lang.String s)
          Convenient test for either null or empty string.
static java.lang.String join(char delim, java.lang.String... args)
          Joins a list of strings together using the specified delimiter.
static java.lang.String[] split(java.lang.String s, char delim)
          Splits a string into substrings using the specified delimiter.
static boolean toBoolean(java.lang.Object value, boolean def)
           
static boolean toBoolean(java.lang.String value, boolean def)
           
static char toChar(java.lang.Object value, char def)
          Safely converts an arbitrary object to a char.
static char toChar(java.lang.String value, char def)
          Safely converts a string to a char.
static java.util.Date toDate(java.lang.Object value)
           
static java.util.Date toDate(java.lang.String value)
           
static double toDouble(java.lang.Object value, double def)
          Safely converts an arbitrary object to a double.
static double toDouble(java.lang.String value, double def)
          Safely converts a string to a double.
static
<E extends java.lang.Enum<E>>
E
toEnum(java.lang.Class<E> cls, java.lang.Object value)
           
static
<E extends java.lang.Enum<E>>
E
toEnum(java.lang.Class<E> cls, java.lang.String value)
           
static int toInt(java.lang.Object value, int def)
          Safely converts an arbitrary object to an integer.
static int toInt(java.lang.String value, int def)
          Safely converts a string to an integer.
static long toLong(java.lang.Object value, long def)
          Safely converts an arbitrary object to a long.
static long toLong(java.lang.String value, long def)
          Safely converts a string to a long.
static java.lang.String toString(java.lang.Object value, java.lang.String def)
           
static java.lang.String toString(java.lang.String value, java.lang.String def)
           
static int toTimestamp(java.lang.Object value)
           
static int toTimestamp(java.lang.String value)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isEmpty

public static final boolean isEmpty(java.lang.String s)
Convenient test for either null or empty string.

Parameters:
s - the string to test
Returns:
true if the string is null or zero-length

split

public static java.lang.String[] split(java.lang.String s,
                                       char delim)
Splits a string into substrings using the specified delimiter. For convenience, a null input string is permitted, and results in a null return value.

Parameters:
s - the string to split, or null
delim - the delimiter character
Returns:
the array of substrings, or null

join

public static java.lang.String join(char delim,
                                    java.lang.String... args)
Joins a list of strings together using the specified delimiter. For convenience, a null input array is permitted, and results in a null return value. No checks are performed to make certain that the strings do not contain the delimiter, so any such precautions are the caller's responsibility.

Parameters:
delim - the delimiter
args - the strings to join together, or null
Returns:
the joined string, or null

fixedHex

public static java.lang.String fixedHex(long value,
                                        int width)
Represents an integer value as hex and pads it to be at least the requested number of hex digits. If the value is too large to fit in a field of that size, then the higher-order digits are discarded. For example, 0x29C becomes "029C" with a width of 4, or "9C" with a width of 2.

Parameters:
value - the value to represent as hex
width - the width to use
Returns:
the value as a hexadecimal string of the requested length

appendFixed

public static java.lang.StringBuilder appendFixed(java.lang.StringBuilder sb,
                                                  java.lang.String s,
                                                  int width)
Forces a string to an exact length. If the string was longer, then the return value is truncated. If it was shorter, the value is padded on the right with spaces. If the input width is negative, then the left is padded instead.

Parameters:
sb - the string buffer to add this string to the end of
s - the string to be padded
width - the desired width, or the negative of the desired width for left-padding instead of right-padding.
Returns:
sb

bf_encrypt

public static java.lang.String bf_encrypt(java.lang.String pt)
                                   throws APIException
Java implementation of the bf_encrypt algorithm. The client uses this function to encrypt passwords before they are sent across the network connection. This algorithm is useful for obfuscation, only; it should not be treated as a cryptographically secure. Also, the plaintext may not exceed 116 bytes in length when encoded as UTF-8 data. In a worst-case scenario, this allows 29 arbitrary unicode characters in the 0x000000-0x10FFFF range.

Parameters:
pt - the plaintext
Returns:
the ciphertext
Throws:
APIException - if the plaintext is too long

toInt

public static int toInt(java.lang.Object value,
                        int def)
Safely converts an arbitrary object to an integer. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toInt

public static int toInt(java.lang.String value,
                        int def)
Safely converts a string to an integer. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toLong

public static long toLong(java.lang.Object value,
                          long def)
Safely converts an arbitrary object to a long. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toLong

public static long toLong(java.lang.String value,
                          long def)
Safely converts a string to a long. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toDouble

public static double toDouble(java.lang.Object value,
                              double def)
Safely converts an arbitrary object to a double. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toDouble

public static double toDouble(java.lang.String value,
                              double def)
Safely converts a string to a double. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toChar

public static char toChar(java.lang.Object value,
                          char def)
Safely converts an arbitrary object to a char. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toChar

public static char toChar(java.lang.String value,
                          char def)
Safely converts a string to a char. If the input is null, or if it cannot be parsed, then the specified default value of def is substituted. In general, this method is never expected to throw any exceptions.

Parameters:
value - the value to convert (may be null)
def - the value to use when value is null or cannot be parsed to the desired format
Returns:
the parsed value, or def

toBoolean

public static boolean toBoolean(java.lang.Object value,
                                boolean def)

toBoolean

public static boolean toBoolean(java.lang.String value,
                                boolean def)

toString

public static java.lang.String toString(java.lang.String value,
                                        java.lang.String def)

toString

public static java.lang.String toString(java.lang.Object value,
                                        java.lang.String def)

toEnum

public static <E extends java.lang.Enum<E>> E toEnum(java.lang.Class<E> cls,
                                                     java.lang.Object value)
                                          throws APIException
Throws:
APIException

toEnum

public static <E extends java.lang.Enum<E>> E toEnum(java.lang.Class<E> cls,
                                                     java.lang.String value)
                                          throws APIException
Throws:
APIException

toDate

public static java.util.Date toDate(java.lang.String value)

toDate

public static java.util.Date toDate(java.lang.Object value)

toTimestamp

public static int toTimestamp(java.lang.String value)

toTimestamp

public static int toTimestamp(java.lang.Object value)