Skip to content

Commit 471e4d2

Browse files
committed
Merge pull request #24311 from valfirst
* pr/24311: Polish "Add support for converting String to Pattern" Add support for converting String to Pattern Closes gh-24311
2 parents bb14dfa + a20a748 commit 471e4d2

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

+11-1
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-2023 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,7 +20,9 @@
2020
import java.util.Currency;
2121
import java.util.Locale;
2222
import java.util.UUID;
23+
import java.util.regex.Pattern;
2324

25+
import org.springframework.core.KotlinDetector;
2426
import org.springframework.core.convert.ConversionService;
2527
import org.springframework.core.convert.converter.ConverterRegistry;
2628
import org.springframework.lang.Nullable;
@@ -166,6 +168,14 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
166168

167169
converterRegistry.addConverter(new StringToUUIDConverter());
168170
converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());
171+
172+
converterRegistry.addConverter(new StringToPatternConverter());
173+
converterRegistry.addConverter(Pattern.class, String.class, new ObjectToStringConverter());
174+
175+
if (KotlinDetector.isKotlinPresent()) {
176+
converterRegistry.addConverter(new StringToRegexConverter());
177+
converterRegistry.addConverter(kotlin.text.Regex.class, String.class, new ObjectToStringConverter());
178+
}
169179
}
170180

171181
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2002-2023 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+
* Converts from a String to a {@link java.util.regex.Pattern}.
25+
*
26+
* @author Valery Yatsynovich
27+
* @author Stephane Nicoll
28+
* @since 6.1
29+
*/
30+
final class StringToPatternConverter implements Converter<String, Pattern> {
31+
32+
@Override
33+
public Pattern convert(String source) {
34+
if (source.isEmpty()) {
35+
return null;
36+
}
37+
return Pattern.compile(source);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.core.convert.support
2+
3+
import org.springframework.core.convert.converter.Converter
4+
5+
/**
6+
* Converts from a String to a [Regex].
7+
*
8+
* @author Stephane Nicoll
9+
*/
10+
class StringToRegexConverter : Converter<String, Regex> {
11+
12+
override fun convert(source: String): Regex? {
13+
if (source.isEmpty()) {
14+
return null
15+
}
16+
return source.toRegex()
17+
}
18+
19+
}

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

+19
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,24 @@ void uuidToStringAndStringToUuid() {
318319
assertThat(convertToUUID).isEqualTo(uuid);
319320
}
320321

322+
@Test
323+
void stringToPatternEmptyString() {
324+
assertThat(conversionService.convert("", Pattern.class)).isNull();
325+
}
326+
327+
@Test
328+
void stringToPattern() {
329+
String pattern = "\\s";
330+
assertThat(conversionService.convert(pattern, Pattern.class))
331+
.isInstanceOfSatisfying(Pattern.class, regex -> assertThat(regex.pattern()).isEqualTo(pattern));
332+
}
333+
334+
@Test
335+
void patternToString() {
336+
String regex = "\\d";
337+
assertThat(conversionService.convert(Pattern.compile(regex), String.class)).isEqualTo(regex);
338+
}
339+
321340
@Test
322341
void numberToNumber() {
323342
assertThat(conversionService.convert(1, Long.class)).isEqualTo(Long.valueOf(1));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.core.convert.support
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
/**
7+
* Tests for Kotlin support in [DefaultConversionService].
8+
*
9+
* @author Stephane Nicoll
10+
*/
11+
class DefaultConversionServiceKotlinTests {
12+
13+
private val conversionService = DefaultConversionService()
14+
15+
@Test
16+
fun stringToRegexEmptyString() {
17+
assertThat(conversionService.convert("", Regex::class.java)).isNull();
18+
}
19+
20+
@Test
21+
fun stringToRegex() {
22+
val pattern = "\\w+"
23+
assertThat(conversionService.convert(pattern, Regex::class.java)).isInstanceOfSatisfying(
24+
Regex::class.java
25+
) { regex -> assertThat(regex.pattern).isEqualTo(pattern) }
26+
}
27+
28+
@Test
29+
fun regexToString() {
30+
val pattern = "\\w+"
31+
assertThat(conversionService.convert(pattern.toRegex(), String::class.java)).isEqualTo(pattern)
32+
}
33+
34+
}

0 commit comments

Comments
 (0)