Frames | No Frames |
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: * Changes 36: * ------- 37: * 03-Oct-2006 : Version 1 (DG); 38: * 06-Mar-2007 : Fix for hashCodeForDoubleArray() method (DG); 39: * 13-Nov-2007 : Added new utility methods (DG); 40: * 41: */ 42: 43: package org.jfree.chart; 44: 45: import java.awt.GradientPaint; 46: import java.awt.Paint; 47: import java.awt.Stroke; 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: } 68: int result = 0; 69: // handle GradientPaint as a special case 70: if (p instanceof GradientPaint) { 71: GradientPaint gp = (GradientPaint) p; 72: result = 193; 73: result = 37 * result + gp.getColor1().hashCode(); 74: result = 37 * result + gp.getPoint1().hashCode(); 75: result = 37 * result + gp.getColor2().hashCode(); 76: result = 37 * result + gp.getPoint2().hashCode(); 77: } 78: else { 79: // we assume that all other Paint instances implement equals() and 80: // hashCode()...of course that might not be true, but what can we 81: // do about it? 82: result = p.hashCode(); 83: } 84: return result; 85: } 86: 87: /** 88: * Returns a hash code for a <code>double[]</code> instance. If the array 89: * is <code>null</code>, this method returns zero. 90: * 91: * @param a the array (<code>null</code> permitted). 92: * 93: * @return The hash code. 94: */ 95: public static int hashCodeForDoubleArray(double[] a) { 96: if (a == null) { 97: return 0; 98: } 99: int result = 193; 100: long temp; 101: for (int i = 0; i < a.length; i++) { 102: temp = Double.doubleToLongBits(a[i]); 103: result = 29 * result + (int) (temp ^ (temp >>> 32)); 104: } 105: return result; 106: } 107: 108: /** 109: * Returns a hash value based on a seed value and the value of a boolean 110: * primitive. 111: * 112: * @param pre the seed value. 113: * @param b the boolean value. 114: * 115: * @return A hash value. 116: * 117: * @since 1.0.7 118: */ 119: public static int hashCode(int pre, boolean b) { 120: return 37 * pre + (b ? 0 : 1); 121: } 122: 123: /** 124: * Returns a hash value based on a seed value and the value of a double 125: * primitive. 126: * 127: * @param pre the seed value. 128: * @param d the double value. 129: * 130: * @return A hash value. 131: * 132: * @since 1.0.7 133: */ 134: public static int hashCode(int pre, double d) { 135: long l = Double.doubleToLongBits(d); 136: return 37 * pre + (int) (l ^ (l >>> 32)); 137: } 138: 139: /** 140: * Returns a hash value based on a seed value and a paint instance. 141: * 142: * @param pre the seed value. 143: * @param p the paint (<code>null</code> permitted). 144: * 145: * @return A hash value. 146: * 147: * @since 1.0.7 148: */ 149: public static int hashCode(int pre, Paint p) { 150: return 37 * pre + hashCodeForPaint(p); 151: } 152: 153: /** 154: * Returns a hash value based on a seed value and a stroke instance. 155: * 156: * @param pre the seed value. 157: * @param s the stroke (<code>null</code> permitted). 158: * 159: * @return A hash value. 160: * 161: * @since 1.0.7 162: */ 163: public static int hashCode(int pre, Stroke s) { 164: int h = (s != null ? s.hashCode() : 0); 165: return 37 * pre + h; 166: } 167: 168: /** 169: * Returns a hash value based on a seed value and a string instance. 170: * 171: * @param pre the seed value. 172: * @param s the string (<code>null</code> permitted). 173: * 174: * @return A hash value. 175: * 176: * @since 1.0.7 177: */ 178: public static int hashCode(int pre, String s) { 179: int h = (s != null ? s.hashCode() : 0); 180: return 37 * pre + h; 181: } 182: 183: /** 184: * Returns a hash value based on a seed value and a <code>Comparable</code> 185: * instance. 186: * 187: * @param pre the seed value. 188: * @param c the comparable (<code>null</code> permitted). 189: * 190: * @return A hash value. 191: * 192: * @since 1.0.7 193: */ 194: public static int hashCode(int pre, Comparable c) { 195: int h = (c != null ? c.hashCode() : 0); 196: return 37 * pre + h; 197: } 198: }