sam.toolkit
Class Text

java.lang.Object
  |
  +--sam.toolkit.Text

public class Text
extends java.lang.Object

Repository for various low-level utilities functions. This class only has static fields or methods.


Field Summary
static java.lang.String USER_GROUP
          Deprecated.  
static java.lang.String USER_NAME
          Deprecated.  
 
Method Summary
static java.util.Vector addArray(java.util.Vector vector, java.lang.Object[] additionnalElements)
          Appends specified elements to the end of the vector.
static java.lang.Object allocateNewArray(java.lang.Class type, int size)
          Allocates a new array of specified base type.
static java.lang.Object allocateNewArray(java.lang.Object array, int size)
          Allocates a new array with the same base type as the specified array.
static char bytesToChar(byte[] value)
          Converts one integer to corresponding character according to the local Charset Warning: this method is highly inefficient.
static boolean compare(byte[] x, byte[] y)
          Compares two byte arrays
static boolean compare(byte[] x, byte[] y, int yBegin)
          Compares two byte arrays
static boolean compare(byte[] x, int xBegin, byte[] y, int yBegin)
          Compares two byte arrays
static boolean compare(byte[] x, int xBegin, byte[] y, int yBegin, int yLength)
          Compares two byte arrays
static boolean compare(byte[] x, int xBegin, int xLength, byte[] y, int yBegin, int yLength)
          Compares two byte arrays
static boolean contains(java.lang.Object[] array, java.lang.Object object)
          Wether specified array contains specified object.
static int countCharacter(java.lang.String s, char c)
          Incredibly enough this is not part of the standard API ...
static int countLines(java.lang.String text)
          Counts the number of lines of a text.
static java.lang.Object[] dropNulls(java.lang.Object[] array)
          Drops the null elements of an array
static java.util.Enumeration enumerate(java.lang.Object[] array)
          Enumerates elements of an array.
static java.lang.String firstWord(java.lang.String pairOfWords, char delimiter)
          Gets the first word of a two-word string
static long fromHexa(java.lang.String s)
          Recovers an int integral number from its hexadecimal representation
static int[] fromStrings(java.lang.String[] strings)
          Turns an array of integers to the array of their String representation
static java.lang.String getDate()
          Deprecated.  
static java.lang.String getDate(long date)
          Deprecated.  
static java.lang.String getDateTime(long date)
          Deprecated.  
static java.lang.String getDuration(long duration)
          Deprecated.  
static java.lang.Object[] getKeys(java.util.Map map, java.lang.Object[] runtimeType)
          Gets a snapshot of the valid key set of a given Map.
static java.lang.String[] getKeyStrings(java.util.Map map)
          Gets a snapshot of the valid key set of a given Map.
static java.lang.String getLastClassName(java.lang.Class clazz)
          Gets the Java class name without its package qualification.
static java.lang.String getMessage(java.lang.Throwable t)
           
static java.lang.String getOneDecimalString(double ratio)
          Gets the representation of ratio with a one-decimal-digit precision.
static java.lang.String getOneDecimalString(long value, long divisor)
          Gets the representation of value / divisor with a one-decimal-digit precision.
static int getPercent(int done, int total, boolean direction)
          Deprecated.  
static int getPercent(long done, long total, boolean direction)
          Deprecated.  
static int getPercentage(long achieved, long limit)
          Gets the percentage corresponding to specified ratio.
static java.lang.String getSizeString(long bytes)
          Gets a human-readable String representation of a size.
static java.lang.String getTime()
          Deprecated.  
static java.lang.String getTime(long date)
          Deprecated.  
static char intToChar(int value)
          Converts one integer to corresponding character according to the local Charset Warning: this method is highly inefficient.
static java.lang.String left(java.lang.String text, int length)
          Shrinks or expands specified text do desired length
static java.lang.String[] merge(java.lang.String[][] stringArrays)
           
static int parseInteger(java.lang.String value)
          Parses an integer, or returns zero if incorrectly formatted
static int parseInteger(java.lang.String value, int defaultValue)
          Parses an integer, or returns defaultValue if incorrectly formatted
static java.lang.String replace(java.lang.String text, java.lang.String pattern, java.lang.String alternative)
          Replaces all occurences of a pattern in a text
static java.lang.String right(java.lang.String text, int length)
          Shrinks or expands specified text do desired length
static java.lang.String secondWord(java.lang.String pairOfWords, char delimiter)
          Gets the second word of a two-word string
static java.lang.String[] split(java.lang.String text, java.lang.String split)
          Splits a String accross specified delimiters
static java.lang.String[] splitLines(java.lang.String text)
          Splits a String accross carriage-return delimiters
static java.lang.Object subArray(java.lang.Object array, int begin, int length)
          Gets a sub-array of an array.
static java.lang.String summarize(java.lang.String text, int length)
          Shortens a String by cutting it on the right
static java.lang.String summarize(java.lang.String text, int length, boolean cutOnTheRight)
           
static java.lang.String toHexa(int n)
          Converts an int integral number to its hexadecimal representation, without trailing zeros
