Add StringToUUIDConverter
Conversion to and from UUID objects is now supported by the DefaultConversionService. Issue: SPR-9765
This commit is contained in:
parent
c8c0e827b4
commit
6adb49b7a9
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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<String, UUID> {
|
||||
|
||||
public UUID convert(String source) {
|
||||
if(StringUtils.hasLength(source)) {
|
||||
return UUID.fromString(source.trim());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue