public abstract static class StringComparator.Base extends java.lang.Object implements StringComparator
compare(java.lang.String, java.lang.String)
begins by comparing the strings as objects, examining
their nullness/emptiness. If that algorithm is already enough to identify
the "greater" string, then there is no need for the actual implementation
class to do anything further. Otherwise, the implementation class's
executeComparison(java.lang.String, java.lang.String)
may proceed already knowing that the strings
are non-null and non-empty, and that String.equals(java.lang.Object)
has already
been tried and is not worth trying again.StringComparator.Base
Modifier and Type | Field and Description |
---|---|
protected static int |
EVALUATION_INDETERMINATE
Semantic constant not intended as an actual return value; instead,
the internal
compareAsObjects(java.lang.String, java.lang.String) method will return this when
its naïve evaluation of the strings (comparing their nullness
and emptiness) isn't enough to resolve the comparison. |
EQUAL, S1_IS_GREATER, S2_IS_GREATER
Constructor and Description |
---|
Base() |
Modifier and Type | Method and Description |
---|---|
int |
compare(java.lang.String s1,
java.lang.String s2)
Executes
compareAsObjects(java.lang.String, java.lang.String) first to determine whether there
is already a clear winner, and if not, calls the comparator's more
specific executeComparison(java.lang.String, java.lang.String) method. |
protected static int |
compareAsObjects(java.lang.String s1,
java.lang.String s2)
Compares the strings as objects — that is, it bases its
comparison on the simple question of whether one or both of the
strings is null or empty.
|
protected abstract int |
executeComparison(java.lang.String s1,
java.lang.String s2)
Executes the comparison that is specific to this class.
|
protected static final int EVALUATION_INDETERMINATE
compareAsObjects(java.lang.String, java.lang.String)
method will return this when
its naïve evaluation of the strings (comparing their nullness
and emptiness) isn't enough to resolve the comparison.protected static int compareAsObjects(java.lang.String s1, java.lang.String s2)
s1.equals(s2)
are equal.s1
- the first string to compares2
- the second string to compareStringComparator.EQUAL
(0) if the strings are equalStringComparator.S1_IS_GREATER
(1) if the first is "greater"StringComparator.S2_IS_GREATER
(-1) if the second is "greater"EVALUATION_INDETERMINATE
if this method's
naïve algorithm was not enough to figure out which is
greater
protected abstract int executeComparison(java.lang.String s1, java.lang.String s2)
compare(java.lang.String, java.lang.String)
is used as-is, then this
method is guaranteed that the strings are non-null, non-empty, and
that String.equals(java.lang.Object)
has returned false
.s1
- the first string to compares2
- the second string to compareStringComparator.EQUAL
(0) if the strings are equalStringComparator.S1_IS_GREATER
(1) if the first is "greater"StringComparator.S2_IS_GREATER
(-1) if the second is "greater"public int compare(java.lang.String s1, java.lang.String s2)
compareAsObjects(java.lang.String, java.lang.String)
first to determine whether there
is already a clear winner, and if not, calls the comparator's more
specific executeComparison(java.lang.String, java.lang.String)
method. Implementation classes
must override this method if they don't want compareAsObjects
to be executed first.
This method does not throw a NullPointerException
,
as it is specifically designed to handle null values.
compare
in interface java.util.Comparator<java.lang.String>
s1
- the first string to compares2
- the second string to compareStringComparator.EQUAL
(0) if the strings are equalStringComparator.S1_IS_GREATER
(1) if the first is "greater"StringComparator.S2_IS_GREATER
(-1) if the second is "greater"EVALUATION_INDETERMINATE
if this method's
naïve algorithm was not enough to figure out which is
greater