static java.lang.String toHexa(long n)
          Converts a long integral number to its hexadecimal representation, without trailing zeros
static java.lang.String toString(java.lang.Object[] array)
          Gets the String representation of an array.
static java.lang.String toString(java.lang.Object[] array, java.lang.String delimiter)
          Gets the String representation of an array.
static java.lang.String toString(java.lang.Object[] array, java.lang.String prefix, java.lang.String delimiter, java.lang.String postfix)
          Gets the String representation of an array.
static java.lang.String[] toStringArray(java.util.Vector vector)
          Turns a vector supposedly containing Strings only to a String[] array.
static java.lang.String[] toStrings(int[] array)
          Turns an array of integers to the array of their String representation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_NAME

public static final java.lang.String USER_NAME
Deprecated.  


USER_GROUP

public static final java.lang.String USER_GROUP
Deprecated.  

See Also:
Constant Field Values
Method Detail

countCharacter

public static int countCharacter(java.lang.String s,
                                 char c)
Incredibly enough this is not part of the standard API ...


dropNulls

public static java.lang.Object[] dropNulls(java.lang.Object[] array)
Drops the null elements of an array

Returns:
null if passed array is itself null Note that the runtime type of the returned array is that of passed array

allocateNewArray

public static java.lang.Object allocateNewArray(java.lang.Object array,
                                                int size)
Allocates a new array with the same base type as the specified array.

Returns:
null if passed type is itself null or if instantiation fails

allocateNewArray

public static java.lang.Object allocateNewArray(java.lang.Class type,
                                                int size)
Allocates a new array of specified base type.

Returns:
null if passed type is itself null or if instantiation fails

subArray

public static java.lang.Object subArray(java.lang.Object array,
                                        int begin,
                                        int length)
Gets a sub-array of an array.

Parameters:
array - the source array
begin - the beginning of the sub-array
length - the sub-array length
Returns:
a newly allocated array of same runtime type as passed array

firstWord

public static java.lang.String firstWord(java.lang.String pairOfWords,
                                         char delimiter)
Gets the first word of a two-word string


secondWord

public static java.lang.String secondWord(java.lang.String pairOfWords,
                                          char delimiter)
Gets the second word of a two-word string


splitLines

public static java.lang.String[] splitLines(java.lang.String text)
Splits a String accross carriage-return delimiters


split

public static java.lang.String[] split(java.lang.String text,
                                       java.lang.String split)
Splits a String accross specified delimiters


merge

public static java.lang.String[] merge(java.lang.String[][] stringArrays)

getKeys

public static java.lang.Object[] getKeys(java.util.Map map,
                                         java.lang.Object[] runtimeType)
Gets a snapshot of the valid key set of a given Map.

Parameters:
map - the map
runtimeType - an array of same runtime array type as the one desired, e.g. new String[0]

getKeyStrings

public static java.lang.String[] getKeyStrings(java.util.Map map)
Gets a snapshot of the valid key set of a given Map.

Parameters:
map - the map containing only String instances or nulls.

intToChar

public static char intToChar(int value)
Converts one integer to corresponding character according to the local Charset Warning: this method is highly inefficient. Moreover, it is intrinsequently buggy since some charsets require char be encoded onto several bytes


bytesToChar

public static char bytesToChar(byte[] value)
Converts one integer to corresponding character according to the local Charset Warning: this method is highly inefficient.


toStringArray

public static java.lang.String[] toStringArray(java.util.Vector vector)
Turns a vector supposedly containing Strings only to a String[] array.

Throws:
java.lang.ClassCastException - if the vector does not contain only String instances

addArray

public static java.util.Vector addArray(java.util.Vector vector,
                                        java.lang.Object[] additionnalElements)
Appends specified elements to the end of the vector.

Returns:
the original vector, or a new one if this was null and additionnalElements was not.

compare

public static boolean compare(byte[] x,
                              byte[] y)
Compares two byte arrays

Parameters:
x - the first byte[] array
y - the second byte[] array
Returns:
true iff both arrays are null or if both regions match

compare

public static boolean compare(byte[] x,
                              byte[] y,
                              int yBegin)
Compares two byte arrays

Parameters:
x - the first byte[] array
y - the second byte[] array
yBegin - the beginning of the second array region
Returns:
true iff both arrays are null or if both regions match

compare

public static boolean compare(byte[] x,
                              int xBegin,
                              byte[] y,
                              int yBegin)
Compares two byte arrays

Parameters:
x - the first byte[] array
xBegin - the beginning of the first array region
y - the second byte[] array
yBegin - the beginning of the second array region
Returns:
true iff both arrays are null or if both regions match

compare

public static boolean compare(byte[] x,
                              int xBegin,
                              byte[] y,
                              int yBegin,
                              int yLength)
Compares two byte arrays

Parameters:
x - the first byte[] array
xBegin - the beginning of the first array region
y - the second byte[] array
yBegin - the beginning of the second array region
yLength - the length of the second array region
Returns:
true iff both arrays are null or if both regions match

compare

public static boolean compare(byte[] x,
                              int xBegin,
                              int xLength,
                              byte[] y,
                              int yBegin,
                              int yLength)
