Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
28128a95
Commit
28128a95
authored
Jul 06, 2020
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for ChronoUnit.WEEKS when using PeriodUnit
Fixes gh-22225
parent
d1f07485
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
5 deletions
+40
-5
PeriodStyle.java
...in/java/org/springframework/boot/convert/PeriodStyle.java
+10
-4
NumberToPeriodConverterTests.java
...gframework/boot/convert/NumberToPeriodConverterTests.java
+6
-1
PeriodToStringConverterTests.java
...gframework/boot/convert/PeriodToStringConverterTests.java
+7
-0
StringToPeriodConverterTests.java
...gframework/boot/convert/StringToPeriodConverterTests.java
+17
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/PeriodStyle.java
View file @
28128a95
...
...
@@ -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
;
}
p
ublic
boolean
isZero
(
Period
value
)
{
p
rivate
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
);
}
p
ublic
static
Unit
fromChronoUnit
(
ChronoUnit
chronoUnit
)
{
p
rivate
static
Unit
fromChronoUnit
(
ChronoUnit
chronoUnit
)
{
if
(
chronoUnit
==
null
)
{
return
Unit
.
DAYS
;
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/NumberToPeriodConverterTests.java
View file @
28128a95
...
...
@@ -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
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/PeriodToStringConverterTests.java
View file @
28128a95
...
...
@@ -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
());
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/StringToPeriodConverterTests.java
View file @
28128a95
...
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment