rt/emul/compact/src/main/java/java/util/Objects.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 22 Feb 2015 09:00:14 +0100
changeset 1781 681e61714a1d
parent 682 5d25a1df3540
permissions -rw-r--r--
Locale.getDefault() reads real value from navigator.language & co.
jaroslav@682
     1
/*
jaroslav@682
     2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@682
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@682
     4
 *
jaroslav@682
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@682
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@682
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@682
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@682
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@682
    10
 *
jaroslav@682
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@682
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@682
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@682
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@682
    15
 * accompanied this code).
jaroslav@682
    16
 *
jaroslav@682
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@682
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@682
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@682
    20
 *
jaroslav@682
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@682
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@682
    23
 * questions.
jaroslav@682
    24
 */
jaroslav@682
    25
jaroslav@682
    26
package java.util;
jaroslav@682
    27
jaroslav@682
    28
/**
jaroslav@682
    29
 * This class consists of {@code static} utility methods for operating
jaroslav@682
    30
 * on objects.  These utilities include {@code null}-safe or {@code
jaroslav@682
    31
 * null}-tolerant methods for computing the hash code of an object,
jaroslav@682
    32
 * returning a string for an object, and comparing two objects.
jaroslav@682
    33
 *
jaroslav@682
    34
 * @since 1.7
jaroslav@682
    35
 */
jaroslav@682
    36
