Class StringComparator

  • All Implemented Interfaces:
    java.util.Comparator

    public class StringComparator
    extends java.lang.Object
    implements java.util.Comparator
    A very simple implementation of the Comparator interface. It is suitable for use on any objects for which the toString method provides a suitable basis for object comparison.

    You might think that calling java.text.Collator.getInstance ought to provide much the same thing; however that gives you a compare method which casts to String rather than calling the toString method on its arguments, leading to a ClassCastException in most useful cases. I wonder why it does that?

    Version:
    $Id$
    Author:
    Mark Taylor (Starlink)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.Object o1, java.lang.Object o2)  
      boolean equals​(java.lang.Object obj)  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • StringComparator

        public StringComparator()
    • Method Detail

      • compare

        public int compare​(java.lang.Object o1,
                           java.lang.Object o2)
        Specified by:
        compare in interface java.util.Comparator
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Comparator
        Overrides:
        equals in class java.lang.Object