From ea8d917278f3e1f5c94e84e58a08a2d2da18ff64 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 16 Oct 2023 14:10:41 -0400 Subject: [PATCH] Code clean up for `UUIDConverter` --- .../integration/util/UUIDConverter.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java index 14ce2872fa..c6cabca920 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java @@ -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 { /** - * @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 { 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 { 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(); }