|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.StringUtils
public class StringUtils
Set of functions related to String
objects and arrays of String objects.
Seems like there are always a set of common manipulations which are done to string objects or arrays of string objects. This class attempts to encapsulate some of these common needs into a single location.
A user of this class will need to look at each of the methods to decide what is useful for their application/purpose.
As an example of some of the power of these functions, lets
assume that you have a hash table where the keys are string
identifiers. The following code fragment would returns all of the
keys from your hash table as a sorted array:
public String[] getSortedKeys(Hashtable ht) {
StringUtils su = new StringUtils();
String[] sa = su.toArray(ht.keys());
return su.sortDup(sa);
}
NOTE: This class also acts as a Comparator for String objects and is preferable to the CompareStrings class - which I may end up
deprecating in the future.
| Field Summary | |
|---|---|
static String[] |
EMPTY_ARRAY
A constant (which everyone may use) that defines a zero length array of string objects. |
| Constructor Summary | |
|---|---|
StringUtils()
Default constructor for the class. |
|
| Method Summary | |
|---|---|
int |
compare(Object o1,
Object o2)
Compare the String representations of two objects. |
String |
getBlankReplace()
Get the string value to replace blank strings (zero length) with when converting collections of strings. |
Comparator |
getComparator()
Get the comparator to use when sorting strings. |
String |
getNullReplace()
Get the string value to replace 'null' entries with when converting collections. |
boolean |
isBlankSkipped()
Should we skip (omit) 'blank' string objects when working with collections? |
boolean |
isCaseIgnored()
Is case ignored when making string comparisons? |
boolean |
isDescending()
Is a descending sort to be generated set? |
boolean |
isNullSkipped()
Should we skip (omit) 'null' string objects when working with collections? |
static String |
repeat(char fill,
int cnt)
Create a string which contains a single character repeated zero or more times. |
void |
setBlankReplace(String val)
Set the string value to replace blank strings (zero length) with when converting collections of strings. |
void |
setBlankSkipped(boolean val)
Set whether we should we skip (omit) 'blank' string objects when working with collections. |
void |
setCaseIgnored(boolean val)
Set whether case should be ignored when making string comparisons. |
void |
setComparator(Comparator val)
Set the comparator to use when sorting strings. |
void |
setDescending(boolean val)
Set whether a descending sort should be generated. |
void |
setNullReplace(String val)
Set the string value to replace 'null' entries with when converting collections. |
void |
setNullSkipped(boolean val)
Set whether we should we skip (omit) 'null' string objects when working with collections. |
void |
sort(String[] sa)
Sorts an array of string objects in place (modifying array passed). |
String[] |
sortDup(String[] sa)
Sorts an array of string objects, returning a new array of sorted strings. |
String[] |
toArray(Enumeration e)
Convert Enumeration of objects to
a array of strings. |
String[] |
toArray(Iterator iter)
Converts a collection of strings to a array of strings imposing the skip and replacement rules. |
static String |
toString(boolean val)
Convert a boolean primitive to its string representation. |
static String |
toString(byte val)
Convert a byte primitive to its string representation. |
static String |
toString(char val)
Convert a char primitive to its string representation. |
static String |
toString(double val)
Convert a double primitive to its string representation. |
static String |
toString(float val)
Convert a float primitive to its string representation. |
String |
toString(InputStream in)
Read the the contents of a InputStream into
a String. |
static String |
toString(int val)
Convert a int primitive to its string representation. |
static String |
toString(long val)
Convert a long primitive to its string representation. |
String |
toString(Object o)
Convert any object to its string representation (safely checking for null and blank values). |
static String |
toString(short val)
Convert a short primitive to its string representation. |
Vector |
toVector(Enumeration e)
Convert Enumeration of objects to
a Vector of Strings. |
Vector |
toVector(Iterator i)
Converts a collection of strings to a Vector of
strings imposing the skip and replacement rules. |
Vector |
toVector(String s)
Read all lines of text from a single string and return a Vector of each line read. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface java.util.Comparator |
|---|
equals |
| Field Detail |
|---|
public static final String[] EMPTY_ARRAY
This constant defines a array of strings which contains zero entries. It is non-mutable, so it is freely useable by ALL code within the JVM.
If used wisely, this does the following:
| Constructor Detail |
|---|
public StringUtils()
toArray(Iterator)
method).
| Method Detail |
|---|
public static String repeat(char fill,
int cnt)
fill - The character to repeat.cnt - How many times the character should be repeated (0 or larger).
public void sort(String[] sa)
This method sorts an array of string objects in place
(updating the array passed). It should be noted that if your
array contains null values or empty strings, then the Comparator associated with the object must be
able to handle these conditions (the default one used - if you
don't specify one - is capable of this).
You may want to take a look at the sortDup(java.lang.String[]) method
(which leaves your original array alone).
sa - Array of String objects to be sorted (you may pass null).sortDup(java.lang.String[])public String[] sortDup(String[] sa)
This method sorts an array of string objects returning a new array of sorted strings. It should be noted that if your original array contained null values or empty strings, then the returned array may omit or replace these values depending upon the 'skip' and 'replace' rules which are in affect.
sa - Array of String objects to be sorted (you may pass null). This
method does NOT modify this arrays contents.
EMPTY_ARRAY.sort(java.lang.String[])public Vector toVector(Iterator i)
Vector of
strings imposing the skip and replacement rules.
This method goes through a iterator and constructs a Vector objecting containing JUST string representations
of each entry iterated over. This imposes the 'skip' and
'replacement' rules currently in affect (null and blank values
might be skipped or replaced in the process of building the
resulting vector).
NOTE: The 'skip' rules are imposed PRIOR to the 'replace'
rules. For example, if you set the null
skipped rule to true and null
replacement to "NULL" and null values encountered would
be 'skipped' and NOT replaced by "NULL". The replacement value of
"NULL" would never be used as the 'skip' rule was imposed first.
i - Iterator which we should step through to build our vector of
strings (you can pass null - in which case we'll return
EMPTY_ARRAY).
setNullSkipped(boolean),
setBlankSkipped(boolean),
setNullReplace(java.lang.String),
setBlankReplace(java.lang.String),
toArray(Iterator),
EnumerationIteratorpublic String[] toArray(Iterator iter)
This method goes through a iterator and constructs a string array containing string representations of each entry iterated over. This imposes the 'skip' and 'replacement' rules currently in affect (null and blank values might be skipped or replaced in the process of building the resulting vector).
NOTE: The 'skip' rules are imposed PRIOR to the 'replace'
rules. For example, if you set the null
skipped rule to true and null
replacement to "NULL" and null values encountered would
be 'skipped' and NOT replaced by "NULL". The replacement value of
"NULL" would never be used as the 'skip' rule was imposed first.
i - Iterator which we should step through to build our array of
strings (you can pass null - in which case we'll return
EMPTY_ARRAY).
EMPTY_ARRAY.setNullSkipped(boolean),
setBlankSkipped(boolean)public Vector toVector(Enumeration e)
Enumeration of objects to
a Vector of Strings.
This is a convenience method which converts the Enumeration passed into a Iterator and then invokes toVector(Iterator).
e - Enumeration which we should step through to build our vector of
strings (you can pass null - in which case we'll return EMPTY_ARRAY).
EMPTY_ARRAY.toVector(Iterator)public Vector toVector(String s)
Vector of each line read.
This is a convenience method which expects to be passed a
single string containing multiple lines of text. Each line of
text is added to a Vector which is then returned.
s - String containing multiple lines of text (you can pass null or
single lines of text as well).
toVector(Iterator)public String[] toArray(Enumeration e)
Enumeration of objects to
a array of strings.
This is a convenience method which converts the Enumeration passed into a Iterator and then invokes toArray(Iterator).
e - Enumeration which we should step through to build our array of
strings (you can pass null - in which case we'll return EMPTY_ARRAY).
EMPTY_ARRAY.toArray(Iterator)
public String toString(InputStream in)
throws IOException
InputStream into
a String.
This method reads the entire contents of a InputStream and returns the contents as a String.
NOTE: The entire contents of the stream are read and processed - so this should NOT be used with large files.
in - InputStream to read (in its entirety) - if you pass null, you
get "" back.
IOException - If an error occurs while reading contents from input stream.public static String toString(boolean val)
This method converts a boolean value to its corresponding
string representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Boolean object as demonstrated in the following:
Boolean toObject(boolean val) {
return new Boolean(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - boolean value to get a string representation of.
public static String toString(byte val)
This method converts a byte value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Byte object as demonstrated in the following:
Byte toObject(byte val) {
return new Byte(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - byte value to get a string representation of.
public static String toString(char val)
This method converts a char value to its corresponding string representation in a language independent (non-localized) form.
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - char value to get a string representation of.
public static String toString(short val)
This method converts a short value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Short object as demonstrated in the following:
Short toObject(short val) {
return new Short(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - short value to get a string representation of.
public static String toString(int val)
This method converts a int value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Integer object as demonstrated in the following:
Integer toObject(int val) {
return new Integer(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - int value to get a string representation of.
public static String toString(long val)
This method converts a long value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Long object as demonstrated in the following:
Long toObject(long val) {
return new Long(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - long value to get a string representation of.
public static String toString(float val)
This method converts a float value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Float object as demonstrated in the following:
Float toObject(float val) {
return new Float(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - float value to get a string representation of.
public static String toString(double val)
This method converts a double value to its corresponding string
representation in a language independent (non-localized)
form. This is done in a manner such that the string value
returned could be later used to initialize a Double object as demonstrated in the following:
Double toObject(double val) {
return new Double(StringUtils.toString(val));
}
This can be handy if you want to save the state of primitives in a ASCII form that you will want to be able to restore later.
val - double value to get a string representation of.
public String toString(Object o)
Often one finds themselves retrieving string values of objects in one of the following manner:
// Direct cast approach
Object o = "Hello";
String s0 = (String) o;
System.println(s0);
String s1 = o.toString();
System.println(s1);
The above works well if you know that your object is not null AND that its string representation is not null AND (in the 'Direct cast' approach) is a String object. However, if any of these are violated, the above code will break.
This method, provides a "safer" way to do this type of casting. It handles the condition when you pass null, a object which is not a string or a object who's string representation is null. In addition, it allows you to remap the null and blank values of the string. For example:
StringUtils su = new StringUtils();
// Prints: ":::hello"
System.out.println(":"+su.toString(null)+":"+
su.toString("")+":"+su.toString("hello"));
// Prints: ":X:Y:hello"
su.setNullReplace("X");
su.setBlankReplace("Y");
System.out.println(":"+su.toString(null)+":"+
su.toString("")+":"+su.toString("hello"));
o - Object which you want to get the string value for (may be null).
blank or null
replacement value.public void setNullReplace(String val)
val - New String value to assign.getNullReplace()public String getNullReplace()
setNullReplace(java.lang.String)public void setBlankReplace(String val)
val - New String value to assign.getBlankReplace()public String getBlankReplace()
setBlankReplace(java.lang.String)public void setNullSkipped(boolean val)
val - New boolean value to assign.isNullSkipped()public boolean isNullSkipped()
setNullSkipped(boolean)public void setBlankSkipped(boolean val)
val - New boolean value to assign.isBlankSkipped()public boolean isBlankSkipped()
setBlankSkipped(boolean)public void setComparator(Comparator val)
val - New Comparator value to assign.getComparator()public Comparator getComparator()
setComparator(java.util.Comparator)public void setDescending(boolean val)
This method can be used to control whether the comparator is to be used for a ascending or descending sort. At the time of construction, a ascending sort is assumed ("a", "b", "c", ...). If you pass true to this method, you'll get a descending sort ("z", "y", "x", ...).
val - false for standard ascending compare, true to reverse the sort
to create a descending compare.isDescending()public boolean isDescending()
setDescending(boolean)public void setCaseIgnored(boolean val)
val - New boolean value to assign.isCaseIgnored()public boolean isCaseIgnored()
setCaseIgnored(boolean)
public int compare(Object o1,
Object o2)
throws ClassCastException
String representations of two objects.
This is the method that all Comparator's must
implement. It determines if one object (o1) is less than, equal
to or greater than another object (o2).
This method can be overridden by derived classes. This implementation uses the following approach:
descending sort.
compare in interface Comparatoro1 - Object to compare against.o2 - Object to compare against o1.
ClassCastExceptionsetDescending(boolean),
setCaseIgnored(boolean)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||