p2pMapReduce.mapreduceModule.util
Class StringUtils

java.lang.Object
  extended by p2pMapReduce.mapreduceModule.util.StringUtils

public class StringUtils
extends java.lang.Object

General string utils


Nested Class Summary
static class StringUtils.TraditionalBinaryPrefix
          The traditional binary prefixes, kilo, mega, ..., exa, which can be represented by a 64-bit integer.
 
Field Summary
static char COMMA
           
static java.lang.String COMMA_STR
           
static java.lang.String[] emptyStringArray
           
static char ESCAPE_CHAR
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String arrayToString(java.lang.String[] strs)
          Given an array of strings, return a comma-separated list of its elements.
static java.lang.String byteDesc(long len)
          Return an abbreviated English-language desc of the byte length
static java.lang.String byteToHexString(byte[] bytes)
          Same as byteToHexString(bytes, 0, bytes.length).
static java.lang.String byteToHexString(byte[] bytes, int start, int end)
          Given an array of bytes it will convert the bytes to a hex string representation of the bytes
static java.lang.String escapeHTML(java.lang.String string)
          Escapes HTML Special characters present in the string.
static java.lang.String escapeString(java.lang.String str)
          Escape commas in the string using the default escape char
static java.lang.String escapeString(java.lang.String str, char escapeChar, char charToEscape)
          Escape charToEscape in the string with the escape char escapeChar
static java.lang.String escapeString(java.lang.String str, char escapeChar, char[] charsToEscape)
           
static int findNext(java.lang.String str, char separator, char escapeChar, int start, java.lang.StringBuilder split)
          Finds the first occurrence of the separator character ignoring the escaped separators starting from the index.
static java.lang.String formatPercent(double done, int digits)
          Format a percentage for presentation to the user.
static java.lang.String formatTime(long timeDiff)
          Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.
static java.lang.String formatTimeDiff(long finishTime, long startTime)
          Given a finish and start time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec, for the time difference between two times.
static java.lang.String getFormattedTimeWithDiff(java.text.DateFormat dateFormat, long finishTime, long startTime)
          Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff().
static java.util.Collection<java.lang.String> getStringCollection(java.lang.String str)
          Returns a collection of strings.
static java.lang.String[] getStrings(java.lang.String str)
          Returns an arraylist of strings.
static java.util.Collection<java.lang.String> getTrimmedStringCollection(java.lang.String str)
          Splits a comma separated value String, trimming leading and trailing whitespace on each value.
static java.lang.String[] getTrimmedStrings(java.lang.String str)
          Splits a comma separated value String, trimming leading and trailing whitespace on each value.
static byte[] hexStringToByte(java.lang.String hex)
          Given a hexstring this will return the byte array corresponding to the string
static java.lang.String humanReadableInt(long number)
          Given an integer, return a string that is in an approximate, but human readable format.
static java.lang.String join(java.lang.CharSequence separator, java.lang.Iterable<java.lang.String> strings)
          Concatenates strings, using a separator.
static java.lang.String limitDecimalTo2(double d)
           
static java.lang.String simpleHostname(java.lang.String fullHostname)
          Given a full hostname, return the word upto the first dot.
static java.lang.String[] split(java.lang.String str)
          Split a string using the default separator
static java.lang.String[] split(java.lang.String str, char escapeChar, char separator)
          Split a string using the given separator
static java.lang.String stringifyException(java.lang.Throwable e)
          Make a string representation of the exception.
static Path[] stringToPath(java.lang.String[] str)
           
static java.net.URI[] stringToURI(java.lang.String[] str)
           
static java.lang.String unEscapeString(java.lang.String str)
          Unescape commas in the string using the default escape char
static java.lang.String unEscapeString(java.lang.String str, char escapeChar, char charToEscape)
          Unescape charToEscape in the string with the escape char escapeChar
static java.lang.String unEscapeString(java.lang.String str, char escapeChar, char[] charsToEscape)
           
static java.lang.String uriToString(java.net.URI[] uris)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

emptyStringArray

public static final java.lang.String[] emptyStringArray

COMMA

public static final char COMMA
See Also:
Constant Field Values

COMMA_STR

public static final java.lang.String COMMA_STR
See Also:
Constant Field Values

ESCAPE_CHAR

public static final char ESCAPE_CHAR
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

stringifyException

public static java.lang.String stringifyException(java.lang.Throwable e)
Make a string representation of the exception.

Parameters:
e - The exception to stringify
Returns:
A string with exception name and call stack.

simpleHostname

public static java.lang.String simpleHostname(java.lang.String fullHostname)
Given a full hostname, return the word upto the first dot.

Parameters:
fullHostname - the full hostname
Returns:
the hostname to the first dot

humanReadableInt

public static java.lang.String humanReadableInt(long number)
Given an integer, return a string that is in an approximate, but human readable format. It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.

Parameters:
number - the number to format
Returns:
a human readable form of the integer

formatPercent

public static java.lang.String formatPercent(double done,
                                             int digits)
Format a percentage for presentation to the user.

Parameters:
done - the percentage to format (0.0 to 1.0)
digits - the number of digits past the decimal point
Returns:
a string representation of the percentage

arrayToString

public static java.lang.String arrayToString(java.lang.String[] strs)
Given an array of strings, return a comma-separated list of its elements.

