1 /* 2 * Copyright (C) 2017 The Android Open Source Project 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 androidx.core.util; 17 18 import org.jspecify.annotations.NonNull; 19 import org.jspecify.annotations.Nullable; 20 21 import java.util.Arrays; 22 import java.util.Objects; 23 24 /** 25 * This class consists of static utility methods for operating on objects. 26 */ 27 public class ObjectsCompat { ObjectsCompat()28 private ObjectsCompat() { 29 // Non-instantiable. 30 } 31 32 /** 33 * Returns {@code true} if the arguments are equal to each other 34 * and {@code false} otherwise. 35 * <p> 36 * Consequently, if both arguments are {@code null}, {@code true} 37 * is returned and if exactly one argument is {@code null}, {@code 38 * false} is returned. Otherwise, equality is determined by using 39 * the {@link Object#equals equals} method of the first 40 * argument. 41 * 42 * @param a an object 43 * @param b an object to be compared with {@code a} for equality 44 * @return {@code true} if the arguments are equal to each other 45 * and {@code false} otherwise 46 * @see Object#equals(Object) 47 */ 48 @SuppressWarnings("EqualsReplaceableByObjectsCall") equals(@ullable Object a, @Nullable Object b)49 public static boolean equals(@Nullable Object a, @Nullable Object b) { 50 return Objects.equals(a, b); 51 } 52 53 /** 54 * Returns the hash code of a non-{@code null} argument and 0 for a {@code null} argument. 55 * 56 * @param o an object 57 * @return the hash code of a non-{@code null} argument and 0 for a {@code null} argument 58 * @see Object#hashCode 59 */ hashCode(@ullable Object o)60 public static int hashCode(@Nullable Object o) { 61 return o != null ? o.hashCode() : 0; 62 } 63 64 /** 65 * Generates a hash code for a sequence of input values. The hash code is generated as if all 66 * the input values were placed into an array, and that array were hashed by calling 67 * {@link Arrays#hashCode(Object[])}. 68 * 69 * <p>This method is useful for implementing {@link Object#hashCode()} on objects containing 70 * multiple fields. For example, if an object that has three fields, {@code x}, {@code y}, and 71 * {@code z}, one could write: 72 * 73 * <blockquote><pre> 74 * @Override public int hashCode() { 75 * return ObjectsCompat.hash(x, y, z); 76 * } 77 * </pre></blockquote> 78 * 79 * <b>Warning: When a single object reference is supplied, the returned value does not equal the 80 * hash code of that object reference.</b> This value can be computed by calling 81 * {@link #hashCode(Object)}. 82 * 83 * @param values the values to be hashed 84 * @return a hash value of the sequence of input values 85 * @see Arrays#hashCode(Object[]) 86 */ hash(Object @ullable .... values)87 public static int hash(Object @Nullable ... values) { 88 return Objects.hash(values); 89 } 90 91 /** 92 * Returns the result of calling {@code toString} on the first argument if the first argument 93 * is not {@code null} and returns the second argument otherwise. 94 * 95 * @param o an object 96 * @param nullDefault string to return if the first argument is {@code null} 97 * @return the result of calling {@code toString} on the first argument if it is not {@code 98 * null} and the second argument otherwise. 99 */ toString(@ullable Object o, @Nullable String nullDefault)100 public static @Nullable String toString(@Nullable Object o, @Nullable String nullDefault) { 101 return (o != null) ? o.toString() : nullDefault; 102 } 103 104 /** 105 * Checks that the specified object reference is not {@code null}. This 106 * method is designed primarily for doing parameter validation in methods 107 * and constructors, as demonstrated below: 108 * <blockquote><pre> 109 * public Foo(Bar bar) { 110 * this.bar = Objects.requireNonNull(bar); 111 * } 112 * </pre></blockquote> 113 * 114 * @param obj the object reference to check for nullity 115 * @param <T> the type of the reference 116 * @return {@code obj} if not {@code null} 117 * @throws NullPointerException if {@code obj} is {@code null} 118 */ requireNonNull(@ullable T obj)119 public static <T> @NonNull T requireNonNull(@Nullable T obj) { 120 if (obj == null) throw new NullPointerException(); 121 return obj; 122 } 123 124 /** 125 * Checks that the specified object reference is not {@code null} and 126 * throws a customized {@link NullPointerException} if it is. This method 127 * is designed primarily for doing parameter validation in methods and 128 * constructors with multiple parameters, as demonstrated below: 129 * <blockquote><pre> 130 * public Foo(Bar bar, Baz baz) { 131 * this.bar = Objects.requireNonNull(bar, "bar must not be null"); 132 * this.baz = Objects.requireNonNull(baz, "baz must not be null"); 133 * } 134 * </pre></blockquote> 135 * 136 * @param obj the object reference to check for nullity 137 * @param message detail message to be used in the event that a {@code 138 * NullPointerException} is thrown 139 * @param <T> the type of the reference 140 * @return {@code obj} if not {@code null} 141 * @throws NullPointerException if {@code obj} is {@code null} 142 */ requireNonNull(@ullable T obj, @NonNull String message)143 public static <T> @NonNull T requireNonNull(@Nullable T obj, @NonNull String message) { 144 if (obj == null) throw new NullPointerException(message); 145 return obj; 146 } 147 } 148