author | darcy |
Fri, 09 Oct 2009 16:11:11 -0700 | |
changeset 4038 | ea7aa63169db |
child 4061 | 35a627ad2443 |
permissions | -rw-r--r-- |
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 |
} |
ea7aa63169db
6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff
changeset
|
110 |
} |