|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.util.StringComparator
public class StringComparator
StringComparator is used to compare two string values. This is most often done when sorting collections of stringss. You can also use StringComparator for non-string items; it invokes the toString() method of objects and compares using the resulting strings.
StringComparator provides a bit of extra smarts: you can specify that the comparison start at a character offset in the strings, instead of a default 0, or that case be ignored (although this is not always appropriate for some character sets).
OrderedCollection
,
StringCollection
,
Serialized FormField Summary | |
---|---|
static StringComparator |
defaultInstance
If you wish to use a default StringComparator , you can use
defaultInstance to avoid creating a new instance. |
Fields inherited from interface com.sas.util.Comparator |
---|
EQUALS, GREATER_THAN, INCOMPARABLE, LESS_THAN |
Constructor Summary | |
---|---|
StringComparator()
Default constructor. |
|
StringComparator(boolean ascending)
Construct a string based comparator. |
|
StringComparator(boolean ascending,
boolean caseSensitive)
Construct a string based comparator. |
|
StringComparator(boolean ascending,
boolean caseSensitive,
int startChar)
Construct a string based comparator. |
|
StringComparator(java.text.Collator collator)
Construct a StringComparator which uses a Collator
to perform the comparison. |
Method Summary | |
---|---|
java.lang.Object |
clone()
Clone this StringComparator |
int |
compare(java.lang.Object first,
java.lang.Object second)
Compare two values. |
java.text.Collator |
getCollator()
Return the collator used to compare the strings. |
int |
getStartChar()
Return the character at which string comparison begins. |
boolean |
isAscending()
Return true if this StringComparator is performing an ascending
comparison. |
boolean |
isCaseSensitive()
Indicate if the comparison is case sensitive or not. |
void |
setAscending(boolean ascending)
Set the StringComparator to an ascending sort comparison (the default). |
void |
setCaseSensitive(boolean caseSensitive)
Set the case sensitivity of the StringComparator
|
void |
setCollator(java.text.Collator newCollator)
Set the collator used to compare strings. |
void |
setStartChar(int startChar)
Set the character at which string comparisons are performed. |
java.lang.String |
toString()
Return a string representation of the comparator |
Field Detail |
---|
public static final StringComparator defaultInstance
StringComparator
, you can use
defaultInstance
to avoid creating a new instance.
It is initialized to new StringComparator()
and thus compares
strings in ascending order, honoring case, starting at character 0.
Note: Java does not provide a means to define const objects,
so nothing prevents you from calling one of the below set
methods on defaultInstance
, but doing so is highly discouraged,
as other packages may be relying on defaultInstance
to have
the default behavior.
Constructor Detail |
---|
public StringComparator()
String
representation.
public StringComparator(java.text.Collator collator)
StringComparator
which uses a Collator
to perform the comparison.
collator
- a collator. You can use Collator.getInstance()
to construct the default Collator for the current Locale
.
public StringComparator(boolean ascending)
String
representation.
ascending
- if false, negate the default comparison.public StringComparator(boolean ascending, boolean caseSensitive)
String
representation, optionally ignoring case differences.
ascending
- if false
, negate the default comparison.caseSensitive
- if true
, the comparison ignores case.
Warning: case insensitive string comparisons are not possible
or correct in all languages/character encodings.public StringComparator(boolean ascending, boolean caseSensitive, int startChar)
String
representation.
ascending
- if false
, negate the default comparison.caseSensitive
- if true
, the comparison ignores case.
Warning: case insensitive string comparisons are not possible
or correct in all languages/character encodings.startChar
- the starting character for comparisons.
characters before the startChar are ignored.Method Detail |
---|
public boolean isAscending()
true
if this StringComparator
is performing an ascending
comparison.
true
if compare("A", "B")
will return LESS
_THANpublic void setAscending(boolean ascending)
StringComparator
to an ascending sort comparison (the default).
ascending
- true
if you want an ascending string {"A", "B", "C"}
;
false
if you want a descending sort {"C", "B", "A"}
, for example.public boolean isCaseSensitive()
true
if the comparison ignores case by comparing
only the String.toLowerCase()
version of strings.public void setCaseSensitive(boolean caseSensitive)
StringComparator
.
- Parameters:
caseSensitive
- if t
rue, the comparator will
honor the case of the strings; if f
alse, comparisons
are done on the toLowerCase()
results. If the StringComparator
was constructed with a Collator
, this method will set the
collator's strength to Collator.TERTIARY
if caseSensitive is tr
ue,
and Collator.PRIMARY
if fal
se. If you need finer granularity,
you can modify the collator directly.public void setCollator(java.text.Collator newCollator)
newCollator
- the new value for the collator property.
If null
, then simple string comparison is used.public java.text.Collator getCollator()
null
if no collator exists, in which case the
StringComparator
uses default string comparison.
public int getStartChar()
public void setStartChar(int startChar)
startChar
- the character index where comparison begins;
characters before this are ignored. If a string is less than
startChar characters long, it is compared as an empty
string; i.e. less than other strings in an ascending sort.public int compare(java.lang.Object first, java.lang.Object second)
StringComparator
is constructed with ascending == true
,
the compare()
method returns LESS_THAN
if the toString()
value
of the first object compares less than the toString()
value of
the second, else EQUALS
or GREATER_THAN
. A nu
ll item
is sorted LESS_THAN
a non-null item in an ascending comparison.
- Specified by:
compare
in interface java.util.Comparator
- Parameters:
first
- the left hand side of the comparisonsecond
- the right hand side of the comparison
- Returns:
LESS_THAN the first object compares "less than" the second, meaning it
should appear before the second in the sorted result
EQUALS the objects are equal
GREATER_THAN the first object compares greater than the first, meaning it
should appear after the second in the sorted result
(swap results if ascending == false)- See Also:
Comparator.LESS_THAN
,
Comparator.EQUALS
,
Comparator.GREATER_THAN
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
StringComparator
clone
in interface com.sas.PublicClonable
clone
in class com.sas.Component
StringComparator
java.lang.CloneNotSupportedException
- if the string comparator cannot be cloned.public java.lang.String toString()
toString
in class java.lang.Object
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |