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}
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);
|
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 * {@return {@code true} if the arguments are equal to each other
51 * and {@code false} otherwise}
162 *
163 * @implNote
164 * This method constructs a string for an object without calling
165 * any overridable methods of the object.
166 *
167 * @implSpec
168 * The method returns a string equivalent to:<br>
169 * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
170 *
171 * @param o an object
172 * @throws NullPointerException if the argument is null
173 * @see Object#toString
174 * @see System#identityHashCode(Object)
175 * @since 19
176 */
177 public static String toIdentityString(Object o) {
178 requireNonNull(o);
179 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
180 }
181
182 /**
183 * {@return {@code true} if the specified object reference is an identity object,
184 * otherwise {@code false}}
185 *
186 * @param obj an object
187 * @throws NullPointerException if {@code obj} is {@code null}
188 * @since Valhalla
189 */
190 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
191 // @IntrinsicCandidate
192 public static boolean hasIdentity(Object obj) {
193 requireNonNull(obj);
194 return obj.getClass().isIdentity() || // Before Valhalla all classes are identity classes
195 obj.getClass() == Object.class;
196 }
197
198 /**
199 * Checks that the specified object reference is an identity object.
200 *
201 * @param obj the object reference to check for identity
202 * @param <T> the type of the reference
203 * @return {@code obj} if {@code obj} is an identity object
204 * @throws NullPointerException if {@code obj} is {@code null}
205 * @throws IdentityException if {@code obj} is not an identity object
206 * @since Valhalla
207 */
208 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
209 @ForceInline
210 public static <T> T requireIdentity(T obj) {
211 Objects.requireNonNull(obj);
212 if (!hasIdentity(obj))
213 throw new IdentityException(obj.getClass());
214 return obj;
215 }
216
217 /**
218 * Checks that the specified object reference is an identity object.
219 *
220 * @param obj the object reference to check for identity
221 * @param message detail message to be used in the event that an
222 * {@code IdentityException} is thrown; may be null
223 * @param <T> the type of the reference
224 * @return {@code obj} if {@code obj} is an identity object
225 * @throws NullPointerException if {@code obj} is {@code null}
226 * @throws IdentityException if {@code obj} is not an identity object
227 * @since Valhalla
228 */
229 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
230 @ForceInline
231 public static <T> T requireIdentity(T obj, String message) {
232 Objects.requireNonNull(obj);
233 if (!hasIdentity(obj))
234 throw new IdentityException(message);
235 return obj;
236 }
237
238 /**
239 * Checks that the specified object reference is an identity object.
240 *
241 * @param obj the object reference to check for identity
242 * @param messageSupplier supplier of the detail message to be
243 * used in the event that an {@code IdentityException} is thrown; may be null
244 * @param <T> the type of the reference
245 * @return {@code obj} if {@code obj} is an identity object
246 * @throws NullPointerException if {@code obj} is {@code null}
247 * @throws IdentityException if {@code obj} is not an identity object
248 * @since Valhalla
249 */
250 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
251 @ForceInline
252 public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
253 Objects.requireNonNull(obj);
254 if (!hasIdentity(obj))
255 throw new IdentityException(messageSupplier == null ?
256 null : messageSupplier.get());
257 return obj;
258 }
259
260 /**
261 * {@return 0 if the arguments are identical and {@code
262 * c.compare(a, b)} otherwise}
263 * Consequently, if both arguments are {@code null} 0
264 * is returned.
265 *
266 * <p>Note that if one of the arguments is {@code null}, a {@code
267 * NullPointerException} may or may not be thrown depending on
268 * what ordering policy, if any, the {@link Comparator Comparator}
269 * chooses to have for {@code null} values.
270 *
271 * @param <T> the type of the objects being compared
272 * @param a an object
273 * @param b an object to be compared with {@code a}
274 * @param c the {@code Comparator} to compare the first two arguments
275 * @see Comparable
276 * @see Comparator
277 */
278 public static <T> int compare(T a, T b, Comparator<? super T> c) {
279 return (a == b) ? 0 : c.compare(a, b);
|