File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
src/main/java/xyz/srnyx/javautilities/manipulation Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,21 @@ public static Optional<UUID> toUUID(@Nullable Object object) {
76
76
return object == null ? Optional .empty () : MiscUtility .handleException (() -> UUID .fromString (object .toString ()), IllegalArgumentException .class );
77
77
}
78
78
79
+ /**
80
+ * Converts a {@link String} to an {@link Enum} value of the specified class
81
+ *
82
+ * @param name the name of the {@link Enum} value to convert
83
+ * @param enumClass the {@link Class} of the {@link Enum} to convert to
84
+ *
85
+ * @return an {@link Optional} containing the {@link Enum} value if it exists, otherwise {@link Optional#empty()}
86
+ *
87
+ * @param <T> the type of the {@link Enum}
88
+ */
89
+ @ NotNull
90
+ public static <T extends Enum <T >> Optional <T > toEnum (@ Nullable String name , @ NotNull Class <T > enumClass ) {
91
+ return name == null || name .isEmpty () ? Optional .empty () : MiscUtility .handleException (() -> Enum .valueOf (enumClass , name .toUpperCase ()), IllegalArgumentException .class );
92
+ }
93
+
79
94
private Mapper () {
80
95
throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated" );
81
96
}
You can’t perform that action at this time.
0 commit comments