public final class Objects {
jaroslav@682
    37
    private Objects() {
jaroslav@682
    38
        throw new AssertionError("No java.util.Objects instances for you!");
jaroslav@682
    39
    }
jaroslav@682
    40
jaroslav@682
    41
    /**
jaroslav@682
    42
     * Returns {@code true} if the arguments are equal to each other
jaroslav@682
    43
     * and {@code false} otherwise.
jaroslav@682
    44
     * Consequently, if both arguments are {@code null}, {@code true}
jaroslav@682
    45
     * is returned and if exactly one argument is {@code null}, {@code
jaroslav@682
    46
     * false} is returned.  Otherwise, equality is determined by using
jaroslav@682
    47
     * the {@link Object#equals equals} method of the first
jaroslav@682
    48
     * argument.
jaroslav@682
    49
     *
jaroslav@682
    50
     * @param a an object
jaroslav@682
    51
     * @param b an object to be compared with {@code a} for equality
jaroslav@682
    52
     * @return {@code true} if the arguments are equal to each other
jaroslav@682
    53
     * and {@code false} otherwise
jaroslav@682
    54
     * @see Object#equals(Object)
jaroslav@682
    55
     */
jaroslav@682
    56
    public static boolean equals(Object a, Object b) {
jaroslav@682
    57
        return (a == b) || (a != null && a.equals(b));
jaroslav@682
    58
    }
jaroslav@682
    59
jaroslav@682
    60
   /**
jaroslav@682
    61
    * Returns {@code true} if the arguments are deeply equal to each other
jaroslav@682
    62
    * and {@code false} otherwise.
jaroslav@682
    63
    *
jaroslav@682
    64
    * Two {@code null} values are deeply equal.  If both arguments are
jaroslav@682
    65
    * arrays, the algorithm in {@link Arrays#deepEquals(Object[],
jaroslav@682
    66
    * Object[]) Arrays.deepEquals} is used to determine equality.
jaroslav@682
    67
    * Otherwise, equality is determined by using the {@link
jaroslav@682
    68
    * Object#equals equals} method of the first argument.
jaroslav@682
    69
    *
jaroslav@682
    70
    * @param a an object
jaroslav@682
    71
    * @param b an object to be compared with {@code a} for deep equality
jaroslav@682
    72
    * @return {@code true} if the arguments are deeply equal to each other
jaroslav@682
    73
    * and {@code false} otherwise
jaroslav@682
    74
    * @see Arrays#deepEquals(Object[], Object[])
jaroslav@682
    75
    * @see Objects#equals(Object, Object)
jaroslav@682
    76
    */
jaroslav@682
    77
    public static boolean deepEquals(Object a, Object b) {
jaroslav@682
    78
        if (a == b)
jaroslav@682
    79
            return true;
jaroslav@682
    80
        else if (a == null || b == null)
jaroslav@682
    81
            return false;
jaroslav@682
    82
        else
jaroslav@682
    83
            return Arrays.deepEquals0(a, b);
jaroslav@682
    84
    }
jaroslav@682
    85
jaroslav@682
    86
    /**
jaroslav@682
    87
     * Returns the hash code of a non-{@code null} argument and 0 for
jaroslav@682
    88
     * a {@code null} argument.
jaroslav@682
    89
     *
jaroslav@682
    90
     * @param o an object
jaroslav@682
    91
     * @return the hash code of a non-{@code null} argument and 0 for
jaroslav@682
    92
     * a {@code null} argument
jaroslav@682
    93
     * @see Object#hashCode
jaroslav@682
    94
     */
jaroslav@682
    95
    public static int hashCode(Object o) {
jaroslav@682
    96
        return o != null ? o.hashCode() : 0;
jaroslav@682
    97
    }
jaroslav@682
    98
jaroslav@682
    99
   /**
jaroslav@682
   100
    * Generates a hash code for a sequence of input values. The hash
jaroslav@682
   101
    * code is generated as if all the input values were placed into an
jaroslav@682
   102
    * array, and that array were hashed by calling {@link
jaroslav@682
   103
    * Arrays#hashCode(Object[])}.
jaroslav@682
   104
    *
jaroslav@682
   105
    * <p>This method is useful for implementing {@link
jaroslav@682
   106
    * Object#hashCode()} on objects containing multiple fields. For
jaroslav@682
   107
    * example, if an object that has three fields, {@code x}, {@code
jaroslav@682
   108
    * y}, and {@code z}, one could write:
jaroslav@682
   109
    *
jaroslav@682
   110
    * <blockquote><pre>
jaroslav@682
   111
    * &#064;Override public int hashCode() {
jaroslav@682
   112
    *     return Objects.hash(x, y, z);
jaroslav@682
   113
    * }
jaroslav@682
   114
    * </pre></blockquote>
jaroslav@682
   115
    *
jaroslav@682
   116
    * <b>Warning: When a single object reference is supplied, the returned
jaroslav@682
   117
    * value does not equal the hash code of that object reference.</b> This
jaroslav@682
   118
    * value can be computed by calling {@link #hashCode(Object)}.
jaroslav@682
   119
    *
jaroslav@682
   120
    * @param values the values to be hashed
jaroslav@682
   121
    * @return a hash value of the sequence of input values
jaroslav@682
   122
    * @see Arrays#hashCode(Object[])
jaroslav@682
   123
    * @see List#hashCode
jaroslav@682
   124
    */
jaroslav@682
   125
    public static int hash(Object... values) {
jaroslav@682
   126
        return Arrays.hashCode(values);
jaroslav@682
   127
    }
jaroslav@682
   128
jaroslav@682
   129
    /**
jaroslav@682
   130
     * Returns the result of calling {@code toString} for a non-{@code
jaroslav@682
   131
     * null} argument and {@code "null"} for a {@code null} argument.
jaroslav@682
   132
     *
jaroslav@682
   133
     * @param o an object
jaroslav@682
   134
     * @return the result of calling {@code toString} for a non-{@code
jaroslav@682
   135
     * null} argument and {@code "null"} for a {@code null} argument
jaroslav@682
   136
     * @see Object#toString
jaroslav@682
   137
     * @see String#valueOf(Object)
jaroslav@682
   138
     */
jaroslav@682
   139
    public static String toString(Object o) {
jaroslav@682
   140
        return String.valueOf(o);
jaroslav@682
   141
    }
jaroslav@682
   142
jaroslav@682
   143
    /**
jaroslav@682
   144
     * Returns the result of calling {@code toString} on the first
jaroslav@682
   145
     * argument if the first argument is not {@code null} and returns
jaroslav@682
   146
     * the second argument otherwise.
jaroslav@682
   147
     *
jaroslav@682
   148
     * @param o an object
jaroslav@682
   149
     * @param nullDefault string to return if the first argument is
jaroslav@682
   150
     *        {@code null}
jaroslav@682
   151
     * @return the result of calling {@code toString} on the first
jaroslav@682
   152
     * argument if it is not {@code null} and the second argument
jaroslav@682
   153
     * otherwise.
jaroslav@682
   154
     * @see Objects#toString(Object)
jaroslav@682
   155
     */
jaroslav@682
   156
    public static String toString(Object o, String nullDefault) {
jaroslav@682
   157
        return (o != null) ? o.toString() : nullDefault;
jaroslav@682
   158
    }
jaroslav@682
   159
jaroslav@682
   160
    /**
jaroslav@682
   161
     * Returns 0 if the arguments are identical and {@code
jaroslav@682
   162
     * c.compare(a, b)} otherwise.
jaroslav@682
   163
     * Consequently, if both arguments are {@code null} 0
jaroslav@682
   164
     * is returned.
jaroslav@682
   165
     *
jaroslav@682
   166
     * <p>Note that if one of the arguments is {@code null}, a {@code
jaroslav@682
   167
     * NullPointerException} may or may not be thrown depending on
jaroslav@682
   168
     * what ordering policy, if any, the {@link Comparator Comparator}
jaroslav@682
   169
     * chooses to have for {@code null} values.
jaroslav@682
   170
     *
jaroslav@682
   171
     * @param <T> the type of the objects being compared
jaroslav@682
   172
     * @param a an object
jaroslav@682
   173
     * @param b an object to be compared with {@code a}
jaroslav@682
   174
     * @param c the {@code Comparator} to compare the first two arguments
jaroslav@682
   175
     * @return 0 if the arguments are identical and {@code
jaroslav@682
   176
     * c.compare(a, b)} otherwise.
jaroslav@682
   177
     * @see Comparable
jaroslav@682
   178
     * @see Comparator
jaroslav@682
   179
     */
jaroslav@682
   180
    public static <T> int compare(T a, T b, Comparator<? super T> c) {
jaroslav@682
   181
        return (a == b) ? 0 :  c.compare(a, b);
jaroslav@682
   182
    }
jaroslav@682
   183
jaroslav@682
   184
    /**
jaroslav@682
   185
     * Checks that the specified object reference is not {@code null}. This
jaroslav@682
   186
     * method is designed primarily for doing parameter validation in methods
jaroslav@682
   187
     * and constructors, as demonstrated below:
jaroslav@682
   188
     * <blockquote><pre>
jaroslav@682
   189
     * public Foo(Bar bar) {
jaroslav@682
   190
     *     this.bar = Objects.requireNonNull(bar);
jaroslav@682
   191
     * }
jaroslav@682
   192
     * </pre></blockquote>
jaroslav@682
   193
     *
jaroslav@682
   194
     * @param obj the object reference to check for nullity
jaroslav@682
   195
     * @param <T> the type of the reference
jaroslav@682
   196
     * @return {@code obj} if not {@code null}
jaroslav@682
   197
     * @throws NullPointerException if {@code obj} is {@code null}
jaroslav@682
   198
     */
jaroslav@682
   199
    public static <T> T requireNonNull(T obj) {
jaroslav@682
   200
        if (obj == null)
jaroslav@682
   201
            throw new NullPointerException();
jaroslav@682
   202
        return obj;
jaroslav@682
   203
    }
jaroslav@682
   204
jaroslav@682
   205
    /**
jaroslav@682
   206
     * Checks that the specified object reference is not {@code null} and
jaroslav@682
   207
     * throws a customized {@link NullPointerException} if it is. This method
jaroslav@682
   208
     * is designed primarily for doing parameter validation in methods and
jaroslav@682
   209
     * constructors with multiple parameters, as demonstrated below:
jaroslav@682
   210
     * <blockquote><pre>
jaroslav@682
   211
     * public Foo(Bar bar, Baz baz) {
jaroslav@682
   212
     *     this.bar = Objects.requireNonNull(bar, "bar must not be null");
jaroslav@682
   213
     *     this.baz = Objects.requireNonNull(baz, "baz must not be null");
jaroslav@682
   214
     * }
jaroslav@682
   215
     * </pre></blockquote>
jaroslav@682
   216
     *
jaroslav@682
   217
     * @param obj     the object reference to check for nullity
jaroslav@682
   218
     * @param message detail message to be used in the event that a {@code
jaroslav@682
   219
     *                NullPointerException} is thrown
jaroslav@682
   220
     * @param <T> the type of the reference
jaroslav@682
   221
     * @return {@code obj} if not {@code null}
jaroslav@682
   222
     * @throws NullPointerException if {@code obj} is {@code null}
jaroslav@682
   223
     */
jaroslav@682
   224
    public static <T> T requireNonNull(T obj, String message) {
jaroslav@682
   225
        if (obj == null)
jaroslav@682
   226
            throw new NullPointerException(message);
jaroslav@682
   227
        return obj;
jaroslav@682
   228
    }
jaroslav@682
   229
}