Skip to content

Commit

Permalink
Code clean up for UUIDConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Oct 16, 2023
1 parent 77d8dfd commit ea8d917
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@
* @author Dave Syer
* @author Gary Russell
* @author Christian Tzolov
* @author Artem Bilan
*/
public class UUIDConverter implements Converter<Object, UUID> {

/**
* @deprecated since 6.0.8 as it is not used internally by the UUIDConverter. The internal implementation relies, now,
* on StandardCharsets.UTF_8 instead.
* @deprecated since 6.0.8 as it is not used internally by the UUIDConverter.
* The internal implementation relies on {@link StandardCharsets#UTF_8} instead.
*/
@Deprecated
public static final String DEFAULT_CHARSET = "UTF-8";

private static final Pattern UUID_REGEX = Pattern
.compile("^[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}$");
private static final Pattern UUID_REGEX =
Pattern.compile("^[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}$");

/**
* Convert the input to a UUID using the convenience method {@link #getUUID(Object)}.
Expand Down Expand Up @@ -75,22 +76,25 @@ public static UUID getUUID(Object input) {
if (input instanceof UUID) {
return (UUID) input;
}
if (input instanceof String) {
String inputText = (String) input;
if (input instanceof String inputText) {
if (isValidUuidStringRepresentation(inputText)) {
return UUID.fromString(inputText);
}
else {
return UUID.nameUUIDFromBytes((inputText).getBytes(StandardCharsets.UTF_8));
return fromStringBytes(inputText);
}
}
if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) {
return UUID.nameUUIDFromBytes(input.toString().getBytes(StandardCharsets.UTF_8));
return fromStringBytes(input.toString());
}
byte[] bytes = serialize(input);
return UUID.nameUUIDFromBytes(bytes);
}

private static UUID fromStringBytes(String input) {
return UUID.nameUUIDFromBytes(input.getBytes(StandardCharsets.UTF_8));
}

private static byte[] serialize(Object object) {
if (object == null) {
return null;
Expand All @@ -99,8 +103,8 @@ private static byte[] serialize(Object object) {
try {
new ObjectOutputStream(stream).writeObject(object);
}
catch (IOException e) {
throw new IllegalArgumentException("Could not serialize object of type: " + object.getClass(), e);
catch (IOException ex) {
throw new IllegalArgumentException("Could not serialize object of type: " + object.getClass(), ex);
}
return stream.toByteArray();
}
Expand Down

0 comments on commit ea8d917

Please sign in to comment.