001/* ====================================================== 002 * JFreeChart : a chart library for the Java(tm) platform 003 * ====================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: https://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 * HashUtils.java 029 * -------------- 030 * (C) Copyright 2006-present, by David Gilbert; 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart; 038 039import java.awt.GradientPaint; 040import java.awt.Paint; 041import java.awt.Stroke; 042import org.jfree.chart.util.BooleanList; 043import org.jfree.chart.util.PaintList; 044import org.jfree.chart.util.StrokeList; 045 046/** 047 * Some utility methods for calculating hash codes. 048 */ 049public class HashUtils { 050 051 private HashUtils() { 052 // no requirement to instantiate 053 } 054 055 /** 056 * Returns a hash code for a {@code Paint} instance. If 057 * {@code p} is {@code null}, this method returns zero. 058 * 059 * @param p the paint ({@code null} permitted). 060 * 061 * @return The hash code. 062 */ 063 public static int hashCodeForPaint(Paint p) { 064 if (p == null) { 065 return 0; 066 } 067 int result; 068 // handle GradientPaint as a special case 069 if (p instanceof GradientPaint) { 070 GradientPaint gp = (GradientPaint) p; 071 result = 193; 072 result = 37 * result + gp.getColor1().hashCode(); 073 result = 37 * result + gp.getPoint1().hashCode(); 074 result = 37 * result + gp.getColor2().hashCode(); 075 result = 37 * result + gp.getPoint2().hashCode(); 076 } 077 else { 078 // we assume that all other Paint instances implement equals() and 079 // hashCode()...of course that might not be true, but what can we 080 // do about it? 081 result = p.hashCode(); 082 } 083 return result; 084 } 085 086 /** 087 * Returns a hash code for a {@code double[]} instance. If the array 088 * is {@code null}, this method returns zero. 089 * 090 * @param a the array ({@code null} permitted). 091 * 092 * @return The hash code. 093 */ 094 public static int hashCodeForDoubleArray(double[] a) { 095 if (a == null) { 096 return 0; 097 } 098 int result = 193; 099 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 /** 108 * Returns a hash value based on a seed value and the value of a boolean 109 * primitive. 110 * 111 * @param pre the seed value. 112 * @param b the boolean value. 113 * 114 * @return A hash value. 115 */ 116 public static int hashCode(int pre, boolean b) { 117 return 37 * pre + (b ? 0 : 1); 118 } 119 120 /** 121 * Returns a hash value based on a seed value and the value of an int 122 * primitive. 123 * 124 * @param pre the seed value. 125 * @param i the int value. 126 * 127 * @return A hash value. 128 */ 129 public static int hashCode(int pre, int i) { 130 return 37 * pre + i; 131 } 132 133 /** 134 * Returns a hash value based on a seed value and the value of a double 135 * primitive. 136 * 137 * @param pre the seed value. 138 * @param d the double value. 139 * 140 * @return A hash value. 141 */ 142 public static int hashCode(int pre, double d) { 143 long l = Double.doubleToLongBits(d); 144 return 37 * pre + (int) (l ^ (l >>> 32)); 145 } 146 147 /** 148 * Returns a hash value based on a seed value and a paint instance. 149 * 150 * @param pre the seed value. 151 * @param p the paint ({@code null} permitted). 152 * 153 * @return A hash value. 154 */ 155 public static int hashCode(int pre, Paint p) { 156 return 37 * pre + hashCodeForPaint(p); 157 } 158 159 /** 160 * Returns a hash value based on a seed value and a stroke instance. 161 * 162 * @param pre the seed value. 163 * @param s the stroke ({@code null} permitted). 164 * 165 * @return A hash value. 166 */ 167 public static int hashCode(int pre, Stroke s) { 168 int h = (s != null ? s.hashCode() : 0); 169 return 37 * pre + h; 170 } 171 172 /** 173 * Returns a hash value based on a seed value and a string instance. 174 * 175 * @param pre the seed value. 176 * @param s the string ({@code null} permitted). 177 * 178 * @return A hash value. 179 */ 180 public static int hashCode(int pre, String s) { 181 int h = (s != null ? s.hashCode() : 0); 182 return 37 * pre + h; 183 } 184 185 /** 186 * Returns a hash value based on a seed value and a {@code Comparable} 187 * instance. 188 * 189 * @param pre the seed value. 190 * @param c the comparable ({@code null} permitted). 191 * 192 * @return A hash value. 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 199 /** 200 * Returns a hash value based on a seed value and an {@code Object} 201 * instance. 202 * 203 * @param pre the seed value. 204 * @param obj the object ({@code null} permitted). 205 * 206 * @return A hash value. 207 */ 208 public static int hashCode(int pre, Object obj) { 209 int h = (obj != null ? obj.hashCode() : 0); 210 return 37 * pre + h; 211 } 212 213 /** 214 * Computes a hash code for a {@link BooleanList}. In the latest version 215 * of JCommon, the {@link BooleanList} class should implement the hashCode() 216 * method correctly, but we compute it here anyway so that we can work with 217 * older versions of JCommon (back to 1.0.0). 218 * 219 * @param pre the seed value. 220 * @param list the list ({@code null} permitted). 221 * 222 * @return The hash code. 223 */ 224 public static int hashCode(int pre, BooleanList list) { 225 if (list == null) { 226 return pre; 227 } 228 int result = 127; 229 int size = list.size(); 230 result = HashUtils.hashCode(result, size); 231 232 // for efficiency, we just use the first, last and middle items to 233 // compute a hashCode... 234 if (size > 0) { 235 result = HashUtils.hashCode(result, list.getBoolean(0)); 236 if (size > 1) { 237 result = HashUtils.hashCode(result, 238 list.getBoolean(size - 1)); 239 if (size > 2) { 240 result = HashUtils.hashCode(result, 241 list.getBoolean(size / 2)); 242 } 243 } 244 } 245 return 37 * pre + result; 246 } 247 248 /** 249 * Computes a hash code for a {@link PaintList}. In the latest version 250 * of JCommon, the {@link PaintList} class should implement the hashCode() 251 * method correctly, but we compute it here anyway so that we can work with 252 * older versions of JCommon (back to 1.0.0). 253 * 254 * @param pre the seed value. 255 * @param list the list ({@code null} permitted). 256 * 257 * @return The hash code. 258 */ 259 public static int hashCode(int pre, PaintList list) { 260 if (list == null) { 261 return pre; 262 } 263 int result = 127; 264 int size = list.size(); 265 result = HashUtils.hashCode(result, size); 266 267 // for efficiency, we just use the first, last and middle items to 268 // compute a hashCode... 269 if (size > 0) { 270 result = HashUtils.hashCode(result, list.getPaint(0)); 271 if (size > 1) { 272 result = HashUtils.hashCode(result, 273 list.getPaint(size - 1)); 274 if (size > 2) { 275 result = HashUtils.hashCode(result, 276 list.getPaint(size / 2)); 277 } 278 } 279 } 280 return 37 * pre + result; 281 } 282 283 /** 284 * Computes a hash code for a {@link StrokeList}. In the latest version 285 * of JCommon, the {@link StrokeList} class should implement the hashCode() 286 * method correctly, but we compute it here anyway so that we can work with 287 * older versions of JCommon (back to 1.0.0). 288 * 289 * @param pre the seed value. 290 * @param list the list ({@code null} permitted). 291 * 292 * @return The hash code. 293 */ 294 public static int hashCode(int pre, StrokeList list) { 295 if (list == null) { 296 return pre; 297 } 298 int result = 127; 299 int size = list.size(); 300 result = HashUtils.hashCode(result, size); 301 302 // for efficiency, we just use the first, last and middle items to 303 // compute a hashCode... 304 if (size > 0) { 305 result = HashUtils.hashCode(result, list.getStroke(0)); 306 if (size > 1) { 307 result = HashUtils.hashCode(result, 308 list.getStroke(size - 1)); 309 if (size > 2) { 310 result = HashUtils.hashCode(result, 311 list.getStroke(size / 2)); 312 } 313 } 314 } 315 return 37 * pre + result; 316 } 317}