Skip to content

Commit 6adb49b

Browse files
committed
Add StringToUUIDConverter
Conversion to and from UUID objects is now supported by the DefaultConversionService. Issue: SPR-9765
1 parent c8c0e82 commit 6adb49b

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.convert.support;
1818

1919
import java.util.Locale;
20+
import java.util.UUID;
2021

2122
import org.springframework.core.convert.ConversionService;
2223
import org.springframework.core.convert.converter.ConverterRegistry;
@@ -80,6 +81,9 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
8081

8182
converterRegistry.addConverter(new PropertiesToStringConverter());
8283
converterRegistry.addConverter(new StringToPropertiesConverter());
84+
85+
converterRegistry.addConverter(new StringToUUIDConverter());
86+
converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());
8387
}
8488

8589
private static void addCollectionConverters(ConverterRegistry converterRegistry) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.core.convert.support;
1818

19-
import static junit.framework.Assert.assertTrue;
19+
import static org.junit.Assert.assertTrue;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
@@ -36,8 +36,8 @@
3636
import java.util.List;
3737
import java.util.Map;
3838
import java.util.Set;
39+
import java.util.UUID;
3940

40-
import org.junit.Ignore;
4141
import org.junit.Test;
4242
import org.springframework.core.convert.ConversionFailedException;
4343
import org.springframework.core.convert.ConverterNotFoundException;
@@ -382,6 +382,15 @@ public void testIgnoreCopyConstructor() {
382382
assertSame(value, result);
383383
}
384384

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+
385394
@Test
386395
public void testPerformance1() {
387396
GenericConversionService conversionService = new DefaultConversionService();

0 commit comments

Comments
 (0)