Source for org.jfree.chart.HashUtilities

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------
  28:  * HashUtilities.java
  29:  * ------------------
  30:  * (C) Copyright 2006, 2007, by Object Refinery Limited;
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: HashUtilities.java,v 1.1.2.2 2007/03/06 16:00:44 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 03-Oct-2006 : Version 1 (DG);
  40:  * 06-Mar-2007 : Fix for hashCodeForDoubleArray() method (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart;
  45: 
  46: import java.awt.GradientPaint;
  47: import java.awt.Paint;
  48: 
  49: /**
  50:  * Some utility methods for calculating hash codes.  
  51:  * 
  52:  * @since 1.0.3
  53:  */
  54: public class HashUtilities {
  55:     
  56:     /**
  57:      * Returns a hash code for a <code>Paint</code> instance.  If 
  58:      * <code>p</code> is <code>null</code>, this method returns zero.
  59:      * 
  60:      * @param p  the paint (<code>null</code> permitted).
  61:      * 
  62:      * @return The hash code.
  63:      */
  64:     public static int hashCodeForPaint(Paint p) {
  65:         if (p == null) 
  66:             return 0;
  67:         int result = 0;
  68:         // handle GradientPaint as a special case
  69:         if (p instanceof GradientPaint) {
  70:             GradientPaint gp = (GradientPaint) p;
  71:             result = 193;
  72:             result = 37 * result + gp.getColor1().hashCode();
  73:             result = 37 * result + gp.getPoint1().hashCode();
  74:             result = 37 * result + gp.getColor2().hashCode();
  75:             result = 37 * result + gp.getPoint2().hashCode();
  76:         }
  77:         else {
  78:             // we assume that all other Paint instances implement equals() and
  79:             // hashCode()...of course that might not be true, but what can we
  80:             // do about it?
  81:             result = p.hashCode();
  82:         }
  83:         return result;
  84:     }
  85:     
  86:     /**
  87:      * Returns a hash code for a <code>double[]</code> instance.  If the array
  88:      * is <code>null</code>, this method returns zero.
  89:      * 
  90:      * @param a  the array (<code>null</code> permitted).
  91:      * 
  92:      * @return The hash code.
  93:      */
  94:     public static int hashCodeForDoubleArray(double[] a) {
  95:         if (a == null) { 
  96:             return 0;
  97:         }
  98:         int result = 193;
  99:         long temp;
 100:         for (int i = 0; i < a.length; i++) {
 101:             temp = Double.doubleToLongBits(a[i]);
 102:             result = 29 * result + (int) (temp ^ (temp >>> 32));
 103:         }
 104:         return result;
 105:     }
 106: 
 107: }