32
32
* @author Dave Syer
33
33
* @author Gary Russell
34
34
* @author Christian Tzolov
35
+ * @author Artem Bilan
35
36
*/
36
37
public class UUIDConverter implements Converter <Object , UUID > {
37
38
38
39
/**
39
- * @deprecated since 6.0.8 as it is not used internally by the UUIDConverter. The internal implementation relies, now,
40
- * on StandardCharsets. UTF_8 instead.
40
+ * @deprecated since 6.0.8 as it is not used internally by the UUIDConverter.
41
+ * The internal implementation relies on {@link StandardCharsets# UTF_8} instead.
41
42
*/
42
43
@ Deprecated
43
44
public static final String DEFAULT_CHARSET = "UTF-8" ;
44
45
45
- private static final Pattern UUID_REGEX = Pattern
46
- .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}$" );
46
+ private static final Pattern UUID_REGEX =
47
+ 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}$" );
47
48
48
49
/**
49
50
* Convert the input to a UUID using the convenience method {@link #getUUID(Object)}.
@@ -75,22 +76,25 @@ public static UUID getUUID(Object input) {
75
76
if (input instanceof UUID ) {
76
77
return (UUID ) input ;
77
78
}
78
- if (input instanceof String ) {
79
- String inputText = (String ) input ;
79
+ if (input instanceof String inputText ) {
80
80
if (isValidUuidStringRepresentation (inputText )) {
81
81
return UUID .fromString (inputText );
82
82
}
83
83
else {
84
- return UUID . nameUUIDFromBytes (( inputText ). getBytes ( StandardCharsets . UTF_8 ) );
84
+ return fromStringBytes ( inputText );
85
85
}
86
86
}
87
87
if (ClassUtils .isPrimitiveOrWrapper (input .getClass ())) {
88
- return UUID . nameUUIDFromBytes (input .toString (). getBytes ( StandardCharsets . UTF_8 ));
88
+ return fromStringBytes (input .toString ());
89
89
}
90
90
byte [] bytes = serialize (input );
91
91
return UUID .nameUUIDFromBytes (bytes );
92
92
}
93
93
94
+ private static UUID fromStringBytes (String input ) {
95
+ return UUID .nameUUIDFromBytes (input .getBytes (StandardCharsets .UTF_8 ));
96
+ }
97
+
94
98
private static byte [] serialize (Object object ) {
95
99
if (object == null ) {
96
100
return null ;
@@ -99,8 +103,8 @@ private static byte[] serialize(Object object) {
99
103
try {
100
104
new ObjectOutputStream (stream ).writeObject (object );
101
105
}
102
- catch (IOException e ) {
103
- throw new IllegalArgumentException ("Could not serialize object of type: " + object .getClass (), e );
106
+ catch (IOException ex ) {
107
+ throw new IllegalArgumentException ("Could not serialize object of type: " + object .getClass (), ex );
104
108
}
105
109
return stream .toByteArray ();
106
110
}
0 commit comments