-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
ObjectUtils.java
279 lines (241 loc) · 8.08 KB
/
ObjectUtils.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.blankj.utilcode.util;
import android.os.Build;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.util.SparseLongArray;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.collection.LongSparseArray;
import androidx.collection.SimpleArrayMap;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/12/24
* desc : utils about object
* </pre>
*/
public final class ObjectUtils {
private ObjectUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether object is empty.
*
* @param obj The object.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isEmpty(final Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
return true;
}
if (obj instanceof CharSequence && obj.toString().length() == 0) {
return true;
}
if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
return true;
}
if (obj instanceof Map && ((Map) obj).isEmpty()) {
return true;
}
if (obj instanceof SimpleArrayMap && ((SimpleArrayMap) obj).isEmpty()) {
return true;
}
if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) {
return true;
}
if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) {
return true;
}
if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) {
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) {
return true;
}
}
if (obj instanceof LongSparseArray && ((LongSparseArray) obj).size() == 0) {
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (obj instanceof android.util.LongSparseArray
&& ((android.util.LongSparseArray) obj).size() == 0) {
return true;
}
}
return false;
}
public static boolean isEmpty(final CharSequence obj) {
return obj == null || obj.toString().length() == 0;
}
public static boolean isEmpty(final Collection obj) {
return obj == null || obj.isEmpty();
}
public static boolean isEmpty(final Map obj) {
return obj == null || obj.isEmpty();
}
public static boolean isEmpty(final SimpleArrayMap obj) {
return obj == null || obj.isEmpty();
}
public static boolean isEmpty(final SparseArray obj) {
return obj == null || obj.size() == 0;
}
public static boolean isEmpty(final SparseBooleanArray obj) {
return obj == null || obj.size() == 0;
}
public static boolean isEmpty(final SparseIntArray obj) {
return obj == null || obj.size() == 0;
}
public static boolean isEmpty(final LongSparseArray obj) {
return obj == null || obj.size() == 0;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean isEmpty(final SparseLongArray obj) {
return obj == null || obj.size() == 0;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static boolean isEmpty(final android.util.LongSparseArray obj) {
return obj == null || obj.size() == 0;
}
/**
* Return whether object is not empty.
*
* @param obj The object.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isNotEmpty(final Object obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final CharSequence obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final Collection obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final Map obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final SimpleArrayMap obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final SparseArray obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final SparseBooleanArray obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final SparseIntArray obj) {
return !isEmpty(obj);
}
public static boolean isNotEmpty(final LongSparseArray obj) {
return !isEmpty(obj);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean isNotEmpty(final SparseLongArray obj) {
return !isEmpty(obj);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static boolean isNotEmpty(final android.util.LongSparseArray obj) {
return !isEmpty(obj);
}
/**
* Return whether object1 is equals to object2.
*
* @param o1 The first object.
* @param o2 The second object.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean equals(final Object o1, final Object o2) {
return o1 == o2 || (o1 != null && o1.equals(o2));
}
/**
* Returns 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise.
* Consequently, if both arguments are {@code null} 0
* is returned.
*/
public static <T> int compare(T a, T b, @NonNull Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
/**
* Checks that the specified object reference is not {@code null}.
*/
public static <T> T requireNonNull(T obj) {
if (obj == null) throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is.
*/
public static <T> T requireNonNull(T obj, String ifNullTip) {
if (obj == null) throw new NullPointerException(ifNullTip);
return obj;
}
/**
* Require the objects are not null.
*
* @param objects The object.
* @throws NullPointerException if any object is null in objects
*/
public static void requireNonNulls(final Object... objects) {
if (objects == null) throw new NullPointerException();
for (Object object : objects) {
if (object == null) throw new NullPointerException();
}
}
/**
* Return the nonnull object or default object.
*
* @param object The object.
* @param defaultObject The default object to use with the object is null.
* @param <T> The value type.
* @return the nonnull object or default object
*/
public static <T> T getOrDefault(final T object, final T defaultObject) {
if (object == null) {
return defaultObject;
}
return object;
}
/**
* Returns the result of calling {@code toString} for a non-{@code
* null} argument and {@code "null"} for a {@code null} argument.
*/
public static String toString(Object obj) {
return String.valueOf(obj);
}
/**
* Returns the result of calling {@code toString} on the first
* argument if the first argument is not {@code null} and returns
* the second argument otherwise.
*/
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
/**
* Return the hash code of object.
*
* @param o The object.
* @return the hash code of object
*/
public static int hashCode(final Object o) {
return o != null ? o.hashCode() : 0;
}
/**
* Return the hash code of objects.
*/
public static int hashCodes(Object... values) {
return Arrays.hashCode(values);
}
}