Skip to content

Commit a20a748

Browse files
committed
Polish "Add support for converting String to Pattern"
See gh-24311
1 parent 06f6b45 commit a20a748

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -22,6 +22,7 @@
2222
import java.util.UUID;
2323
import java.util.regex.Pattern;
2424

25+
import org.springframework.core.KotlinDetector;
2526
import org.springframework.core.convert.ConversionService;
2627
import org.springframework.core.convert.converter.ConverterRegistry;
2728
import org.springframework.lang.Nullable;
@@ -170,6 +171,11 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
170171

171172
converterRegistry.addConverter(new StringToPatternConverter());
172173
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+
}
173179
}
174180

175181
}

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -21,15 +21,19 @@
2121
import org.springframework.core.convert.converter.Converter;
2222

2323
/**
24-
* Convert a String to a {@link Pattern}.
24+
* Converts from a String to a {@link java.util.regex.Pattern}.
2525
*
2626
* @author Valery Yatsynovich
27-
* @since 5.2
27+
* @author Stephane Nicoll
28+
* @since 6.1
2829
*/
29-
class StringToPatternConverter implements Converter<String, Pattern> {
30+
final class StringToPatternConverter implements Converter<String, Pattern> {
3031

3132
@Override
3233
public Pattern convert(String source) {
34+
if (source.isEmpty()) {
35+
return null;
36+
}
3337
return Pattern.compile(source);
3438
}
3539

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,16 @@ void uuidToStringAndStringToUuid() {
319319
assertThat(convertToUUID).isEqualTo(uuid);
320320
}
321321

322+
@Test
323+
void stringToPatternEmptyString() {
324+
assertThat(conversionService.convert("", Pattern.class)).isNull();
325+
}
326+
322327
@Test
323328
void stringToPattern() {
324-
String regex = "\\s";
325-
assertThat(conversionService.convert(regex, Pattern.class)).extracting(Pattern::pattern).isEqualTo(regex);
329+
String pattern = "\\s";
330+
assertThat(conversionService.convert(pattern, Pattern.class))
331+
.isInstanceOfSatisfying(Pattern.class, regex -> assertThat(regex.pattern()).isEqualTo(pattern));
326332
}
327333

328334
@Test
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)