Skip to content

Commit dc4d71f

Browse files
Grubhartphilwebb
authored andcommitted
Add Period converter support
Add converter support for `javax.time.Period` including: String -> Period Number -> Period Period -> String Period to Number conversion is not supported since `Period` has no ability to deduce the number of calendar days in the period. See gh-21136
1 parent a8f56b5 commit dc4d71f

File tree

15 files changed

+1072
-4
lines changed

15 files changed

+1072
-4
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -130,6 +130,19 @@ private static class FieldCollector implements TreeVisitor {
130130
DATA_SIZE_SUFFIX = Collections.unmodifiableMap(values);
131131
}
132132

133+
private static final String PERIOD_OF = "Period.of";
134+
135+
private static final Map<String, String> PERIOD_SUFFIX;
136+
137+
static {
138+
Map<String, String> values = new HashMap<>();
139+
values.put("Days", "d");
140+
values.put("Weeks", "w");
141+
values.put("Months", "m");
142+
values.put("Years", "y");
143+
PERIOD_SUFFIX = Collections.unmodifiableMap(values);
144+
}
145+
133146
private final Map<String, Object> fieldValues = new HashMap<>();
134147

135148
private final Map<String, Object> staticFinals = new HashMap<>();
@@ -194,6 +207,10 @@ private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {
194207
if (dataSizeValue != null) {
195208
return dataSizeValue;
196209
}
210+
Object periodValue = getFactoryValue(expression, factoryValue, PERIOD_OF, PERIOD_SUFFIX);
211+
if (periodValue != null) {
212+
return periodValue;
213+
}
197214
return factoryValue;
198215
}
199216

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -100,6 +100,11 @@ void getFieldValues() throws Exception {
100100
assertThat(values.get("dataSizeMegabytes")).isEqualTo("20MB");
101101
assertThat(values.get("dataSizeGigabytes")).isEqualTo("30GB");
102102
assertThat(values.get("dataSizeTerabytes")).isEqualTo("40TB");
103+
assertThat(values.get("periodNone")).isNull();
104+
assertThat(values.get("periodDays")).isEqualTo("3d");
105+
assertThat(values.get("periodWeeks")).isEqualTo("2w");
106+
assertThat(values.get("periodMonths")).isEqualTo("10m");
107+
assertThat(values.get("periodYears")).isEqualTo("15y");
103108
}
104109

105110
@SupportedAnnotationTypes({ "org.springframework.boot.configurationsample.ConfigurationProperties" })

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -19,6 +19,7 @@
1919
import java.nio.charset.Charset;
2020
import java.nio.charset.StandardCharsets;
2121
import java.time.Duration;
22+
import java.time.Period;
2223

2324
import org.springframework.boot.configurationsample.ConfigurationProperties;
2425
import org.springframework.util.MimeType;
@@ -136,4 +137,14 @@ public class FieldValues {
136137

137138
private DataSize dataSizeTerabytes = DataSize.ofTerabytes(40);
138139

140+
private Period periodNone;
141+
142+
private Period periodDays = Period.ofDays(3);
143+
144+
private Period periodWeeks = Period.ofWeeks(2);
145+
146+
private Period periodMonths = Period.ofMonths(10);
147+
148+
private Period periodYears = Period.ofYears(15);
149+
139150
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -110,8 +110,11 @@ public static void configure(FormatterRegistry registry) {
110110
public static void addApplicationConverters(ConverterRegistry registry) {
111111
addDelimitedStringConverters(registry);
112112
registry.addConverter(new StringToDurationConverter());
113+
registry.addConverter(new StringToPeriodConverter());
113114
registry.addConverter(new DurationToStringConverter());
115+
registry.addConverter(new PeriodToStringConverter());
114116
registry.addConverter(new NumberToDurationConverter());
117+
registry.addConverter(new NumberToPeriodConverter());
115118
registry.addConverter(new DurationToNumberConverter());
116119
registry.addConverter(new StringToDataSizeConverter());
117120
registry.addConverter(new NumberToDataSizeConverter());
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.convert;
18+
19+
import java.time.Period;
20+
import java.util.Collections;
21+
import java.util.Set;
22+
23+
import org.springframework.core.convert.TypeDescriptor;
24+
import org.springframework.core.convert.converter.Converter;
25+
import org.springframework.core.convert.converter.GenericConverter;
26+
27+
/**
28+
* {@link Converter} to convert from a {@link Number} to a {@link Period}. Supports
29+
* {@link Period#parse(CharSequence)} as well a more readable {@code 10m} form.
30+
*
31+
* @author Eddú Meléndez
32+
* @author Edson Chávez
33+
* @see PeriodFormat
34+
* @see PeriodUnit
35+
*/
36+
final class NumberToPeriodConverter implements GenericConverter {
37+
38+
private final StringToPeriodConverter delegate = new StringToPeriodConverter();
39+
40+
@Override
41+
public Set<ConvertiblePair> getConvertibleTypes() {
42+
return Collections.singleton(new ConvertiblePair(Number.class, Period.class));
43+
}
44+
45+
@Override
46+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
47+
return this.delegate.convert((source != null) ? source.toString() : null, TypeDescriptor.valueOf(String.class),
48+
targetType);
49+
}
50+
51+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.convert;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
import java.time.Period;
25+
26+
/**
27+
* Annotation that can be used to indicate the format to use when converting a
28+
* {@link Period}.
29+
*
30+
* @author Eddú Meléndez
31+
* @author Edson Chávez
32+
* @since 2.3.0
33+
*/
34+
@Target(ElementType.FIELD)
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Documented
37+
public @interface PeriodFormat {
38+
39+
/**
40+
* The {@link Period} format style.
41+
* @return the period format style.
42+
*/
43+
PeriodStyle value();
44+
45+
}

0 commit comments

Comments
 (0)