1 /* 2 * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import jdk.internal.util.Preconditions; 29 import jdk.internal.vm.annotation.ForceInline; 30 31 import java.util.function.Supplier; 32 33 /** 34 * This class consists of {@code static} utility methods for operating 35 * on objects, or checking certain conditions before operation. These utilities 36 * include {@code null}-safe or {@code null}-tolerant methods for computing the 37 * hash code of an object, returning a string for an object, comparing two 38 * objects, and checking if indexes or sub-range values are out of bounds. 39 * 40 * @since 1.7 41 */ 42 public final class Objects { 43 private Objects() { 44 throw new AssertionError("No java.util.Objects instances for you!"); 45 } 46 47 /** 48 * Returns {@code true} if the arguments are equal to each other 49 * and {@code false} otherwise. 50 * Consequently, if both arguments are {@code null}, {@code true} 51 * is returned. Otherwise, if the first argument is not {@code 52 * null}, equality is determined by calling the {@link 53 * Object#equals equals} method of the first argument with the 54 * second argument of this method. Otherwise, {@code false} is 55 * returned. 56 * 57 * @param a an object 58 * @param b an object to be compared with {@code a} for equality 59 * @return {@code true} if the arguments are equal to each other 60 * and {@code false} otherwise 61 * @see Object#equals(Object) 62 */ 63 public static boolean equals(Object a, Object b) { 64 return (a == b) || (a != null && a.equals(b)); 65 } 66 67 /** 68 * Returns {@code true} if the arguments are deeply equal to each other 69 * and {@code false} otherwise. 70 * 71 * Two {@code null} values are deeply equal. If both arguments are 72 * arrays, the algorithm in {@link Arrays#deepEquals(Object[], 73 * Object[]) Arrays.deepEquals} is used to determine equality. 74 * Otherwise, equality is determined by using the {@link 75 * Object#equals equals} method of the first argument. 76 * 77 * @param a an object 78 * @param b an object to be compared with {@code a} for deep equality 79 * @return {@code true} if the arguments are deeply equal to each other 80 * and {@code false} otherwise 81 * @see Arrays#deepEquals(Object[], Object[]) 82 * @see Objects#equals(Object, Object) 83 */ 84 public static boolean deepEquals(Object a, Object b) { 85 if (a == b) 86 return true; 87 else if (a == null || b == null) 88 return false; 89 else 90 return Arrays.deepEquals0(a, b); 91 } 92 93 /** 94 * Returns the hash code of a non-{@code null} argument and 0 for 95 * a {@code null} argument. 96 * 97 * @param o an object 98 * @return the hash code of a non-{@code null} argument and 0 for 99 * a {@code null} argument 100 * @see Object#hashCode 101 */ 102 public static int hashCode(Object o) { 103 return o != null ? o.hashCode() : 0; 104 } 105 106 /** 107 * Generates a hash code for a sequence of input values. The hash 108 * code is generated as if all the input values were placed into an 109 * array, and that array were hashed by calling {@link 110 * Arrays#hashCode(Object[])}. 111 * 112 * <p>This method is useful for implementing {@link 113 * Object#hashCode()} on objects containing multiple fields. For 114 * example, if an object that has three fields, {@code x}, {@code 115 * y}, and {@code z}, one could write: 116 * 117 * <blockquote><pre> 118 * @Override public int hashCode() { 119 * return Objects.hash(x, y, z); 120 * } 121 * </pre></blockquote> 122 * 123 * <b>Warning: When a single object reference is supplied, the returned 124 * value does not equal the hash code of that object reference.</b> This 125 * value can be computed by calling {@link #hashCode(Object)}. 126 * 127 * @param values the values to be hashed 128 * @return a hash value of the sequence of input values 129 * @see Arrays#hashCode(Object[]) 130 * @see List#hashCode 131 */ 132 public static int hash(Object... values) { 133 return Arrays.hashCode(values); 134 } 135 136 /** 137 * Returns the result of calling {@code toString} for a non-{@code 138 * null} argument and {@code "null"} for a {@code null} argument. 139 * 140 * @param o an object 141 * @return the result of calling {@code toString} for a non-{@code 142 * null} argument and {@code "null"} for a {@code null} argument 143 * @see Object#toString 144 * @see String#valueOf(Object) 145 */ 146 public static String toString(Object o) { 147 return String.valueOf(o); 148 } 149 150 /** 151 * Returns the result of calling {@code toString} on the first 152 * argument if the first argument is not {@code null} and returns 153 * the second argument otherwise. 154 * 155 * @param o an object 156 * @param nullDefault string to return if the first argument is 157 * {@code null} 158 * @return the result of calling {@code toString} on the first 159 * argument if it is not {@code null} and the second argument 160 * otherwise. 161 * @see Objects#toString(Object) 162 */ 163 public static String toString(Object o, String nullDefault) { 164 return (o != null) ? o.toString() : nullDefault; 165 } 166 167 /** 168 * {@return a string equivalent to the string returned by {@code 169 * Object.toString} if that method and {@code hashCode} are not 170 * overridden} 171 * 172 * @implNote 173 * This method constructs a string for an object without calling 174 * any overridable methods of the object. 175 * 176 * @implSpec 177 * The method returns a string equivalent to:<br> 178 * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))} 179 * 180 * @param o an object 181 * @throws NullPointerException if the argument is null 182 * @see Object#toString 183 * @see System#identityHashCode(Object) 184 * @since 19 185 */ 186 public static String toIdentityString(Object o) { 187 requireNonNull(o); 188 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o)); 189 } 190 191 /** 192 * Returns 0 if the arguments are identical and {@code 193 * c.compare(a, b)} otherwise. 194 * Consequently, if both arguments are {@code null} 0 195 * is returned. 196 * 197 * <p>Note that if one of the arguments is {@code null}, a {@code 198 * NullPointerException} may or may not be thrown depending on 199 * what ordering policy, if any, the {@link Comparator Comparator} 200 * chooses to have for {@code null} values. 201 * 202 * @param <T> the type of the objects being compared 203 * @param a an object 204 * @param b an object to be compared with {@code a} 205 * @param c the {@code Comparator} to compare the first two arguments 206 * @return 0 if the arguments are identical and {@code 207 * c.compare(a, b)} otherwise. 208 * @see Comparable 209 * @see Comparator 210 */ 211 public static <T> int compare(T a, T b, Comparator<? super T> c) { 212 return (a == b) ? 0 : c.compare(a, b); 213 } 214 215 /** 216 * Checks that the specified object reference is not {@code null}. This 217 * method is designed primarily for doing parameter validation in methods 218 * and constructors, as demonstrated below: 219 * <blockquote><pre> 220 * public Foo(Bar bar) { 221 * this.bar = Objects.requireNonNull(bar); 222 * } 223 * </pre></blockquote> 224 * 225 * @param obj the object reference to check for nullity 226 * @param <T> the type of the reference 227 * @return {@code obj} if not {@code null} 228 * @throws NullPointerException if {@code obj} is {@code null} 229 */ 230 @ForceInline 231 public static <T> T requireNonNull(T obj) { 232 if (obj == null) 233 throw new NullPointerException(); 234 return obj; 235 } 236 237 /** 238 * Checks that the specified object reference is not {@code null} and 239 * throws a customized {@link NullPointerException} if it is. This method 240 * is designed primarily for doing parameter validation in methods and 241 * constructors with multiple parameters, as demonstrated below: 242 * <blockquote><pre> 243 * public Foo(Bar bar, Baz baz) { 244 * this.bar = Objects.requireNonNull(bar, "bar must not be null"); 245 * this.baz = Objects.requireNonNull(baz, "baz must not be null"); 246 * } 247 * </pre></blockquote> 248 * 249 * @param obj the object reference to check for nullity 250 * @param message detail message to be used in the event that a {@code 251 * NullPointerException} is thrown 252 * @param <T> the type of the reference 253 * @return {@code obj} if not {@code null} 254 * @throws NullPointerException if {@code obj} is {@code null} 255 */ 256 @ForceInline 257 public static <T> T requireNonNull(T obj, String message) { 258 if (obj == null) 259 throw new NullPointerException(message); 260 return obj; 261 } 262 263 /** 264 * Returns {@code true} if the provided reference is {@code null} otherwise 265 * returns {@code false}. 266 * 267 * @apiNote This method exists to be used as a 268 * {@link java.util.function.Predicate}, {@code filter(Objects::isNull)} 269 * 270 * @param obj a reference to be checked against {@code null} 271 * @return {@code true} if the provided reference is {@code null} otherwise 272 * {@code false} 273 * 274 * @see java.util.function.Predicate 275 * @since 1.8 276 */ 277 public static boolean isNull(Object obj) { 278 return obj == null; 279 } 280 281 /** 282 * Returns {@code true} if the provided reference is non-{@code null} 283 * otherwise returns {@code false}. 284 * 285 * @apiNote This method exists to be used as a 286 * {@link java.util.function.Predicate}, {@code filter(Objects::nonNull)} 287 * 288 * @param obj a reference to be checked against {@code null} 289 * @return {@code true} if the provided reference is non-{@code null} 290 * otherwise {@code false} 291 * 292 * @see java.util.function.Predicate 293 * @since 1.8 294 */ 295 public static boolean nonNull(Object obj) { 296 return obj != null; 297 } 298 299 /** 300 * Returns the first argument if it is non-{@code null} and 301 * otherwise returns the non-{@code null} second argument. 302 * 303 * @param obj an object 304 * @param defaultObj a non-{@code null} object to return if the first argument 305 * is {@code null} 306 * @param <T> the type of the reference 307 * @return the first argument if it is non-{@code null} and 308 * otherwise the second argument if it is non-{@code null} 309 * @throws NullPointerException if both {@code obj} is null and 310 * {@code defaultObj} is {@code null} 311 * @since 9 312 */ 313 public static <T> T requireNonNullElse(T obj, T defaultObj) { 314 return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj"); 315 } 316 317 /** 318 * Returns the first argument if it is non-{@code null} and otherwise 319 * returns the non-{@code null} value of {@code supplier.get()}. 320 * 321 * @param obj an object 322 * @param supplier of a non-{@code null} object to return if the first argument 323 * is {@code null} 324 * @param <T> the type of the first argument and return type 325 * @return the first argument if it is non-{@code null} and otherwise 326 * the value from {@code supplier.get()} if it is non-{@code null} 327 * @throws NullPointerException if both {@code obj} is null and 328 * either the {@code supplier} is {@code null} or 329 * the {@code supplier.get()} value is {@code null} 330 * @since 9 331 */ 332 public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) { 333 return (obj != null) ? obj 334 : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()"); 335 } 336 337 /** 338 * Checks that the specified object reference is not {@code null} and 339 * throws a customized {@link NullPointerException} if it is. 340 * 341 * <p>Unlike the method {@link #requireNonNull(Object, String)}, 342 * this method allows creation of the message to be deferred until 343 * after the null check is made. While this may confer a 344 * performance advantage in the non-null case, when deciding to 345 * call this method care should be taken that the costs of 346 * creating the message supplier are less than the cost of just 347 * creating the string message directly. 348 * 349 * @param obj the object reference to check for nullity 350 * @param messageSupplier supplier of the detail message to be 351 * used in the event that a {@code NullPointerException} is thrown 352 * @param <T> the type of the reference 353 * @return {@code obj} if not {@code null} 354 * @throws NullPointerException if {@code obj} is {@code null} 355 * @since 1.8 356 */ 357 public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) { 358 if (obj == null) 359 throw new NullPointerException(messageSupplier == null ? 360 null : messageSupplier.get()); 361 return obj; 362 } 363 364 /** 365 * Checks if the {@code index} is within the bounds of the range from 366 * {@code 0} (inclusive) to {@code length} (exclusive). 367 * 368 * <p>The {@code index} is defined to be out of bounds if any of the 369 * following inequalities is true: 370 * <ul> 371 * <li>{@code index < 0}</li> 372 * <li>{@code index >= length}</li> 373 * <li>{@code length < 0}, which is implied from the former inequalities</li> 374 * </ul> 375 * 376 * @param index the index 377 * @param length the upper-bound (exclusive) of the range 378 * @return {@code index} if it is within bounds of the range 379 * @throws IndexOutOfBoundsException if the {@code index} is out of bounds 380 * @since 9 381 */ 382 @ForceInline 383 public static 384 int checkIndex(int index, int length) { 385 return Preconditions.checkIndex(index, length, null); 386 } 387 388 /** 389 * Checks if the sub-range from {@code fromIndex} (inclusive) to 390 * {@code toIndex} (exclusive) is within the bounds of range from {@code 0} 391 * (inclusive) to {@code length} (exclusive). 392 * 393 * <p>The sub-range is defined to be out of bounds if any of the following 394 * inequalities is true: 395 * <ul> 396 * <li>{@code fromIndex < 0}</li> 397 * <li>{@code fromIndex > toIndex}</li> 398 * <li>{@code toIndex > length}</li> 399 * <li>{@code length < 0}, which is implied from the former inequalities</li> 400 * </ul> 401 * 402 * @param fromIndex the lower-bound (inclusive) of the sub-range 403 * @param toIndex the upper-bound (exclusive) of the sub-range 404 * @param length the upper-bound (exclusive) the range 405 * @return {@code fromIndex} if the sub-range within bounds of the range 406 * @throws IndexOutOfBoundsException if the sub-range is out of bounds 407 * @since 9 408 */ 409 public static 410 int checkFromToIndex(int fromIndex, int toIndex, int length) { 411 return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null); 412 } 413 414 /** 415 * Checks if the sub-range from {@code fromIndex} (inclusive) to 416 * {@code fromIndex + size} (exclusive) is within the bounds of range from 417 * {@code 0} (inclusive) to {@code length} (exclusive). 418 * 419 * <p>The sub-range is defined to be out of bounds if any of the following 420 * inequalities is true: 421 * <ul> 422 * <li>{@code fromIndex < 0}</li> 423 * <li>{@code size < 0}</li> 424 * <li>{@code fromIndex + size > length}, taking into account integer overflow</li> 425 * <li>{@code length < 0}, which is implied from the former inequalities</li> 426 * </ul> 427 * 428 * @param fromIndex the lower-bound (inclusive) of the sub-interval 429 * @param size the size of the sub-range 430 * @param length the upper-bound (exclusive) of the range 431 * @return {@code fromIndex} if the sub-range within bounds of the range 432 * @throws IndexOutOfBoundsException if the sub-range is out of bounds 433 * @since 9 434 */ 435 public static 436 int checkFromIndexSize(int fromIndex, int size, int length) { 437 return Preconditions.checkFromIndexSize(fromIndex, size, length, null); 438 } 439 440 /** 441 * Checks if the {@code index} is within the bounds of the range from 442 * {@code 0} (inclusive) to {@code length} (exclusive). 443 * 444 * <p>The {@code index} is defined to be out of bounds if any of the 445 * following inequalities is true: 446 * <ul> 447 * <li>{@code index < 0}</li> 448 * <li>{@code index >= length}</li> 449 * <li>{@code length < 0}, which is implied from the former inequalities</li> 450 * </ul> 451 * 452 * @param index the index 453 * @param length the upper-bound (exclusive) of the range 454 * @return {@code index} if it is within bounds of the range 455 * @throws IndexOutOfBoundsException if the {@code index} is out of bounds 456 * @since 16 457 */ 458 @ForceInline 459 public static 460 long checkIndex(long index, long length) { 461 return Preconditions.checkIndex(index, length, null); 462 } 463 464 /** 465 * Checks if the sub-range from {@code fromIndex} (inclusive) to 466 * {@code toIndex} (exclusive) is within the bounds of range from {@code 0} 467 * (inclusive) to {@code length} (exclusive). 468 * 469 * <p>The sub-range is defined to be out of bounds if any of the following 470 * inequalities is true: 471 * <ul> 472 * <li>{@code fromIndex < 0}</li> 473 * <li>{@code fromIndex > toIndex}</li> 474 * <li>{@code toIndex > length}</li> 475 * <li>{@code length < 0}, which is implied from the former inequalities</li> 476 * </ul> 477 * 478 * @param fromIndex the lower-bound (inclusive) of the sub-range 479 * @param toIndex the upper-bound (exclusive) of the sub-range 480 * @param length the upper-bound (exclusive) the range 481 * @return {@code fromIndex} if the sub-range within bounds of the range 482 * @throws IndexOutOfBoundsException if the sub-range is out of bounds 483 * @since 16 484 */ 485 public static 486 long checkFromToIndex(long fromIndex, long toIndex, long length) { 487 return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null); 488 } 489 490 /** 491 * Checks if the sub-range from {@code fromIndex} (inclusive) to 492 * {@code fromIndex + size} (exclusive) is within the bounds of range from 493 * {@code 0} (inclusive) to {@code length} (exclusive). 494 * 495 * <p>The sub-range is defined to be out of bounds if any of the following 496 * inequalities is true: 497 * <ul> 498 * <li>{@code fromIndex < 0}</li> 499 * <li>{@code size < 0}</li> 500 * <li>{@code fromIndex + size > length}, taking into account integer overflow</li> 501 * <li>{@code length < 0}, which is implied from the former inequalities</li> 502 * </ul> 503 * 504 * @param fromIndex the lower-bound (inclusive) of the sub-interval 505 * @param size the size of the sub-range 506 * @param length the upper-bound (exclusive) of the range 507 * @return {@code fromIndex} if the sub-range within bounds of the range 508 * @throws IndexOutOfBoundsException if the sub-range is out of bounds 509 * @since 16 510 */ 511 public static 512 long checkFromIndexSize(long fromIndex, long size, long length) { 513 return Preconditions.checkFromIndexSize(fromIndex, size, length, null); 514 } 515 } --- EOF ---