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


 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


 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>Compared to the sibling method {@link requireNonNull(Object,
 237      * String}, this methods allows creation of the message to be
 238      * deferred until after the null check is made. Note that if the
 239      * supplier is provided via a lambda expression, there can be an
 240      * overhead involved in creating the supplier. Therefore, while
 241      * this method may confer a net performance advantage in the
 242      * non-null case, it is most likely to do so if creating the
 243      * message string is expensive.
 244      *
 245      * @param obj     the object reference to check for nullity
 246      * @param messageSupplier supplier of the detail message to be
 247      * used in the event that a {@code NullPointerException} is thrown
 248      * @param <T> the type of the reference
 249      * @return {@code obj} if not {@code null}
 250      * @throws NullPointerException if {@code obj} is {@code null}
 251      * @since 1.8
 252      */
 253     public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
 254         if (obj == null)
 255             throw new NullPointerException(messageSupplier.get());
 256         return obj;
 257     }    
 258 }