Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Period converter support #21136

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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.
Expand Down Expand Up @@ -130,6 +130,19 @@ private static class FieldCollector implements TreeVisitor {
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<>();
Expand Down Expand Up @@ -194,6 +207,10 @@ private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {
if (dataSizeValue != null) {
return dataSizeValue;
}
Object periodValue = getFactoryValue(expression, factoryValue, PERIOD_OF, PERIOD_SUFFIX);
if (periodValue != null) {
return periodValue;
}
return factoryValue;
}

Expand Down
@@ -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.
Expand Down Expand Up @@ -100,6 +100,11 @@ void getFieldValues() throws Exception {
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" })
Expand Down
@@ -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.
Expand All @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -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);

}
@@ -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.
Expand Down Expand Up @@ -110,8 +110,11 @@ public static void configure(FormatterRegistry registry) {
public static void addApplicationConverters(ConverterRegistry registry) {
addDelimitedStringConverters(registry);
registry.addConverter(new StringToDurationConverter());
registry.addConverter(new StringToPeriodConverter());
registry.addConverter(new DurationToStringConverter());
registry.addConverter(new PeriodToStringConverter());
registry.addConverter(new NumberToDurationConverter());
registry.addConverter(new NumberToPeriodConverter());
registry.addConverter(new DurationToNumberConverter());
registry.addConverter(new StringToDataSizeConverter());
registry.addConverter(new NumberToDataSizeConverter());
Expand Down
@@ -0,0 +1,51 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.convert;

import java.time.Period;
import java.util.Collections;
import java.util.Set;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;

/**
* {@link Converter} to convert from a {@link Number} to a {@link Period}. Supports
* {@link Period#parse(CharSequence)} as well a more readable {@code 10m} form.
*
* @author Eddú Meléndez
* @author Edson Chávez
* @see PeriodFormat
* @see PeriodUnit
*/
final class NumberToPeriodConverter implements GenericConverter {

private final StringToPeriodConverter delegate = new StringToPeriodConverter();

@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Number.class, Period.class));
}

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.delegate.convert((source != null) ? source.toString() : null, TypeDescriptor.valueOf(String.class),
targetType);
}

}
@@ -0,0 +1,45 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.convert;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Period;

/**
* Annotation that can be used to indicate the format to use when converting a
* {@link Period}.
*
* @author Eddú Meléndez
* @author Edson Chávez
* @since 2.3.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PeriodFormat {

/**
* The {@link Period} format style.
* @return the period format style.
*/
PeriodStyle value();

}