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