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
This commit is contained in:
Grubhart
2020-04-25 11:40:40 -05:00
committed by Phillip Webb
parent a8f56b57cb
commit dc4d71f91e
15 changed files with 1072 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -130,6 +130,19 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
DATA_SIZE_SUFFIX = Collections.unmodifiableMap(values);
}
private static final String PERIOD_OF = "Period.of";
private static final Map<String, String> PERIOD_SUFFIX;
static {
Map<String, String> values = new HashMap<>();
values.put("Days", "d");
values.put("Weeks", "w");
values.put("Months", "m");
values.put("Years", "y");
PERIOD_SUFFIX = Collections.unmodifiableMap(values);
}
private final Map<String, Object> fieldValues = new HashMap<>();
private final Map<String, Object> staticFinals = new HashMap<>();
@@ -194,6 +207,10 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
if (dataSizeValue != null) {
return dataSizeValue;
}
Object periodValue = getFactoryValue(expression, factoryValue, PERIOD_OF, PERIOD_SUFFIX);
if (periodValue != null) {
return periodValue;
}
return factoryValue;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -100,6 +100,11 @@ public abstract class AbstractFieldValuesProcessorTests {
assertThat(values.get("dataSizeMegabytes")).isEqualTo("20MB");
assertThat(values.get("dataSizeGigabytes")).isEqualTo("30GB");
assertThat(values.get("dataSizeTerabytes")).isEqualTo("40TB");
assertThat(values.get("periodNone")).isNull();
assertThat(values.get("periodDays")).isEqualTo("3d");
assertThat(values.get("periodWeeks")).isEqualTo("2w");
assertThat(values.get("periodMonths")).isEqualTo("10m");
assertThat(values.get("periodYears")).isEqualTo("15y");
}
@SupportedAnnotationTypes({ "org.springframework.boot.configurationsample.ConfigurationProperties" })

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.boot.configurationsample.fieldvalues;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Period;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.util.MimeType;
@@ -136,4 +137,14 @@ public class FieldValues {
private DataSize dataSizeTerabytes = DataSize.ofTerabytes(40);
private Period periodNone;
private Period periodDays = Period.ofDays(3);
private Period periodWeeks = Period.ofWeeks(2);
private Period periodMonths = Period.ofMonths(10);
private Period periodYears = Period.ofYears(15);
}