001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------ 028 * HashUtilities.java 029 * ------------------ 030 * (C) Copyright 2006-2016, by Object Refinery Limited; 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 03-Oct-2006 : Version 1 (DG); 038 * 06-Mar-2007 : Fix for hashCodeForDoubleArray() method (DG); 039 * 13-Nov-2007 : Added new utility methods (DG); 040 * 22-Nov-2007 : Added hashCode() method for 'int' (DG); 041 * 05-Dec-2007 : Added special methods to handle BooleanList, PaintList, 042 * and StrokeList (DG); 043 * 044 */ 045 046package org.jfree.chart; 047 048import java.awt.GradientPaint; 049import java.awt.Paint; 050import java.awt.Stroke; 051import org.jfree.chart.util.BooleanList; 052import org.jfree.chart.util.PaintList; 053import org.jfree.chart.util.StrokeList; 054 055/** 056 * Some utility methods for calculating hash codes. 057 * 058 * @since 1.0.3 059 */ 060public class HashUtils { 061 062 /** 063 * Returns a hash code for a {@code Paint} instance. If 064 * {@code p} is {@code null}, this method returns zero. 065 * 066 * @param p the paint ({@code null} permitted). 067 * 068 * @return The hash code. 069 */ 070 public static int hashCodeForPaint(Paint p) { 071 if (p == null) { 072 return 0; 073 } 074 int result; 075 // handle GradientPaint as a special case 076 if (p instanceof GradientPaint) { 077 GradientPaint gp = (GradientPaint) p; 078 result = 193; 079 result = 37 * result + gp.getColor1().hashCode(); 080 result = 37 * result + gp.getPoint1().hashCode(); 081 result = 37 * result + gp.getColor2().hashCode(); 082 result = 37 * result + gp.getPoint2().hashCode(); 083 } 084 else { 085 // we assume that all other Paint instances implement equals() and 086 // hashCode()...of course that might not be true, but what can we 087 // do about it? 088 result = p.hashCode(); 089 } 090 return result; 091 } 092 093 /** 094 * Returns a hash code for a {@code double[]} instance. If the array 095 * is {@code null}, this method returns zero. 096 * 097 * @param a the array ({@code null} permitted). 098 * 099 * @return The hash code. 100 */ 101 public static int hashCodeForDoubleArray(double[] a) { 102 if (a == null) { 103 return 0; 104 } 105 int result = 193; 106 long temp; 107 for (int i = 0; i < a.length; i++) { 108 temp = Double.doubleToLongBits(a[i]); 109 result = 29 * result + (int) (temp ^ (temp >>> 32)); 110 } 111 return result; 112 } 113 114 /** 115 * Returns a hash value based on a seed value and the value of a boolean 116 * primitive. 117 * 118 * @param pre the seed value. 119 * @param b the boolean value. 120 * 121 * @return A hash value. 122 * 123 * @since 1.0.7 124 */ 125 public static int hashCode(int pre, boolean b) { 126 return 37 * pre + (b ? 0 : 1); 127 } 128 129 /** 130 * Returns a hash value based on a seed value and the value of an int 131 * primitive. 132 * 133 * @param pre the seed value. 134 * @param i the int value. 135 * 136 * @return A hash value. 137 * 138 * @since 1.0.8 139 */ 140 public static int hashCode(int pre, int i) { 141 return 37 * pre + i; 142 } 143 144 /** 145 * Returns a hash value based on a seed value and the value of a double 146 * primitive. 147 * 148 * @param pre the seed value. 149 * @param d the double value. 150 * 151 * @return A hash value. 152 * 153 * @since 1.0.7 154 */ 155 public static int hashCode(int pre, double d) { 156 long l = Double.doubleToLongBits(d); 157 return 37 * pre + (int) (l ^ (l >>> 32)); 158 } 159 160 /** 161 * Returns a hash value based on a seed value and a paint instance. 162 * 163 * @param pre the seed value. 164 * @param p the paint ({@code null} permitted). 165 * 166 * @return A hash value. 167 * 168 * @since 1.0.7 169 */ 170 public static int hashCode(int pre, Paint p) { 171 return 37 * pre + hashCodeForPaint(p); 172 } 173 174 /** 175 * Returns a hash value based on a seed value and a stroke instance. 176 * 177 * @param pre the seed value. 178 * @param s the stroke ({@code null} permitted). 179 * 180 * @return A hash value. 181 * 182 * @since 1.0.7 183 */ 184 public static int hashCode(int pre, Stroke s) { 185 int h = (s != null ? s.hashCode() : 0); 186 return 37 * pre + h; 187 } 188 189 /** 190 * Returns a hash value based on a seed value and a string instance. 191 * 192 * @param pre the seed value. 193 * @param s the string ({@code null} permitted). 194 * 195 * @return A hash value. 196 * 197 * @since 1.0.7 198 */ 199 public static int hashCode(int pre, String s) { 200 int h = (s != null ? s.hashCode() : 0); 201 return 37 * pre + h; 202 } 203 204 /** 205 * Returns a hash value based on a seed value and a {@code Comparable} 206 * instance. 207 * 208 * @param pre the seed value. 209 * @param c the comparable ({@code null} permitted). 210 * 211 * @return A hash value. 212 * 213 * @since 1.0.7 214 */ 215 public static int hashCode(int pre, Comparable c) { 216 int h = (c != null ? c.hashCode() : 0); 217 return 37 * pre + h; 218 } 219 220 /** 221 * Returns a hash value based on a seed value and an {@code Object} 222 * instance. 223 * 224 * @param pre the seed value. 225 * @param obj the object ({@code null} permitted). 226 * 227 * @return A hash value. 228 * 229 * @since 1.0.8 230 */ 231 public static int hashCode(int pre, Object obj) { 232 int h = (obj != null ? obj.hashCode() : 0); 233 return 37 * pre + h; 234 } 235 236 /** 237 * Computes a hash code for a {@link BooleanList}. In the latest version 238 * of JCommon, the {@link BooleanList} class should implement the hashCode() 239 * method correctly, but we compute it here anyway so that we can work with 240 * older versions of JCommon (back to 1.0.0). 241 * 242 * @param pre the seed value. 243 * @param list the list ({@code null} permitted). 244 * 245 * @return The hash code. 246 * 247 * @since 1.0.9 248 */ 249 public static int hashCode(int pre, BooleanList list) { 250 if (list == null) { 251 return pre; 252 } 253 int result = 127; 254 int size = list.size(); 255 result = HashUtils.hashCode(result, size); 256 257 // for efficiency, we just use the first, last and middle items to 258 // compute a hashCode... 259 if (size > 0) { 260 result = HashUtils.hashCode(result, list.getBoolean(0)); 261 if (size > 1) { 262 result = HashUtils.hashCode(result, 263 list.getBoolean(size - 1)); 264 if (size > 2) { 265 result = HashUtils.hashCode(result, 266 list.getBoolean(size / 2)); 267 } 268 } 269 } 270 return 37 * pre + result; 271 } 272 273 /** 274 * Computes a hash code for a {@link PaintList}. In the latest version 275 * of JCommon, the {@link PaintList} class should implement the hashCode() 276 * method correctly, but we compute it here anyway so that we can work with 277 * older versions of JCommon (back to 1.0.0). 278 * 279 * @param pre the seed value. 280 * @param list the list ({@code null} permitted). 281 * 282 * @return The hash code. 283 * 284 * @since 1.0.9 285 */ 286 public static int hashCode(int pre, PaintList list) { 287 if (list == null) { 288 return pre; 289 } 290 int result = 127; 291 int size = list.size(); 292 result = HashUtils.hashCode(result, size); 293 294 // for efficiency, we just use the first, last and middle items to 295 // compute a hashCode... 296 if (size > 0) { 297 result = HashUtils.hashCode(result, list.getPaint(0)); 298 if (size > 1) { 299 result = HashUtils.hashCode(result, 300 list.getPaint(size - 1)); 301 if (size > 2) { 302 result = HashUtils.hashCode(result, 303 list.getPaint(size / 2)); 304 } 305 } 306 } 307 return 37 * pre + result; 308 } 309 310 /** 311 * Computes a hash code for a {@link StrokeList}. In the latest version 312 * of JCommon, the {@link StrokeList} class should implement the hashCode() 313 * method correctly, but we compute it here anyway so that we can work with 314 * older versions of JCommon (back to 1.0.0). 315 * 316 * @param pre the seed value. 317 * @param list the list ({@code null} permitted). 318 * 319 * @return The hash code. 320 * 321 * @since 1.0.9 322 */ 323 public static int hashCode(int pre, StrokeList list) { 324 if (list == null) { 325 return pre; 326 } 327 int result = 127; 328 int size = list.size(); 329 result = HashUtils.hashCode(result, size); 330 331 // for efficiency, we just use the first, last and middle items to 332 // compute a hashCode... 333 if (size > 0) { 334 result = HashUtils.hashCode(result, list.getStroke(0)); 335 if (size > 1) { 336 result = HashUtils.hashCode(result, 337 list.getStroke(size - 1)); 338 if (size > 2) { 339 result = HashUtils.hashCode(result, 340 list.getStroke(size / 2)); 341 } 342 } 343 } 344 return 37 * pre + result; 345 } 346}