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 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.
1717package org .springframework .core .convert .support ;
1818
1919import java .util .Locale ;
20+ import java .util .UUID ;
2021
2122import org .springframework .core .convert .ConversionService ;
2223import 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 ) {
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 1616
1717package org .springframework .core .convert .support ;
1818
19- import static junit . framework .Assert .assertTrue ;
19+ import static org . junit .Assert .assertTrue ;
2020import static org .junit .Assert .assertEquals ;
2121import static org .junit .Assert .assertFalse ;
2222import static org .junit .Assert .assertNotNull ;
3636import java .util .List ;
3737import java .util .Map ;
3838import java .util .Set ;
39+ import java .util .UUID ;
3940
40- import org .junit .Ignore ;
4141import org .junit .Test ;
4242import org .springframework .core .convert .ConversionFailedException ;
4343import 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 ();
You can’t perform that action at this time.
0 commit comments