File tree Expand file tree Collapse file tree 3 files changed +55
-3
lines changed
main/java/org/springframework/core/convert/support
test/java/org/springframework/core/convert/support Expand file tree Collapse file tree 3 files changed +55
-3
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .core .convert .support ;
18
18
19
19
import java .util .Locale ;
20
+ import java .util .UUID ;
20
21
21
22
import org .springframework .core .convert .ConversionService ;
22
23
import org .springframework .core .convert .converter .ConverterRegistry ;
@@ -80,6 +81,9 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
80
81
81
82
converterRegistry .addConverter (new PropertiesToStringConverter ());
82
83
converterRegistry .addConverter (new StringToPropertiesConverter ());
84
+
85
+ converterRegistry .addConverter (new StringToUUIDConverter ());
86
+ converterRegistry .addConverter (UUID .class , String .class , new ObjectToStringConverter ());
83
87
}
84
88
85
89
private static void addCollectionConverters (ConverterRegistry converterRegistry ) {
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2002-2012 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org .springframework .core .convert .support ;
18
+
19
+ import java .util .UUID ;
20
+
21
+ import org .springframework .core .convert .converter .Converter ;
22
+ import org .springframework .util .StringUtils ;
23
+
24
+ /**
25
+ * Converts from a String to a java.util.UUID by calling {@link UUID#fromString(String)}.
26
+ *
27
+ * @author Phillip Webb
28
+ * @since 3.2
29
+ */
30
+ final class StringToUUIDConverter implements Converter <String , UUID > {
31
+
32
+ public UUID convert (String source ) {
33
+ if (StringUtils .hasLength (source )) {
34
+ return UUID .fromString (source .trim ());
35
+ }
36
+ return null ;
37
+ }
38
+
39
+ }
Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .core .convert .support ;
18
18
19
- import static junit . framework .Assert .assertTrue ;
19
+ import static org . junit .Assert .assertTrue ;
20
20
import static org .junit .Assert .assertEquals ;
21
21
import static org .junit .Assert .assertFalse ;
22
22
import static org .junit .Assert .assertNotNull ;
36
36
import java .util .List ;
37
37
import java .util .Map ;
38
38
import java .util .Set ;
39
+ import java .util .UUID ;
39
40
40
- import org .junit .Ignore ;
41
41
import org .junit .Test ;
42
42
import org .springframework .core .convert .ConversionFailedException ;
43
43
import org .springframework .core .convert .ConverterNotFoundException ;
@@ -382,6 +382,15 @@ public void testIgnoreCopyConstructor() {
382
382
assertSame (value , result );
383
383
}
384
384
385
+ @ Test
386
+ public void testConvertUUID () throws Exception {
387
+ GenericConversionService service = new DefaultConversionService ();
388
+ UUID uuid = UUID .randomUUID ();
389
+ String convertToString = service .convert (uuid , String .class );
390
+ UUID convertToUUID = service .convert (convertToString , UUID .class );
391
+ assertEquals (uuid , convertToUUID );
392
+ }
393
+
385
394
@ Test
386
395
public void testPerformance1 () {
387
396
GenericConversionService conversionService = new DefaultConversionService ();
You can’t perform that action at this time.
0 commit comments