|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.CompareStrings
public class CompareStrings
Base class to help build comparators.
This class can be used as a standard string comparator, however, it is intended to serve
more as a base for building custom comparators. It provides the following functionality:
Refer to the CStrings.java example program to see how to use this class directly.
If you extend this class (as it is intended to be), you will
want to implement a custom compare method, be sure
to pass your comparison results through the adjustCompare method before returning the final value (this will
handle the ascending/desceding option for you). This looks
something like the following:
public int compare(Object o1, Object o2) {
int rc = 0; // assume objects will be equal
... // insert your custom code to compare
return adjustCompare(rc); // allow your comparison value to be adjusted
}
Arrays,
Comparator,
CString.java| Constructor Summary | |
|---|---|
CompareStrings()
|
|
| Method Summary | |
|---|---|
protected int |
adjustCompare(int cv)
Adjust the "compare value" computed. |
int |
compare(Object o1,
Object o2)
Compare the String representations of two objects. |
static int |
compareObjects(Object o1,
Object o2)
Compare two the String values of two objects (allow for null). |
static int |
compareStrings(String s1,
String s2)
Compare two String objects where one or more could be null. |
boolean |
isDescending()
Is a descending sort to be generated set? |
void |
setDescending(boolean val)
Set whether a descending sort should be generated. |
| 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 |
| Constructor Detail |
|---|
public CompareStrings()
| Method Detail |
|---|
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 is likely to be overridden by derived classes. This implementation uses the following approach:
compareObjects method to
get an initial compare value for the two objects passed (which
tries to look at the string representation of the objects when
possible).
adjustCompare(int) method, which may negate the result if this
object has been configured to do a descending sort.
compare in interface Comparatoro1 - Object to compare against.o2 - Object to compare against o1.
ClassCastExceptionsetDescending(boolean)
public static final int compareStrings(String s1,
String s2)
String objects where one or more could be null.
This method compares to strings to determine which
is less than or equal to the other. It is similar to compareTo method in the String class, except that it allows one to pass null values
(null values are considered to be less than non-null values). If
both values passed are not null, then the results returned will
be the same as if one did: o1.compareTo(o2).
o1 - String to compare against (null is OK).o2 - String to compare against o1 (null is OK).
compareObjects(java.lang.Object, java.lang.Object)
public static final int compareObjects(Object o1,
Object o2)
String values of two objects (allow for null).
This method compares to objects to
determine which is less than or equal to the other. It allows one
to pass null values (null values are considered to be less than
non-null values). If both values passed are not null, then the
string values of the two objects will be retrieved and processed
by the compareStrings(java.lang.String, java.lang.String) method.
o1 - Object to compare against (null is OK).o2 - Object to compare against o1 (null is OK).
compareStrings(java.lang.String, java.lang.String)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)protected int adjustCompare(int cv)
This method should be used by all derived classes when they
implement the compare method. It will make any
last minute adjustments to the value returned. It's main purpose
is to automatically handle the ascending/descending option for
all derived classes.
cv - Standard integer "compare value" as described in the compare method.
setDescending(boolean)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||