1 /*
2 * Copyright (c) 2009, 2023, 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 * {@return {@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 * @see Object#equals(Object)
60 */
61 public static boolean equals(Object a, Object b) {
62 return (a == b) || (a != null && a.equals(b));
63 }
64
65 /**
66 * {@return {@code true} if the arguments are deeply equal to each other
67 * and {@code false} otherwise}
68 *
69 * Two {@code null} values are deeply equal. If both arguments are
70 * arrays, the algorithm in {@link Arrays#deepEquals(Object[],
71 * Object[]) Arrays.deepEquals} is used to determine equality.
72 * Otherwise, equality is determined by using the {@link
73 * Object#equals equals} method of the first argument.
74 *
75 * @param a an object
76 * @param b an object to be compared with {@code a} for deep equality
77 * @see Arrays#deepEquals(Object[], Object[])
78 * @see Objects#equals(Object, Object)
79 */
80 public static boolean deepEquals(Object a, Object b) {
81 if (a == b)
82 return true;
160 *
161 * @implNote
162 * This method constructs a string for an object without calling
163 * any overridable methods of the object.
164 *
165 * @implSpec
166 * The method returns a string equivalent to:<br>
167 * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
168 *
169 * @param o an object
170 * @throws NullPointerException if the argument is null
171 * @see Object#toString
172 * @see System#identityHashCode(Object)
173 * @since 19
174 */
175 public static String toIdentityString(Object o) {
176 requireNonNull(o);
177 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
178 }
179
180 /**
181 * {@return 0 if the arguments are identical and {@code
182 * c.compare(a, b)} otherwise}
183 * Consequently, if both arguments are {@code null} 0
184 * is returned.
185 *
186 * <p>Note that if one of the arguments is {@code null}, a {@code
187 * NullPointerException} may or may not be thrown depending on
188 * what ordering policy, if any, the {@link Comparator Comparator}
189 * chooses to have for {@code null} values.
190 *
191 * @param <T> the type of the objects being compared
192 * @param a an object
193 * @param b an object to be compared with {@code a}
194 * @param c the {@code Comparator} to compare the first two arguments
195 * @see Comparable
196 * @see Comparator
197 */
198 public static <T> int compare(T a, T b, Comparator<? super T> c) {
199 return (a == b) ? 0 : c.compare(a, b);
|
1 /*
2 * Copyright (c) 2009, 2025, 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.javac.PreviewFeature;
29 import jdk.internal.misc.PreviewFeatures;
30 import jdk.internal.util.Preconditions;
31 import jdk.internal.vm.annotation.ForceInline;
32
33 import java.util.function.Supplier;
34
35 /**
36 * This class consists of {@code static} utility methods for operating
37 * on objects, or checking certain conditions before operation. These utilities
38 * include {@code null}-safe or {@code null}-tolerant methods for computing the
39 * hash code of an object, returning a string for an object, comparing two
40 * objects, and checking if indexes or sub-range values are out of bounds.
41 *
42 * @since 1.7
43 */
44 public final class Objects {
45 private Objects() {
46 throw new AssertionError("No java.util.Objects instances for you!");
47 }
48
49 /**
50 * {@return {@code true} if the arguments are equal to each other
51 * and {@code false} otherwise}
52 * Consequently, if both arguments are {@code null}, {@code true}
53 * is returned. Otherwise, if the first argument is not {@code
54 * null}, equality is determined by calling the {@link
55 * Object#equals equals} method of the first argument with the
56 * second argument of this method. Otherwise, {@code false} is
57 * returned.
58 *
59 * @param a an object
60 * @param b an object to be compared with {@code a} for equality
61 * @see Object#equals(Object)
62 */
63 @ForceInline
64 public static boolean equals(Object a, Object b) {
65 if (PreviewFeatures.isEnabled()) {
66 // With --enable-preview avoid acmp
67 return (a == null) ? b == null : a.equals(b);
68 } else {
69 return (a == b) || (a != null && a.equals(b));
70 }
71 }
72
73 /**
74 * {@return {@code true} if the arguments are deeply equal to each other
75 * and {@code false} otherwise}
76 *
77 * Two {@code null} values are deeply equal. If both arguments are
78 * arrays, the algorithm in {@link Arrays#deepEquals(Object[],
79 * Object[]) Arrays.deepEquals} is used to determine equality.
80 * Otherwise, equality is determined by using the {@link
81 * Object#equals equals} method of the first argument.
82 *
83 * @param a an object
84 * @param b an object to be compared with {@code a} for deep equality
85 * @see Arrays#deepEquals(Object[], Object[])
86 * @see Objects#equals(Object, Object)
87 */
88 public static boolean deepEquals(Object a, Object b) {
89 if (a == b)
90 return true;
168 *
169 * @implNote
170 * This method constructs a string for an object without calling
171 * any overridable methods of the object.
172 *
173 * @implSpec
174 * The method returns a string equivalent to:<br>
175 * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
176 *
177 * @param o an object
178 * @throws NullPointerException if the argument is null
179 * @see Object#toString
180 * @see System#identityHashCode(Object)
181 * @since 19
182 */
183 public static String toIdentityString(Object o) {
184 requireNonNull(o);
185 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
186 }
187
188 /**
189 * {@return {@code true} if the object is a non-null reference
190 * to an {@linkplain Class#isIdentity() identity object}, otherwise {@code false}}
191 *
192 * @apiNote
193 * If the parameter is {@code null}, there is no object
194 * and hence no class to check for identity; the return is {@code false}.
195 * To test for a {@linkplain Class#isValue() value object} use:
196 * {@snippet type="java" :
197 * if (obj != null && !Objects.hasIdentity(obj)) {
198 * // obj is a non-null value object
199 * }
200 * }
201 * @param obj an object or {@code null}
202 * @since Valhalla
203 */
204 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
205 // @IntrinsicCandidate
206 public static boolean hasIdentity(Object obj) {
207 return (obj == null) ? false : obj.getClass().isIdentity();
208 }
209
210 /**
211 * {@return {@code true} if the object is a non-null reference
212 * to a {@linkplain Class#isValue() value object}, otherwise {@code false}}
213 *
214 * @param obj an object or {@code null}
215 * @since Valhalla
216 */
217 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
218 // @IntrinsicCandidate
219 public static boolean isValueObject(Object obj) {
220 return (obj == null) ? false : obj.getClass().isValue();
221 }
222
223 /**
224 * Checks that the specified object reference is an identity object.
225 *
226 * @param obj the object reference to check for identity
227 * @param <T> the type of the reference
228 * @return {@code obj} if {@code obj} is an identity object
229 * @throws NullPointerException if {@code obj} is {@code null}
230 * @throws IdentityException if {@code obj} is not an identity object
231 * @since Valhalla
232 */
233 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
234 @ForceInline
235 public static <T> T requireIdentity(T obj) {
236 Objects.requireNonNull(obj);
237 if (!hasIdentity(obj))
238 throw new IdentityException(obj.getClass());
239 return obj;
240 }
241
242 /**
243 * Checks that the specified object reference is an identity object.
244 *
245 * @param obj the object reference to check for identity
246 * @param message detail message to be used in the event that an
247 * {@code IdentityException} is thrown; may be null
248 * @param <T> the type of the reference
249 * @return {@code obj} if {@code obj} is an identity object
250 * @throws NullPointerException if {@code obj} is {@code null}
251 * @throws IdentityException if {@code obj} is not an identity object
252 * @since Valhalla
253 */
254 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
255 @ForceInline
256 public static <T> T requireIdentity(T obj, String message) {
257 Objects.requireNonNull(obj);
258 if (!hasIdentity(obj))
259 throw new IdentityException(message);
260 return obj;
261 }
262
263 /**
264 * Checks that the specified object reference is an identity object.
265 *
266 * @param obj the object reference to check for identity
267 * @param messageSupplier supplier of the detail message to be
268 * used in the event that an {@code IdentityException} is thrown; may be null
269 * @param <T> the type of the reference
270 * @return {@code obj} if {@code obj} is an identity object
271 * @throws NullPointerException if {@code obj} is {@code null}
272 * @throws IdentityException if {@code obj} is not an identity object
273 * @since Valhalla
274 */
275 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
276 @ForceInline
277 public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
278 Objects.requireNonNull(obj);
279 if (!hasIdentity(obj))
280 throw new IdentityException(messageSupplier == null ?
281 null : messageSupplier.get());
282 return obj;
283 }
284
285 /**
286 * {@return 0 if the arguments are identical and {@code
287 * c.compare(a, b)} otherwise}
288 * Consequently, if both arguments are {@code null} 0
289 * is returned.
290 *
291 * <p>Note that if one of the arguments is {@code null}, a {@code
292 * NullPointerException} may or may not be thrown depending on
293 * what ordering policy, if any, the {@link Comparator Comparator}
294 * chooses to have for {@code null} values.
295 *
296 * @param <T> the type of the objects being compared
297 * @param a an object
298 * @param b an object to be compared with {@code a}
299 * @param c the {@code Comparator} to compare the first two arguments
300 * @see Comparable
301 * @see Comparator
302 */
303 public static <T> int compare(T a, T b, Comparator<? super T> c) {
304 return (a == b) ? 0 : c.compare(a, b);
|