-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathObjectHelper.java
94 lines (86 loc) · 3.07 KB
/
ObjectHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.functions;
import io.reactivex.functions.BiPredicate;
/**
* Utility methods containing the backport of Java 7's Objects utility class.
* <p>Named as such to avoid clash with java.util.Objects.
*/
public enum ObjectHelper {
;
/**
* Verifies if the object is not null and returns it or throws a NullPointerException
* with the given message.
* @param <T> the value type
* @param object the object to verify
* @param message the message to use with the NullPointerException
* @return the object itself
* @throws NullPointerException if object is null
*/
public static final <T> T requireNonNull(T object, String message) {
if (object == null) {
throw new NullPointerException(message);
}
return object;
}
/**
* Compares two potentially null objects with each other using Object.equals.
* @param o1 the first object
* @param o2 the second object
* @return the comparison result
*/
public static boolean equals(Object o1, Object o2) { // NOPMD
return o1 == o2 || (o1 != null && o1.equals(o2));
}
/**
* Returns the hashCode of a non-null object or zero for a null object.
* @param o the object to get the hashCode for.
* @return the hashCode
*/
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
/**
* Compares two integer values similar to Integer.compare.
* @param v1 the first value
* @param v2 the second value
* @return the comparison result
*/
public static int compare(int v1, int v2) {
return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0);
}
/**
* Compares two integer values similar to Long.compare.
* @param v1 the first value
* @param v2 the second value
* @return the comparison result
*/
public static int compare(long v1, long v2) {
return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0);
}
static final BiPredicate<Object, Object> EQUALS = new BiPredicate<Object, Object>() {
@Override
public boolean test(Object o1, Object o2) {
return ObjectHelper.equals(o1, o2);
}
};
/**
* Returns a BiPredicate that compares its parameters via Objects.equals().
* @param <T> the value type
* @return the bipredicate
*/
@SuppressWarnings("unchecked")
public static <T> BiPredicate<T, T> equalsPredicate() {
return (BiPredicate<T, T>)EQUALS;
}
}