Skip to content

Commit 44f3f6f

Browse files
committed
Add StringToPatternConverter
1 parent 5715432 commit 44f3f6f

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
@@ -41,6 +41,7 @@
4141
import java.util.Set;
4242
import java.util.TimeZone;
4343
import java.util.UUID;
44+
import java.util.regex.Pattern;
4445
import java.util.stream.Stream;
4546

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

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

0 commit comments

Comments
 (0)