jdk/src/share/classes/java/util/Objects.java
author dl
Mon, 02 Nov 2009 17:25:38 -0800
changeset 4110 ac033ba6ede4
parent 4061 35a627ad2443
child 4165 7cd799c224da
permissions -rw-r--r--
6865582: jsr166y - jsr166 maintenance update 6865571: Add a lightweight task framework known as ForkJoin 6445158: Phaser - an improved CyclicBarrier 6865579: Add TransferQueue/LinkedTransferQueue Reviewed-by: martin, chegar, dice
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     1
/*
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     4
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    10
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    15
 * accompanied this code).
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    16
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    20
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    23
 * have any questions.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    24
 */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    25
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    26
package java.util;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    27
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    28
/**
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    29
 * This class consists of {@code static} utility methods for operating
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    30
 * on objects.  These utilities include {@code null}-safe or {@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    31
 * null}-tolerant methods for computing the hash code of an object,
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    32
 * returning a string for an object, and comparing two objects.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    33
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    34
 * @since 1.7
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    35
 */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    36
public class Objects {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    37
    private Objects() {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    38
        throw new AssertionError("No java.util.Objects instances for you!");
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    39
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    40
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    41
    /**
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    42
     * Returns {@code true} if the arguments are equal to each other
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    43
     * and {@code false} otherwise.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    44
     * Consequently, if both arguments are {@code null}, {@code true}
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    45
     * is returned and if exactly one argument is {@code null}, {@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    46
     * false} is returned.  Otherwise, equality is determined by using
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    47
     * the {@link Object#equals equals} method of the first
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    48
     * argument.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    49
     *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    50
     * @param a an object
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    51
     * @param b an object to be compared with {@code a} for equality
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    52
     * @return {@code true} if the arguments are equal to each other
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    53
     * and {@code false} otherwise
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    54
     * @see Object#equals(Object)
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    55
     */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    56
    public static boolean equals(Object a, Object b) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    57
        return (a == b) || (a != null && a.equals(b));
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    58
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    59
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    60
    /**
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    61
     * Returns the hash code of a non-{@code null} argument and 0 for
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    62
     * a {@code null} argument.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    63
     *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    64
     * @param o an object
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    65
     * @return the hash code of a non-{@code null} argument and 0 for
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    66
     * a {@code null} argument
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    67
     * @see Object#hashCode
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    68
     */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    69
    public static int hashCode(Object o) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    70
        return o != null ? o.hashCode() : 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    71
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    72
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    73
    /**
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    74
     * Returns the result of calling {@code toString} for a non-{@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    75
     * null} argument and {@code "null"} for a {@code null} argument.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    76
     *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    77
     * @param o an object
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    78
     * @return the result of calling {@code toString} for a non-{@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    79
     * null} argument and {@code "null"} for a {@code null} argument
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    80
     * @see Object#toString
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    81
     * @see String#valueOf(Object)
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    82
     */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    83
    public static String toString(Object o) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    84
        return String.valueOf(o);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    85
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    86
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    87
    /**
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    88
     * Returns 0 if the arguments are identical and {@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    89
     * c.compare(a, b)} otherwise.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    90
     * Consequently, if both arguments are {@code null} 0
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    91
     * is returned.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    92
     *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    93
     * <p>Note that if one of the arguments is {@code null}, a {@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    94
     * NullPointerException} may or may not be thrown depending on
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    95
     * what ordering policy, if any, the {@link Comparator Comparator}
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    96
     * chooses to have for {@code null} values.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    97
     *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    98
     * @param <T> the type of the objects being compared
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    99
     * @param a an object
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   100
     * @param b an object to be compared with {@code a}
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   101
     * @param c the {@code Comparator} to compare the first two arguments
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   102
     * @return 0 if the arguments are identical and {@code
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   103
     * c.compare(a, b)} otherwise.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   104
     * @see Comparable
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   105
     * @see Comparator
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   106
     */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   107
    public static <T> int compare(T a, T b, Comparator<? super T> c) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   108
        return (a == b) ? 0 :  c.compare(a, b);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   109
    }
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   110
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   111
    /**
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   112
     * Checks that the specified object reference is not {@code null}. This
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   113
     * method is designed primarily for doing parameter validation in methods
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   114
     * and constructors, as demonstrated below:
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   115
     * <blockquote><pre>
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   116
     * public Foo(Bar bar) {
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   117
     *     this.bar = Objects.nonNull(bar);
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   118
     * }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   119
     * </pre></blockquote>
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   120
     *
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   121
     * @param obj the object reference to check for nullity
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   122
     * @param <T> the type of the reference
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   123
     * @return {@code obj} if not {@code null}
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   124
     * @throws NullPointerException if {@code obj} is {@code null}
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   125
     */
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   126
    public static <T> T nonNull(T obj) {
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   127
        if (obj == null)
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   128
            throw new NullPointerException();
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   129
        return obj;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   130
    }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   131
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   132
    /**
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   133
     * Checks that the specified object reference is not {@code null} and
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   134
     * throws a customized {@link NullPointerException} if it is. This method
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   135
     * is designed primarily for doing parameter validation in methods and
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   136
     * constructors with multiple parameters, as demonstrated below:
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   137
     * <blockquote><pre>
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   138
     * public Foo(Bar bar, Baz baz) {
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   139
     *     this.bar = Objects.nonNull(bar, "bar must not be null");
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   140
     *     this.baz = Objects.nonNull(baz, "baz must not be null");
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   141
     * }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   142
     * </pre></blockquote>
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   143
     *
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   144
     * @param obj     the object reference to check for nullity
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   145
     * @param message detail message to be used in the event that a {@code
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   146
     *                NullPointerException} is thrown
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   147
     * @param <T> the type of the reference
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   148
     * @return {@code obj} if not {@code null}
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   149
     * @throws NullPointerException if {@code obj} is {@code null}
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   150
     */
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   151
    public static <T> T nonNull(T obj, String message) {
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   152
        if (obj == null)
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   153
            throw new NullPointerException(message);
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   154
        return obj;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   155
    }
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   156
}