From 6adb49b7a928b9f611693ddcde601215583381cf Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 12 Oct 2012 14:24:07 -0700 Subject: [PATCH] Add StringToUUIDConverter Conversion to and from UUID objects is now supported by the DefaultConversionService. Issue: SPR-9765 --- .../support/DefaultConversionService.java | 6 ++- .../support/StringToUUIDConverter.java | 39 +++++++++++++++++++ .../GenericConversionServiceTests.java | 13 ++++++- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 spring-core/src/main/java/org/springframework/core/convert/support/StringToUUIDConverter.java diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index e4fffd37c3..fe510e9a53 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.core.convert.support; import java.util.Locale; +import java.util.UUID; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.ConverterRegistry; @@ -80,6 +81,9 @@ public class DefaultConversionService extends GenericConversionService { converterRegistry.addConverter(new PropertiesToStringConverter()); converterRegistry.addConverter(new StringToPropertiesConverter()); + + converterRegistry.addConverter(new StringToUUIDConverter()); + converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter()); } private static void addCollectionConverters(ConverterRegistry converterRegistry) { diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToUUIDConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToUUIDConverter.java new file mode 100644 index 0000000000..6478d47845 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToUUIDConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.convert.support; + +import java.util.UUID; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.StringUtils; + +/** + * Converts from a String to a java.util.UUID by calling {@link UUID#fromString(String)}. + * + * @author Phillip Webb + * @since 3.2 + */ +final class StringToUUIDConverter implements Converter { + + public UUID convert(String source) { + if(StringUtils.hasLength(source)) { + return UUID.fromString(source.trim()); + } + return null; + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 6c4769a27c..27f15b76d0 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -16,7 +16,7 @@ package org.springframework.core.convert.support; -import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -36,8 +36,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; -import org.junit.Ignore; import org.junit.Test; import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; @@ -382,6 +382,15 @@ public class GenericConversionServiceTests { assertSame(value, result); } + @Test + public void testConvertUUID() throws Exception { + GenericConversionService service = new DefaultConversionService(); + UUID uuid = UUID.randomUUID(); + String convertToString = service.convert(uuid, String.class); + UUID convertToUUID = service.convert(convertToString, UUID.class); + assertEquals(uuid, convertToUUID); + } + @Test public void testPerformance1() { GenericConversionService conversionService = new DefaultConversionService();