6
6
import xyz .srnyx .javautilities .MiscUtility ;
7
7
8
8
import java .util .Optional ;
9
+ import java .util .UUID ;
9
10
10
11
11
12
/**
12
13
* A utility class for converting {@link Object Objects} to other types
13
14
*/
14
15
public class Mapper {
15
16
/**
16
- * Converts an {@link Object} to the specified {@link Class}
17
+ * Casts an {@link Object} to the specified {@link Class}
17
18
*
18
19
* @param object the {@link Object} to convert
19
20
* @param clazz the {@link Class} to convert to
20
21
*
21
- * @return the converted {@link Object} or {@code null }
22
+ * @return the converted {@link Object} or {@code Optional#empty() }
22
23
*
23
24
* @param <T> the type of the {@link Class}
24
25
*/
@@ -32,7 +33,7 @@ public static <T> Optional<T> to(@Nullable Object object, @NotNull Class<T> claz
32
33
*
33
34
* @param object the {@link Object} to convert
34
35
*
35
- * @return the {@link Integer} or {@code null }
36
+ * @return the {@link Integer} or {@link Optional#empty() }
36
37
*/
37
38
@ NotNull
38
39
public static Optional <Integer > toInt (@ Nullable Object object ) {
@@ -44,7 +45,7 @@ public static Optional<Integer> toInt(@Nullable Object object) {
44
45
*
45
46
* @param object the {@link Object} to convert
46
47
*
47
- * @return the {@link Double} or {@code null }
48
+ * @return the {@link Double} or {@link Optional#empty() }
48
49
*/
49
50
@ NotNull
50
51
public static Optional <Double > toDouble (@ Nullable Object object ) {
@@ -56,13 +57,25 @@ public static Optional<Double> toDouble(@Nullable Object object) {
56
57
*
57
58
* @param object the {@link Object} to convert
58
59
*
59
- * @return the {@link Long} or {@code null }
60
+ * @return the {@link Long} or {@link Optional#empty() }
60
61
*/
61
62
@ NotNull
62
63
public static Optional <Long > toLong (@ Nullable Object object ) {
63
64
return object == null ? Optional .empty () : MiscUtility .handleException (() -> Long .parseLong (object .toString ()));
64
65
}
65
66
67
+ /**
68
+ * Converts an {@link Object} to a {@link UUID}
69
+ *
70
+ * @param object the {@link Object} to convert
71
+ *
72
+ * @return the {@link UUID} or {@link Optional#empty()}
73
+ */
74
+ @ NotNull
75
+ public static Optional <UUID > toUUID (@ Nullable Object object ) {
76
+ return object == null ? Optional .empty () : MiscUtility .handleException (() -> UUID .fromString (object .toString ()));
77
+ }
78
+
66
79
private Mapper () {
67
80
throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated" );
68
81
}
0 commit comments