Code clean up for UUIDConverter

This commit is contained in:
Artem Bilan
2023-10-16 14:10:41 -04:00
parent 77d8dfd091
commit ea8d917278

View File

@@ -32,18 +32,19 @@ import org.springframework.util.ClassUtils;
* @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)}.
@@ -75,22 +76,25 @@ public class UUIDConverter implements Converter<Object, UUID> {
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;
@@ -99,8 +103,8 @@ public class UUIDConverter implements Converter<Object, UUID> {
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();
}