Compares two byte arrays

Parameters:
x - the first byte[] array
xBegin - the beginning of the first array region
xLength - the length of the first array region
y - the second byte[] array
yBegin - the beginning of the second array region
yLength - the length of the second array region
Returns:
true iff both arrays are null or if both regions match

enumerate

public static java.util.Enumeration enumerate(java.lang.Object[] array)
Enumerates elements of an array.

Incredibly enough this is not part of the standard API.


parseInteger

public static int parseInteger(java.lang.String value)
Parses an integer, or returns zero if incorrectly formatted


parseInteger

public static int parseInteger(java.lang.String value,
                               int defaultValue)
Parses an integer, or returns defaultValue if incorrectly formatted


toHexa

public static java.lang.String toHexa(long n)
Converts a long integral number to its hexadecimal representation, without trailing zeros


toHexa

public static java.lang.String toHexa(int n)
Converts an int integral number to its hexadecimal representation, without trailing zeros


fromHexa

public static long fromHexa(java.lang.String s)
Recovers an int integral number from its hexadecimal representation


summarize

public static java.lang.String summarize(java.lang.String text,
                                         int length)
Shortens a String by cutting it on the right

Parameters:
text - the text to be shortened
length - the desired length

summarize

public static java.lang.String summarize(java.lang.String text,
                                         int length,
                                         boolean cutOnTheRight)

left

public static java.lang.String left(java.lang.String text,
                                    int length)
Shrinks or expands specified text do desired length

Parameters:
text - the text to be modified
length - the new desired length If text is null of too small, spaces are appended at the end. If >text is too long then leading characters ar dropped.

right

public static java.lang.String right(java.lang.String text,
                                     int length)
Shrinks or expands specified text do desired length

Parameters:
text - the text to be modified
length - the new desired length If text is null of too small, spaces are appended at the beginning. If >text is too long then leading characters ar dropped.

countLines

public static int countLines(java.lang.String text)
Counts the number of lines of a text.

Parameters:
text - the desired text
Returns:
the number of lines

getMessage

public static java.lang.String getMessage(java.lang.Throwable t)

getLastClassName

public static java.lang.String getLastClassName(java.lang.Class clazz)
Gets the Java class name without its package qualification. This is consistent with non-anonymous inner classes as long as they are named with '$' as a separator.


toString

public static java.lang.String toString(java.lang.Object[] array)
Gets the String representation of an array. It is incredible that this does not exist in the standard jre ...


toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String delimiter)
Gets the String representation of an array. It is incredible that this does not exist in the standard jre ...

Parameters:
delimiter - the delimiter printed between each pair of elements of the array, e.g. ", "

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String prefix,
                                        java.lang.String delimiter,
                                        java.lang.String postfix)
Gets the String representation of an array. It is incredible that this does not exist in the standard jre ...

Parameters:
delimiter - the delimiter printed between each pair of elements of the array, e.g. ", "
prefix - the prefix first printed, e.g. "["
postfix - the postfix printed at last, e.g. "]"

toStrings

public static java.lang.String[] toStrings(int[] array)
Turns an array of integers to the array of their String representation

Returns:
null if array is null

fromStrings

public static int[] fromStrings(java.lang.String[] strings)
Turns an array of integers to the array of their String representation

Returns:
null if array is null, or in case of invalid number format

contains

public static boolean contains(java.lang.Object[] array,
                               java.lang.Object object)
Wether specified array contains specified object. It is incredible that this does not exist in the standard jre ...


replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String pattern,
                                       java.lang.String alternative)
Replaces all occurences of a pattern in a text

Parameters:
text - the text to be replaced in
pattern - the pattern to be replaced
alternative - the desired substitution to pattern

getSizeString

public static java.lang.String getSizeString(long bytes)
Gets a human-readable String representation of a size. This is basically what a du -h would do to du Note that the multiplicator is 1024 for "kilo" and 1048576 for "mega"


getOneDecimalString

public static java.lang.String getOneDecimalString(double ratio)
Gets the representation of ratio with a one-decimal-digit precision.


getOneDecimalString

public static java.lang.String getOneDecimalString(long value,
                                                   long divisor)
Gets the representation of value / divisor with a one-decimal-digit precision.


getPercentage

public static int getPercentage(long achieved,
                                long limit)
Gets the percentage corresponding to specified ratio.

Parameters:
achieved - the numerator supposedly non-negative
limit - the divisor supposedly non-negative
Returns:
100 times the ratio (rounded)

getPercent

public static int getPercent(int done,
                             int total,
                             boolean direction)
Deprecated.  


getPercent

public static int getPercent(long done,
                             long total,
                             boolean direction)
Deprecated.  


getDateTime

public static java.lang.String getDateTime(long date)
Deprecated.  


getDate

public static java.lang.String getDate()
Deprecated.  


getDate

public static java.lang.String getDate(long date)
Deprecated.  


getTime

public static java.lang.String getTime()
Deprecated.  


getTime

public static java.lang.String getTime(long date)
Deprecated.  


getDuration

public static java.lang.String getDuration(long duration)
Deprecated.