1 /* 2 Copyright 2008 Ramon Servadei 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package fulmine.util.reference; 17 18 import java.util.Arrays; 19 20 /** 21 * A utility class for performing member comparisons within implementations of 22 * the {@link Object#equals(Object)} method. Instead of the usual 23 * 24 * <pre> 25 * if (this.name == null) 26 * { 27 * if (other.name != null) 28 * { 29 * return false; 30 * } 31 * } 32 * else 33 * { 34 * if (!name.equals(other.name)) 35 * { 36 * return false; 37 * } 38 * } 39 * if (this.code != other.code) 40 * return false; 41 * </pre> 42 * 43 * this class allows the following 44 * 45 * <pre> 46 * return is.eq(this.name, other.name) && is.eq(this.code, other.code); 47 * </pre> 48 * 49 * @author Ramon Servadei 50 */ 51 public abstract class is 52 { 53 /** 54 * Test if the two objects are of the same {@link Class}. <b>This assumes 55 * the first argument is not null.</b> Also checks if the second argument is 56 * <code>null</code>. 57 * 58 * @param candidate 59 * the first object 60 * @param other 61 * the other object 62 * @return <code>false</code> if both objects are of the same {@link Class}, 63 * <code>true</code> if they are not OR the second argument is 64 * <code>null</code> (in which case the classes are not equal 65 * anyway). 66 */ 67 public static boolean differentClass(Object candidate, Object other) 68 { 69 return other == null || candidate.getClass() != other.getClass(); 70 } 71 72 /** 73 * Performs a reference check of two objects. 74 * 75 * @param o1 76 * the first object reference 77 * @param o2 78 * the other object reference 79 * @return <code>true</code> if the objects references are for the same 80 * object 81 */ 82 public static boolean same(Object o1, Object o2) 83 { 84 return o1 == o2; 85 } 86 87 /** 88 * Performs an equality check of two objects. Also checks neither is 89 * <code>null</code>. 90 * 91 * @param o1 92 * the first object 93 * @param o2 94 * the other object 95 * @return <code>true</code> if the objects are equal OR both are 96 * <code>null</code> 97 */ 98 public static boolean eq(Object o1, Object o2) 99 { 100 return (o1 == null && o2 == null) || (o1 != null && o1.equals(o2)); 101 } 102 103 /** 104 * Performs an equality check for two <code>boolean</code> arguments 105 * 106 * @param b1 107 * the first argument 108 * @param b2 109 * the other argument 110 * @return <code>true</code> if the arguments are equal 111 */ 112 public static boolean eq(boolean b1, boolean b2) 113 { 114 return b1 == b2; 115 } 116 117 /** 118 * Performs an equality check for two <code>byte</code> arguments 119 * 120 * @param b1 121 * the first argument 122 * @param b2 123 * the other argument 124 * @return <code>true</code> if the arguments are equal 125 */ 126 public static boolean eq(byte b1, byte b2) 127 { 128 return b1 == b2; 129 } 130 131 /** 132 * Performs an equality check for two <code>char</code> arguments 133 * 134 * @param c1 135 * the first argument 136 * @param c2 137 * the other argument 138 * @return <code>true</code> if the arguments are equal 139 */ 140 public static boolean eq(char c1, char c2) 141 { 142 return c1 == c2; 143 } 144 145 /** 146 * Performs an equality check for two <code>short</code> arguments 147 * 148 * @param s1 149 * the first argument 150 * @param s2 151 * the other argument 152 * @return <code>true</code> if the arguments are equal 153 */ 154 public static boolean eq(short s1, short s2) 155 { 156 return s1 == s2; 157 } 158 159 /** 160 * Performs an equality check for two <code>integer</code> arguments 161 * 162 * @param i1 163 * the first argument 164 * @param i2 165 * the other argument 166 * @return <code>true</code> if the arguments are equal 167 */ 168 public static boolean eq(int i1, int i2) 169 { 170 return i1 == i2; 171 } 172 173 /** 174 * Performs an equality check for two <code>long</code> arguments 175 * 176 * @param l1 177 * the first argument 178 * @param l2 179 * the other argument 180 * @return <code>true</code> if the arguments are equal 181 */ 182 public static boolean eq(long l1, long l2) 183 { 184 return l1 == l2; 185 } 186 187 /** 188 * Performs an equality check for two <code>float</code> arguments 189 * 190 * @see Float#floatToIntBits(float) 191 * @param f1 192 * the first argument 193 * @param f2 194 * the other argument 195 * @return <code>true</code> if the arguments are equal 196 */ 197 public static boolean eq(float f1, float f2) 198 { 199 return Float.floatToIntBits(f1) == Float.floatToIntBits(f2); 200 } 201 202 /** 203 * Performs an equality check for two <code>double</code> arguments. 204 * 205 * @see Double#doubleToLongBits(double) 206 * @param d1 207 * the first argument 208 * @param d2 209 * the other argument 210 * @return <code>true</code> if the arguments are equal 211 */ 212 public static boolean eq(double d1, double d2) 213 { 214 return Double.doubleToLongBits(d1) == Double.doubleToLongBits(d2); 215 } 216 217 /** 218 * Performs an equality check for two <code>boolean[]</code> arguments 219 * 220 * @see Arrays#equals(boolean[], boolean[]) 221 * @param a1 222 * the first argument 223 * @param a2 224 * the other argument 225 * @return <code>true</code> if the arguments are equal 226 */ 227 public static boolean eq(boolean[] a1, boolean[] a2) 228 { 229 return Arrays.equals(a1, a2); 230 } 231 232 /** 233 * Performs an equality check for two <code>byte[]</code> arguments 234 * 235 * @see Arrays#equals(byte[], byte[]) 236 * @param a1 237 * the first argument 238 * @param a2 239 * the other argument 240 * @return <code>true</code> if the arguments are equal 241 */ 242 public static boolean eq(byte[] a1, byte[] a2) 243 { 244 return Arrays.equals(a1, a2); 245 } 246 247 /** 248 * Performs an equality check for two <code>char[]</code> arguments 249 * 250 * @see Arrays#equals(char[], char[]) 251 * @param a1 252 * the first argument 253 * @param a2 254 * the other argument 255 * @return <code>true</code> if the arguments are equal 256 */ 257 public static boolean eq(char[] a1, char[] a2) 258 { 259 return Arrays.equals(a1, a2); 260 } 261 262 /** 263 * Performs an equality check for two <code>short[]</code> arguments 264 * 265 * @see Arrays#equals(short[], short[]) 266 * @param a1 267 * the first argument 268 * @param a2 269 * the other argument 270 * @return <code>true</code> if the arguments are equal 271 */ 272 public static boolean eq(short[] a1, short[] a2) 273 { 274 return Arrays.equals(a1, a2); 275 } 276 277 /** 278 * Performs an equality check for two <code>int[]</code> arguments 279 * 280 * @see Arrays#equals(int[], int[]) 281 * @param a1 282 * the first argument 283 * @param a2 284 * the other argument 285 * @return <code>true</code> if the arguments are equal 286 */ 287 public static boolean eq(int[] a1, int[] a2) 288 { 289 return Arrays.equals(a1, a2); 290 } 291 292 /** 293 * Performs an equality check for two <code>long[]</code> arguments 294 * 295 * @see Arrays#equals(long[], long[]) 296 * @param a1 297 * the first argument 298 * @param a2 299 * the other argument 300 * @return <code>true</code> if the arguments are equal 301 */ 302 public static boolean eq(long[] a1, long[] a2) 303 { 304 return Arrays.equals(a1, a2); 305 } 306 307 /** 308 * Performs an equality check for two <code>float[]</code> arguments 309 * 310 * @see Arrays#equals(float[], float[]) 311 * @param a1 312 * the first argument 313 * @param a2 314 * the other argument 315 * @return <code>true</code> if the arguments are equal 316 */ 317 public static boolean eq(float[] a1, float[] a2) 318 { 319 return Arrays.equals(a1, a2); 320 } 321 322 /** 323 * Performs an equality check for two <code>double[]</code> arguments 324 * 325 * @see Arrays#equals(double[], double[]) 326 * @param a1 327 * the first argument 328 * @param a2 329 * the other argument 330 * @return <code>true</code> if the arguments are equal 331 */ 332 public static boolean eq(double[] a1, double[] a2) 333 { 334 return Arrays.equals(a1, a2); 335 } 336 337 /** 338 * Performs an equality check for two <code>Object[]</code> arguments 339 * 340 * @see Arrays#equals(Object[], Object[]) 341 * @param a1 342 * the first argument 343 * @param a2 344 * the other argument 345 * @return <code>true</code> if the arguments are equal 346 */ 347 public static boolean eq(Object[] a1, Object[] a2) 348 { 349 return Arrays.equals(a1, a2); 350 } 351 352 /** 353 * Performs a deep equality check for two <code>Object[]</code> arguments 354 * 355 * @see Arrays#deepEquals(Object[], Object[]) 356 * @param a1 357 * the first argument 358 * @param a2 359 * the other argument 360 * @return <code>true</code> if the arguments are equal 361 */ 362 public static boolean deepEq(Object[] a1, Object[] a2) 363 { 364 return Arrays.deepEquals(a1, a2); 365 } 366 367 /** 368 * Checks that the argument is not <code>null</code> 369 * 370 * @param o 371 * the argument 372 * @return <code>true</code> if the argument is not <code>null</code> 373 */ 374 public static boolean notNull(Object o) 375 { 376 return o != null; 377 } 378 }