Class StrComparator

  extended by net.sf.ivmaidns.util.GComparator
      extended by net.sf.ivmaidns.util.StrComparator
All Implemented Interfaces:, Immutable, Metricable

public class StrComparator
extends GComparator
implements Metricable

Class for string comparators/metrics. This comparator class overrides the default semantics of GComparator class for the case when both of the objects being compared are instances of String class. This class also implements Metricable interface to offer some assessment metrics for string objects. Here, case-insensitive comparison of strings is implemented (GComparator class implementation offers case-sensitive comparison for string objects), and the metrics is string length plus one. In the subclasses compare(String, String) method should be overridden to implement specific comparison and metrics.

Ivan Maidanski
See Also:
CharVector, Serialized Form

Field Summary
static GComparator INSTANCE
          An instance of this comparator.
Constructor Summary
          Constructs a new comparator.
Method Summary
 int compare(java.lang.String strA, java.lang.String strB)
          Compares two given strings.
 int evaluate(java.lang.Object obj)
          The body of the metrics.
 boolean greater(java.lang.Object objA, java.lang.Object objB)
          The body of 'Greater-Than' comparator.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


public static final GComparator INSTANCE
An instance of this comparator. This constant field is initialized with the instantiation of exactly this comparator (hides INSTANCE of the superclass). The implemented comparator orders correctly strings ignoring letters case, other objects are ordered in the same way as by GComparator exact instance; evaluate(Object) method for a string here returns its length() plus one, for other objects zero is returned.

See Also:
greater(java.lang.Object, java.lang.Object), evaluate(java.lang.Object)
Constructor Detail


public StrComparator()
Constructs a new comparator. This constructor is made public only to allow custom dynamic instantiation of this class. In other cases, INSTANCE should be used.

See Also:
Method Detail


public final boolean greater(java.lang.Object objA,
                             java.lang.Object objB)
The body of 'Greater-Than' comparator. Tests whether or not the first specified object is greater than the second one. If both arguments are of String class then (compare(objA, objB) > 0) is returned, else greater(objA, objB) of the superclass is returned.

greater in class GComparator
objA - the first compared argument (may be null).
objB - the second compared argument (may be null).
true if and only if objA is greater than objB.
See Also:
INSTANCE, compare(java.lang.String, java.lang.String)


public final int evaluate(java.lang.Object obj)
The body of the metrics. Evaluates the supplied object. If obj is of String class then compare(null, obj) is returned, else this method always returns 0.

Specified by:
evaluate in interface Metricable
obj - the object (may be null) to evaluate.
the integer result of the performed evaluation.
See Also:
INSTANCE, compare(java.lang.String, java.lang.String)


public int compare(java.lang.String strA,
                   java.lang.String strB)
Compares two given strings. This method returns a signed integer indicating 'less-equal-greater' relation between the specified strings (the absolute value of the result, in fact, is the distance between the first found mismatch and the end of the bigger-length string). This method should be overridden in subclasses to implement specific string comparison rules (if not, this method compares strings in the case-insensitive manner and the metrics (when strA == null) is length() of strB plus one). Important notes: zero is returned if strA == strB; this function is always asymmetrical.

strA - the first compared string (may be null).
strB - the second compared string (may be null).
a negative integer, zero, or a positive integer as strA object is less than, equal to, or greater than strB one.
See Also:
INSTANCE, greater(java.lang.Object, java.lang.Object), evaluate(java.lang.Object)