001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: HashCodeComparator.java,v $
023   Revision 1.6  2004/05/31 07:25:05  markl
024   handle nulls in comparison method
025
026   Revision 1.5  2004/05/05 21:22:45  markl
027   Comment header updates.
028
029   Revision 1.4  2004/05/05 18:31:16  markl
030   added equals() method
031   ----------------------------------------------------------------------------
032*/
033
034package kiwi.util;
035
036import java.util.*;
037
038/** A hash code comparator. This class compares the hash codes of two objects.
039 *
040 * @author Mark Lindner
041 */
042
043public class HashCodeComparator implements Comparator
044  {
045
046  /** Construct a new <code>HashCodeComparator</code>.
047   */
048  
049  public HashCodeComparator()
050    {
051    }
052  
053  /** Compare the hash codes of two objects. A
054   * <code>hashCode()</code> is performed on both objects, and then the
055   * resulting integers are compared.
056   *
057   * @param a The first object.
058   * @param b The second object.
059   *
060   * @return <code>0</code> if the objects are equal, <code>-1</code>
061   * if <code>a</code> is less than <code>b</code>, and <code>1</code>
062   * if <code>a</code> is greater than <code>b</code>.
063   */
064
065  public int compare(Object a, Object b)
066    {
067    if((a == null) && (b != null))
068      return(-1);
069    else if((a != null) && (b == null))
070      return(1);
071    else if((a == null ) && (b == null))
072      return(0);
073    
074    int ha = a.hashCode(), hb = b.hashCode();
075
076    return((ha == hb) ? 0 : ((ha > hb) ? 1 : -1));
077    }
078
079  /*
080   */
081
082  public boolean equals(Object obj)
083    {
084    return(obj.getClass() == HashCodeComparator.class);
085    }
086  
087  }
088
089/* end of source file */