Skip to content

Commit 150bcfd

Browse files
committed
Add Mapper#toUUID(Object)
1 parent 9d7b30b commit 150bcfd

File tree

1 file changed

+18
-5
lines changed
  • src/main/java/xyz/srnyx/javautilities/manipulation

1 file changed

+18
-5
lines changed

src/main/java/xyz/srnyx/javautilities/manipulation/Mapper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import xyz.srnyx.javautilities.MiscUtility;
77

88
import java.util.Optional;
9+
import java.util.UUID;
910

1011

1112
/**
1213
* A utility class for converting {@link Object Objects} to other types
1314
*/
1415
public class Mapper {
1516
/**
16-
* Converts an {@link Object} to the specified {@link Class}
17+
* Casts an {@link Object} to the specified {@link Class}
1718
*
1819
* @param object the {@link Object} to convert
1920
* @param clazz the {@link Class} to convert to
2021
*
21-
* @return the converted {@link Object} or {@code null}
22+
* @return the converted {@link Object} or {@code Optional#empty()}
2223
*
2324
* @param <T> the type of the {@link Class}
2425
*/
@@ -32,7 +33,7 @@ public static <T> Optional<T> to(@Nullable Object object, @NotNull Class<T> claz
3233
*
3334
* @param object the {@link Object} to convert
3435
*
35-
* @return the {@link Integer} or {@code null}
36+
* @return the {@link Integer} or {@link Optional#empty()}
3637
*/
3738
@NotNull
3839
public static Optional<Integer> toInt(@Nullable Object object) {
@@ -44,7 +45,7 @@ public static Optional<Integer> toInt(@Nullable Object object) {
4445
*
4546
* @param object the {@link Object} to convert
4647
*
47-
* @return the {@link Double} or {@code null}
48+
* @return the {@link Double} or {@link Optional#empty()}
4849
*/
4950
@NotNull
5051
public static Optional<Double> toDouble(@Nullable Object object) {
@@ -56,13 +57,25 @@ public static Optional<Double> toDouble(@Nullable Object object) {
5657
*
5758
* @param object the {@link Object} to convert
5859
*
59-
* @return the {@link Long} or {@code null}
60+
* @return the {@link Long} or {@link Optional#empty()}
6061
*/
6162
@NotNull
6263
public static Optional<Long> toLong(@Nullable Object object) {
6364
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Long.parseLong(object.toString()));
6465
}
6566

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+
6679
private Mapper() {
6780
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
6881
}

0 commit comments

Comments
 (0)