Skip to content

Commit 06f6b45

Browse files
valfirstsnicoll
authored andcommitted
Add support for converting String to Pattern
See gh-24311
1 parent bb14dfa commit 06f6b45

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
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-2017 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -20,6 +20,7 @@
2020
import java.util.Currency;
2121
import java.util.Locale;
2222
import java.util.UUID;
23+
import java.util.regex.Pattern;
2324

2425
import org.springframework.core.convert.ConversionService;
2526
import org.springframework.core.convert.converter.ConverterRegistry;
@@ -166,6 +167,9 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
166167

167168
converterRegistry.addConverter(new StringToUUIDConverter());
168169
converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());
170+
171+
converterRegistry.addConverter(new StringToPatternConverter());
172+
converterRegistry.addConverter(Pattern.class, String.class, new ObjectToStringConverter());
169173
}
170174

171175
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2022 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+
* https://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.regex.Pattern;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* Convert a String to a {@link Pattern}.
25+
*
26+
* @author Valery Yatsynovich
27+
* @since 5.2
28+
*/
29+
class StringToPatternConverter implements Converter<String, Pattern> {
30+
31+
@Override
32+
public Pattern convert(String source) {
33+
return Pattern.compile(source);
34+
}
35+
36+
}

spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Set;
4141
import java.util.TimeZone;
4242
import java.util.UUID;
43+
import java.util.regex.Pattern;
4344
import java.util.stream.Stream;
4445

4546
import org.junit.jupiter.api.Test;
@@ -318,6 +319,18 @@ void uuidToStringAndStringToUuid() {
318319
assertThat(convertToUUID).isEqualTo(uuid);
319320
}
320321

322+
@Test
323+
void stringToPattern() {
324+
String regex = "\\s";
325+
assertThat(conversionService.convert(regex, Pattern.class)).extracting(Pattern::pattern).isEqualTo(regex);
326+
}
327+
328+
@Test
329+
void patternToString() {
330+
String regex = "\\d";
331+
assertThat(conversionService.convert(Pattern.compile(regex), String.class)).isEqualTo(regex);
332+
}
333+
321334
@Test
322335
void numberToNumber() {
323336
assertThat(conversionService.convert(1, Long.class)).isEqualTo(Long.valueOf(1));

0 commit comments

Comments
 (0)