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.
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 */
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
|
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.util.Preconditions;
30 import jdk.internal.vm.annotation.ForceInline;
31 import jdk.internal.misc.Unsafe;
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 * Returns {@code true} if the arguments are equal to each other
51 * and {@code false} otherwise.
173 *
174 * @implNote
175 * This method constructs a string for an object without calling
176 * any overridable methods of the object.
177 *
178 * @implSpec
179 * The method returns a string equivalent to:<br>
180 * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
181 *
182 * @param o an object
183 * @throws NullPointerException if the argument is null
184 * @see Object#toString
185 * @see System#identityHashCode(Object)
186 * @since 19
187 */
188 public static String toIdentityString(Object o) {
189 requireNonNull(o);
190 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
191 }
192
193 /**
194 * {@return {@code true} if the specified object reference is an identity object,
195 * otherwise {@code false}}
196 *
197 * @param obj an object
198 * @throws NullPointerException if {@code obj} is {@code null}
199 */
200 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
201 // @IntrinsicCandidate
202 public static boolean isIdentityObject(Object obj) {
203 requireNonNull(obj);
204 return obj.getClass().isIdentity() || // Before Valhalla all classes are identity classes
205 obj.getClass() == Object.class;
206 }
207
208 /**
209 * Checks that the specified object reference is an identity object.
210 *
211 * @param obj the object reference to check for identity
212 * @param <T> the type of the reference
213 * @return {@code obj} if {@code obj} is an identity object
214 * @throws NullPointerException if {@code obj} is {@code null}
215 * @throws IdentityException if {@code obj} is not an identity object
216 * @since Valhalla
217 */
218 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
219 @ForceInline
220 public static <T> T requireIdentity(T obj) {
221 Objects.requireNonNull(obj);
222 if (!isIdentityObject(obj))
223 throw new IdentityException(obj.getClass());
224 return obj;
225 }
226
227 /**
228 * Checks that the specified object reference is an identity object.
229 *
230 * @param obj the object reference to check for identity
231 * @param message detail message to be used in the event that an
232 * {@code IdentityException} is thrown; may be null
233 * @param <T> the type of the reference
234 * @return {@code obj} if {@code obj} is an identity object
235 * @throws NullPointerException if {@code obj} is {@code null}
236 * @throws IdentityException if {@code obj} is not an identity object
237 * @since Valhalla
238 */
239 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
240 @ForceInline
241 public static <T> T requireIdentity(T obj, String message) {
242 Objects.requireNonNull(obj);
243 if (!isIdentityObject(obj))
244 throw new IdentityException(message);
245 return obj;
246 }
247
248 /**
249 * Checks that the specified object reference is an identity object.
250 *
251 * @param obj the object reference to check for identity
252 * @param messageSupplier supplier of the detail message to be
253 * used in the event that an {@code IdentityException} is thrown; may be null
254 * @param <T> the type of the reference
255 * @return {@code obj} if {@code obj} is an identity object
256 * @throws NullPointerException if {@code obj} is {@code null}
257 * @throws IdentityException if {@code obj} is not an identity object
258 * @since Valhalla
259 */
260 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
261 @ForceInline
262 public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
263 Objects.requireNonNull(obj);
264 if (!isIdentityObject(obj))
265 throw new IdentityException(messageSupplier == null ?
266 null : messageSupplier.get());
267 return obj;
268 }
269
270 /**
271 * {@return {@code true} if the specified object is a {@linkplain Class#isValue value object},
272 * otherwise {@code false}}
273 *
274 * @param obj an object
275 * @throws NullPointerException if {@code obj} is {@code null}
276 */
277 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
278 // @IntrinsicCandidate
279 public static boolean isValueObject(Object obj) {
280 requireNonNull(obj, "obj");
281 return obj.getClass().isValue();
282 }
283
284 /**
285 * Returns 0 if the arguments are identical and {@code
286 * c.compare(a, b)} otherwise.
287 * Consequently, if both arguments are {@code null} 0
288 * is returned.
289 *
290 * <p>Note that if one of the arguments is {@code null}, a {@code
291 * NullPointerException} may or may not be thrown depending on
292 * what ordering policy, if any, the {@link Comparator Comparator}
293 * chooses to have for {@code null} values.
294 *
295 * @param <T> the type of the objects being compared
296 * @param a an object
297 * @param b an object to be compared with {@code a}
298 * @param c the {@code Comparator} to compare the first two arguments
299 * @return 0 if the arguments are identical and {@code
300 * c.compare(a, b)} otherwise.
301 * @see Comparable
302 * @see Comparator
303 */
513 * inequalities is true:
514 * <ul>
515 * <li>{@code fromIndex < 0}</li>
516 * <li>{@code size < 0}</li>
517 * <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
518 * <li>{@code length < 0}, which is implied from the former inequalities</li>
519 * </ul>
520 *
521 * @param fromIndex the lower-bound (inclusive) of the sub-interval
522 * @param size the size of the sub-range
523 * @param length the upper-bound (exclusive) of the range
524 * @return {@code fromIndex} if the sub-range within bounds of the range
525 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
526 * @since 9
527 */
528 public static
529 int checkFromIndexSize(int fromIndex, int size, int length) {
530 return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
531 }
532
533 /**
534 * Return the size of the object in the heap.
535 *
536 * @param o an object
537 * @return the objects's size
538 * @since Valhalla
539 */
540 public static long getObjectSize(Object o) {
541 return Unsafe.getUnsafe().getObjectSize(o);
542 }
543
544 /**
545 * Checks if the {@code index} is within the bounds of the range from
546 * {@code 0} (inclusive) to {@code length} (exclusive).
547 *
548 * <p>The {@code index} is defined to be out of bounds if any of the
549 * following inequalities is true:
550 * <ul>
551 * <li>{@code index < 0}</li>
552 * <li>{@code index >= length}</li>
553 * <li>{@code length < 0}, which is implied from the former inequalities</li>
554 * </ul>
555 *
556 * @param index the index
557 * @param length the upper-bound (exclusive) of the range
558 * @return {@code index} if it is within bounds of the range
559 * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
560 * @since 16
561 */
562 @ForceInline
563 public static
|