Parameters:
strs - Array of strings
Returns:
Empty string if strs.length is 0, comma separated list of strings otherwise

byteToHexString

public static java.lang.String byteToHexString(byte[] bytes,
                                               int start,
                                               int end)
Given an array of bytes it will convert the bytes to a hex string representation of the bytes

Parameters:
bytes -
start - start index, inclusively
end - end index, exclusively
Returns:
hex string representation of the byte array

byteToHexString

public static java.lang.String byteToHexString(byte[] bytes)
Same as byteToHexString(bytes, 0, bytes.length).


hexStringToByte

public static byte[] hexStringToByte(java.lang.String hex)
Given a hexstring this will return the byte array corresponding to the string

Parameters:
hex - the hex String array
Returns:
a byte array that is a hex string representation of the given string. The size of the byte array is therefore hex.length/2

uriToString

public static java.lang.String uriToString(java.net.URI[] uris)
Parameters:
uris -

stringToURI

public static java.net.URI[] stringToURI(java.lang.String[] str)
Parameters:
str -

stringToPath

public static Path[] stringToPath(java.lang.String[] str)
Parameters:
str -

formatTimeDiff

public static java.lang.String formatTimeDiff(long finishTime,
                                              long startTime)
Given a finish and start time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec, for the time difference between two times. If finish time comes before start time then negative valeus of X, Y and Z wil return.

Parameters:
finishTime - finish time
startTime - start time

formatTime

public static java.lang.String formatTime(long timeDiff)
Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.

Parameters:
timeDiff - The time difference to format

getFormattedTimeWithDiff

public static java.lang.String getFormattedTimeWithDiff(java.text.DateFormat dateFormat,
                                                        long finishTime,
                                                        long startTime)
Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff(). If finish time is 0, empty string is returned, if start time is 0 then difference is not appended to return value.

Parameters:
dateFormat - date format to use
finishTime - fnish time
startTime - start time
Returns:
formatted value.

getStrings

public static java.lang.String[] getStrings(java.lang.String str)
Returns an arraylist of strings.

Parameters:
str - the comma seperated string values
Returns:
the arraylist of the comma seperated string values

getStringCollection

public static java.util.Collection<java.lang.String> getStringCollection(java.lang.String str)
Returns a collection of strings.

Parameters:
str - comma seperated string values
Returns:
an ArrayList of string values

getTrimmedStringCollection

public static java.util.Collection<java.lang.String> getTrimmedStringCollection(java.lang.String str)
Splits a comma separated value String, trimming leading and trailing whitespace on each value.

Parameters:
str - a comma separated with values
Returns:
a Collection of String values

getTrimmedStrings

public static java.lang.String[] getTrimmedStrings(java.lang.String str)
Splits a comma separated value String, trimming leading and trailing whitespace on each value.

Parameters:
str - a comma separated with values
Returns:
an array of String values

split

public static java.lang.String[] split(java.lang.String str)
Split a string using the default separator

Parameters:
str - a string that may have escaped separator
Returns:
an array of strings

split

public static java.lang.String[] split(java.lang.String str,
                                       char escapeChar,
                                       char separator)
Split a string using the given separator

Parameters:
str - a string that may have escaped separator
escapeChar - a char that be used to escape the separator
separator - a separator char
Returns:
an array of strings

findNext

public static int findNext(java.lang.String str,
                           char separator,
                           char escapeChar,
                           int start,
                           java.lang.StringBuilder split)
Finds the first occurrence of the separator character ignoring the escaped separators starting from the index. Note the substring between the index and the position of the separator is passed.

Parameters:
str - the source string
separator - the character to find
escapeChar - character used to escape
start - from where to search
split - used to pass back the extracted string

escapeString

public static java.lang.String escapeString(java.lang.String str)
Escape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an escaped string

escapeString

public static java.lang.String escapeString(java.lang.String str,
                                            char escapeChar,
                                            char charToEscape)
Escape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the char to be escaped
Returns:
an escaped string

escapeString

public static java.lang.String escapeString(java.lang.String str,
                                            char escapeChar,
                                            char[] charsToEscape)
Parameters:
charsToEscape - array of characters to be escaped

unEscapeString

public static java.lang.String unEscapeString(java.lang.String str)
Unescape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an unescaped string

unEscapeString

public static java.lang.String unEscapeString(java.lang.String str,
                                              char escapeChar,
                                              char charToEscape)
Unescape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the escaped char
Returns:
an unescaped string

unEscapeString

public static java.lang.String unEscapeString(java.lang.String str,
                                              char escapeChar,
                                              char[] charsToEscape)
Parameters:
charsToEscape - array of characters to unescape

escapeHTML

public static java.lang.String escapeHTML(java.lang.String string)
Escapes HTML Special characters present in the string.

Parameters:
string -
Returns:
HTML Escaped String representation

byteDesc

public static java.lang.String byteDesc(long len)
Return an abbreviated English-language desc of the byte length


limitDecimalTo2

public static java.lang.String limitDecimalTo2(double d)

join

public static java.lang.String join(java.lang.CharSequence separator,
                                    java.lang.Iterable<java.lang.String> strings)
Concatenates strings, using a separator.

Parameters:
separator - Separator to join with.
strings - Strings to join.