TopoWeb Commons API

org.topoweb.util
Class StringComparator

java.lang.Object
  |
  +--org.topoweb.util.StringComparator
All Implemented Interfaces:
java.util.Comparator

public class StringComparator
extends java.lang.Object
implements java.util.Comparator

Comparator that uses the string representation of objects (Object.toString()). Additionally, the value null is taken to mean the largest possible value in this total ordering. This comparator works with all objects as every object has a toString() method.

Note: this comparator imposes orderings that are inconsistent with equals. The only situations where the ordering is consistent with equals is if for every pair of elements (a, b) in a set s of non-null elements, the expression a.toString().equals(b.toString()) implies that a.equals(b). The trivial case where the ordering is consistent with equals is when all the objects compared are Strings.

Author:
Babak Farhang

Constructor Summary
StringComparator()
           
 
Method Summary
 int compare(java.lang.Object a, java.lang.Object b)
          Compares the two objects a and b using their string representations.
 boolean equals(java.lang.Object other)
          Tests whether this instance is equal to another.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringComparator

public StringComparator()
Method Detail

compare

public final int compare(java.lang.Object a,
                         java.lang.Object b)
Compares the two objects a and b using their string representations. Additionally, the value null for either of the arguments, represents the largest possible value. This method never throws an exception.

Specified by:
compare in interface java.util.Comparator
Parameters:
a - the first argument
b - the second argument
Returns:
  • if either a or b is null:
    -1, if b is null but a is not; 0, if both a and b are null; 1, if a is null but b is not
  • otherwise:
    a.toString().compareTo(b.toString())

equals

public final boolean equals(java.lang.Object other)
Tests whether this instance is equal to another.

Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class java.lang.Object
Returns:
true, iff other is an instance of StringComparator

TopoWeb Commons API

Copyright (C) 2002 Babak Farhang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.