-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathObjectUtils.java
More file actions
155 lines (137 loc) · 3.83 KB
/
ObjectUtils.java
File metadata and controls
155 lines (137 loc) · 3.83 KB
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
package javasabr.rlib.common.util;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.Supplier;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
/**
* The class with utility methods.
*
* @author JavaSaBr
*/
@NullMarked
public final class ObjectUtils {
/**
* @param <T> the object's type.
* @param obj the object to check on not null.
* @param message the message for exception if the object is null.
* @return the passed object if it is not null.
* @see Objects#requireNonNull(Object, String)
*/
public static <T> T notNull(@Nullable T obj, String message) {
return Objects.requireNonNull(obj, message);
}
/**
* @param <T> the object's type.
* @param obj the object to check on not null.
* @return the passed object if it is not null.
* @see Objects#requireNonNull(Object)
*/
public static <T> T notNull(@Nullable T obj) {
return Objects.requireNonNull(obj);
}
/**
* Check the object to be not null. If the object is null this method throws an exception from the supplier.
*
* @param <T> the object's type.
* @param obj the checked object.
* @param supplier the exception factory.
* @return the object.
* @since 9.0.2
*/
public static <T> T notNull(@Nullable T obj, Supplier<? extends RuntimeException> supplier) {
if (obj == null) {
throw supplier.get();
}
return obj;
}
/**
* Check the object to be not null. If the object is null this method throws an exception from the factory.
*
* @param <T> the object's type.
* @param <F> the argument's type.
* @param obj the checked object.
* @param arg the argument for the exception factory.
* @param factory the exception factory.
* @return the object.
* @since 9.0.2
*/
public static <T, F> T notNull(
@Nullable T obj,
F arg,
Function<F, ? extends RuntimeException> factory) {
if (obj == null) {
throw factory.apply(arg);
}
return obj;
}
/**
* Check the object to be not null. If the object is null this method throws an exception from the factory.
*
* @param <T> the object's type.
* @param obj the checked object.
* @param arg the argument for the exception factory.
* @param factory the exception factory.
* @return the object.
* @since 9.0.3
*/
public static <T> T notNull(
@Nullable T obj,
long arg,
LongFunction<? extends RuntimeException> factory) {
if (obj == null) {
throw factory.apply(arg);
}
return obj;
}
/**
* Returns the another object if the first object is null.
*
* @param obj the object.
* @param another the another object.
* @param <T> the object's type.
* @return the another object if the first object is null.
*/
public static <T> T ifNull(@Nullable T obj, T another) {
return obj == null ? another : obj;
}
/**
* Returns a new object if the first object is null.
*
* @param obj the object.
* @param factory the factory.
* @param <T> the object's type.
* @return a new object if the first object is null.
*/
public static <T> T ifNull(@Nullable T obj, Supplier<T> factory) {
return obj == null ? factory.get() : obj;
}
/**
* Gets hash of the boolean value.
*
* @param value the boolean value.
* @return the hash.
*/
public static int hash(boolean value) {
return value ? 1231 : 1237;
}
/**
* Gets hash of the long value.
*
* @param value the long value.
* @return the hash.
*/
public static int hash(long value) {
return (int) (value ^ value >>> 32);
}
/**
* Gets hash of the object.
*
* @param object the object.
* @return the hash.
*/
public static int hash(@Nullable Object object) {
return object == null ? 0 : object.hashCode();
}
}