Add support for ChronoUnit.WEEKS when using PeriodUnit

Fixes gh-22225
pull/22996/head
Andy Wilkinson 4 years ago
parent d1f074858e
commit 28128a9577

@ -212,13 +212,18 @@ public enum PeriodStyle {
throw new IllegalArgumentException("'" + value + "' is not a valid period");
}
enum Unit {
private enum Unit {
/**
* Days, represented by suffix {@code d}.
*/
DAYS(ChronoUnit.DAYS, "d", Period::getDays, Period::ofDays),
/**
* Weeks, represented by suffix {@code w}.
*/
WEEKS(ChronoUnit.WEEKS, "w", null, Period::ofWeeks),
/**
* Months, represented by suffix {@code m}.
*/
@ -253,15 +258,16 @@ public enum PeriodStyle {
return intValue(value) + this.suffix;
}
public boolean isZero(Period value) {
private boolean isZero(Period value) {
return intValue(value) == 0;
}
public int intValue(Period value) {
private int intValue(Period value) {
Assert.notNull(this.intValue, () -> "intValue cannot be extracted from " + this.name());
return this.intValue.apply(value);
}
public static Unit fromChronoUnit(ChronoUnit chronoUnit) {
private static Unit fromChronoUnit(ChronoUnit chronoUnit) {
if (chronoUnit == null) {
return Unit.DAYS;
}

@ -48,9 +48,14 @@ class NumberToPeriodConverterTests {
@ConversionServiceTest
void convertWhenSimpleWithoutSuffixButWithAnnotationShouldReturnPeriod(ConversionService conversionService) {
assertThat(convert(conversionService, 10, ChronoUnit.DAYS)).isEqualTo(Period.ofDays(10));
assertThat(convert(conversionService, -10, ChronoUnit.DAYS)).isEqualTo(Period.ofDays(-10));
assertThat(convert(conversionService, 10, ChronoUnit.WEEKS)).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, -10, ChronoUnit.WEEKS)).isEqualTo(Period.ofWeeks(-10));
assertThat(convert(conversionService, 10, ChronoUnit.MONTHS)).isEqualTo(Period.ofMonths(10));
assertThat(convert(conversionService, +10, ChronoUnit.MONTHS)).isEqualTo(Period.ofMonths(10));
assertThat(convert(conversionService, -10, ChronoUnit.MONTHS)).isEqualTo(Period.ofMonths(-10));
assertThat(convert(conversionService, 10, ChronoUnit.YEARS)).isEqualTo(Period.ofYears(10));
assertThat(convert(conversionService, -10, ChronoUnit.YEARS)).isEqualTo(Period.ofYears(-10));
}
private Period convert(ConversionService conversionService, Integer source) {

@ -71,6 +71,13 @@ class PeriodToStringConverterTests {
assertThat(converted).isEqualTo("1y3d");
}
@ConversionServiceTest
void convertWithWeekUnitShouldConvertToStringInDays(ConversionService conversionService) {
String converted = (String) conversionService.convert(Period.ofWeeks(53),
MockPeriodTypeDescriptor.get(null, PeriodStyle.SIMPLE), TypeDescriptor.valueOf(String.class));
assertThat(converted).isEqualTo("371d");
}
static Stream<? extends Arguments> conversionServices() throws Exception {
return ConversionServiceArguments.with(new PeriodToStringConverter());
}

@ -55,6 +55,14 @@ class StringToPeriodConverterTests {
assertThat(convert(conversionService, "-10D")).isEqualTo(Period.ofDays(-10));
}
@ConversionServiceTest
void convertWhenSimpleWeeksShouldReturnPeriod(ConversionService conversionService) {
assertThat(convert(conversionService, "10w")).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, "10W")).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, "+10w")).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, "-10W")).isEqualTo(Period.ofWeeks(-10));
}
@ConversionServiceTest
void convertWhenSimpleMonthsShouldReturnPeriod(ConversionService conversionService) {
assertThat(convert(conversionService, "10m")).isEqualTo(Period.ofMonths(10));
@ -80,9 +88,18 @@ class StringToPeriodConverterTests {
@ConversionServiceTest
void convertWhenSimpleWithoutSuffixButWithAnnotationShouldReturnPeriod(ConversionService conversionService) {
assertThat(convert(conversionService, "10", ChronoUnit.DAYS, null)).isEqualTo(Period.ofDays(10));
assertThat(convert(conversionService, "+10", ChronoUnit.DAYS, null)).isEqualTo(Period.ofDays(10));
assertThat(convert(conversionService, "-10", ChronoUnit.DAYS, null)).isEqualTo(Period.ofDays(-10));
assertThat(convert(conversionService, "10", ChronoUnit.WEEKS, null)).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, "+10", ChronoUnit.WEEKS, null)).isEqualTo(Period.ofWeeks(10));
assertThat(convert(conversionService, "-10", ChronoUnit.WEEKS, null)).isEqualTo(Period.ofWeeks(-10));
assertThat(convert(conversionService, "10", ChronoUnit.MONTHS, null)).isEqualTo(Period.ofMonths(10));
assertThat(convert(conversionService, "+10", ChronoUnit.MONTHS, null)).isEqualTo(Period.ofMonths(10));
assertThat(convert(conversionService, "-10", ChronoUnit.MONTHS, null)).isEqualTo(Period.ofMonths(-10));
assertThat(convert(conversionService, "10", ChronoUnit.YEARS, null)).isEqualTo(Period.ofYears(10));
assertThat(convert(conversionService, "+10", ChronoUnit.YEARS, null)).isEqualTo(Period.ofYears(10));
assertThat(convert(conversionService, "-10", ChronoUnit.YEARS, null)).isEqualTo(Period.ofYears(-10));
}
private Period convert(ConversionService conversionService, String source) {

Loading…
Cancel
Save