-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodefValidator.java
More file actions
32 lines (26 loc) · 890 Bytes
/
CodefValidator.java
File metadata and controls
32 lines (26 loc) · 890 Bytes
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
package io.codef.api;
import io.codef.api.error.CodefError;
import io.codef.api.error.CodefException;
import java.util.Optional;
import java.util.UUID;
public class CodefValidator {
private CodefValidator() {
}
public static <T> T requireNonNullElseThrow(
T object,
CodefError codefError
) {
return Optional.ofNullable(object)
.orElseThrow(() -> CodefException.from(codefError));
}
public static void requireValidUUIDPattern(
String uuid,
CodefError codefError
) {
final String UUID_REGULAR_EXPRESSION = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
Optional.ofNullable(uuid)
.filter(uuids -> uuids.matches(UUID_REGULAR_EXPRESSION))
.map(UUID::fromString)
.orElseThrow(() -> CodefException.from(codefError));
}
}