src/share/classes/java/util/Objects.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 2011, 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 /**
  29  * This class consists of {@code static} utility methods for operating
  30  * on objects.  These utilities include {@code null}-safe or {@code
  31  * null}-tolerant methods for computing the hash code of an object,
  32  * returning a string for an object, and comparing two objects.
  33  *
  34  * @since 1.7
  35  */
  36 public final class Objects {
  37     private Objects() {
  38         throw new AssertionError("No java.util.Objects instances for you!");
  39     }
  40 
  41     /**
  42      * Returns {@code true} if the arguments are equal to each other
  43      * and {@code false} otherwise.
  44      * Consequently, if both arguments are {@code null}, {@code true}
  45      * is returned and if exactly one argument is {@code null}, {@code
  46      * false} is returned.  Otherwise, equality is determined by using
  47      * the {@link Object#equals equals} method of the first


 207      * throws a customized {@link NullPointerException} if it is. This method
 208      * is designed primarily for doing parameter validation in methods and
 209      * constructors with multiple parameters, as demonstrated below:
 210      * <blockquote><pre>
 211      * public Foo(Bar bar, Baz baz) {
 212      *     this.bar = Objects.requireNonNull(bar, "bar must not be null");
 213      *     this.baz = Objects.requireNonNull(baz, "baz must not be null");
 214      * }
 215      * </pre></blockquote>
 216      *
 217      * @param obj     the object reference to check for nullity
 218      * @param message detail message to be used in the event that a {@code
 219      *                NullPointerException} is thrown
 220      * @param <T> the type of the reference
 221      * @return {@code obj} if not {@code null}
 222      * @throws NullPointerException if {@code obj} is {@code null}
 223      */
 224     public static <T> T requireNonNull(T obj, String message) {
 225         if (obj == null)
 226             throw new NullPointerException(message);


























 227         return obj;
 228     }
 229 }
   1 /*
   2  * Copyright (c) 2009, 2013, 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 java.util.function.Supplier;
  29 
  30 /**
  31  * This class consists of {@code static} utility methods for operating
  32  * on objects.  These utilities include {@code null}-safe or {@code
  33  * null}-tolerant methods for computing the hash code of an object,
  34  * returning a string for an object, and comparing two objects.
  35  *
  36  * @since 1.7
  37  */
  38 public final class Objects {
  39     private Objects() {
  40         throw new AssertionError("No java.util.Objects instances for you!");
  41     }
  42 
  43     /**
  44      * Returns {@code true} if the arguments are equal to each other
  45      * and {@code false} otherwise.
  46      * Consequently, if both arguments are {@code null}, {@code true}
  47      * is returned and if exactly one argument is {@code null}, {@code
  48      * false} is returned.  Otherwise, equality is determined by using
  49      * the {@link Object#equals equals} method of the first


 209      * throws a customized {@link NullPointerException} if it is. This method
 210      * is designed primarily for doing parameter validation in methods and
 211      * constructors with multiple parameters, as demonstrated below:
 212      * <blockquote><pre>
 213      * public Foo(Bar bar, Baz baz) {
 214      *     this.bar = Objects.requireNonNull(bar, "bar must not be null");
 215      *     this.baz = Objects.requireNonNull(baz, "baz must not be null");
 216      * }
 217      * </pre></blockquote>
 218      *
 219      * @param obj     the object reference to check for nullity
 220      * @param message detail message to be used in the event that a {@code
 221      *                NullPointerException} is thrown
 222      * @param <T> the type of the reference
 223      * @return {@code obj} if not {@code null}
 224      * @throws NullPointerException if {@code obj} is {@code null}
 225      */
 226     public static <T> T requireNonNull(T obj, String message) {
 227         if (obj == null)
 228             throw new NullPointerException(message);
 229         return obj;
 230     }
 231 
 232     /**
 233      * Checks that the specified object reference is not {@code null} and
 234      * throws a customized {@link NullPointerException} if it is.
 235      *
 236      * <p>Unlike the method {@link requireNonNull(Object, String},
 237      * this method allows creation of the message to be deferred until
 238      * after the null check is made. While this may confer a
 239      * performance advantage in the non-null case, when deciding to
 240      * call this method care should be taken that the costs of
 241      * creating the message supplier are less than the cost of just
 242      * creating the string message directly.
 243      *
 244      * @param obj     the object reference to check for nullity
 245      * @param messageSupplier supplier of the detail message to be
 246      * used in the event that a {@code NullPointerException} is thrown
 247      * @param <T> the type of the reference
 248      * @return {@code obj} if not {@code null}
 249      * @throws NullPointerException if {@code obj} is {@code null}
 250      * @since 1.8
 251      */
 252     public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
 253         if (obj == null)
 254             throw new NullPointerException(messageSupplier.get());
 255         return obj;
 256     }
 257 }