17
17
package org .springframework .format .datetime .standard ;
18
18
19
19
import java .time .Duration ;
20
- import java .time .temporal .ChronoUnit ;
21
20
import java .util .regex .Matcher ;
22
21
import java .util .regex .Pattern ;
23
22
29
28
/**
30
29
* Support {@code Duration} parsing and printing in several styles, as listed in
31
30
* {@link DurationFormat.Style}.
32
- * <p>Some styles may not enforce any unit to be present, defaulting to {@code ChronoUnit #MILLIS}
33
- * in that case. Methods in this class offer overloads that take a {@code ChronoUnit } to
31
+ * <p>Some styles may not enforce any unit to be present, defaulting to {@code DurationFormat.Unit #MILLIS}
32
+ * in that case. Methods in this class offer overloads that take a {@link DurationFormat.Unit } to
34
33
* be used as a fall-back instead of the ultimate MILLIS default.
35
34
*
36
35
* @author Phillip Webb
@@ -61,7 +60,7 @@ public static Duration parse(String value, DurationFormat.Style style) {
61
60
* will default to ms)
62
61
* @return a duration
63
62
*/
64
- public static Duration parse (String value , DurationFormat .Style style , @ Nullable ChronoUnit unit ) {
63
+ public static Duration parse (String value , DurationFormat .Style style , @ Nullable DurationFormat . Unit unit ) {
65
64
return switch (style ) {
66
65
case ISO8601 -> parseIso8601 (value );
67
66
case SIMPLE -> parseSimple (value , unit );
@@ -86,7 +85,7 @@ public static String print(Duration value, DurationFormat.Style style) {
86
85
* to ms)
87
86
* @return the printed result
88
87
*/
89
- public static String print (Duration value , DurationFormat .Style style , @ Nullable ChronoUnit unit ) {
88
+ public static String print (Duration value , DurationFormat .Style style , @ Nullable DurationFormat . Unit unit ) {
90
89
return switch (style ) {
91
90
case ISO8601 -> value .toString ();
92
91
case SIMPLE -> printSimple (value , unit );
@@ -113,7 +112,7 @@ public static Duration detectAndParse(String value) {
113
112
* @throws IllegalArgumentException if the value is not a known style or cannot be
114
113
* parsed
115
114
*/
116
- public static Duration detectAndParse (String value , @ Nullable ChronoUnit unit ) {
115
+ public static Duration detectAndParse (String value , @ Nullable DurationFormat . Unit unit ) {
117
116
return parse (value , detect (value ), unit );
118
117
}
119
118
@@ -134,19 +133,6 @@ public static DurationFormat.Style detect(String value) {
134
133
throw new IllegalArgumentException ("'" + value + "' is not a valid duration, cannot detect any known style" );
135
134
}
136
135
137
- public static long longValueFromUnit (Duration duration , ChronoUnit unit ) {
138
- return switch (unit ) {
139
- case NANOS -> duration .toNanos ();
140
- case MICROS -> duration .toNanos () / 1000L ;
141
- case MILLIS -> duration .toMillis ();
142
- case SECONDS -> duration .toSeconds ();
143
- case MINUTES -> duration .toMinutes ();
144
- case HOURS -> duration .toHours ();
145
- case DAYS -> duration .toDays ();
146
- default -> throw new IllegalArgumentException ("'" + unit .name () + "' is not a supported ChronoUnit for simple duration representation" );
147
- };
148
- }
149
-
150
136
private static final Pattern ISO_8601_PATTERN = Pattern .compile ("^[+-]?[pP].*$" );
151
137
private static final Pattern SIMPLE_PATTERN = Pattern .compile ("^([+-]?\\ d+)([a-zA-Z]{0,2})$" );
152
138
@@ -159,51 +145,25 @@ private static Duration parseIso8601(String value) {
159
145
}
160
146
}
161
147
162
- private static Duration parseSimple (String text , @ Nullable ChronoUnit fallbackUnit ) {
148
+ private static Duration parseSimple (String text , @ Nullable DurationFormat . Unit fallbackUnit ) {
163
149
try {
164
150
Matcher matcher = SIMPLE_PATTERN .matcher (text );
165
151
Assert .state (matcher .matches (), "Does not match simple duration pattern" );
166
152
String suffix = matcher .group (2 );
167
- ChronoUnit parsingUnit = (fallbackUnit == null ? ChronoUnit .MILLIS : fallbackUnit );
153
+ DurationFormat . Unit parsingUnit = (fallbackUnit == null ? DurationFormat . Unit .MILLIS : fallbackUnit );
168
154
if (StringUtils .hasLength (suffix )) {
169
- parsingUnit = unitFromSuffix (suffix );
155
+ parsingUnit = DurationFormat . Unit . fromSuffix (suffix );
170
156
}
171
- return Duration . of ( Long . parseLong ( matcher .group (1 )), parsingUnit );
157
+ return parsingUnit . parse ( matcher .group (1 ));
172
158
}
173
159
catch (Exception ex ) {
174
160
throw new IllegalArgumentException ("'" + text + "' is not a valid simple duration" , ex );
175
161
}
176
162
}
177
163
178
- private static String printSimple (Duration duration , @ Nullable ChronoUnit unit ) {
179
- unit = (unit == null ? ChronoUnit .MILLIS : unit );
180
- return longValueFromUnit (duration , unit ) + suffixFromUnit (unit );
181
- }
182
-
183
- /* package-private */ static ChronoUnit unitFromSuffix (String suffix ) {
184
- return switch (suffix .toLowerCase ()) {
185
- case "ns" -> ChronoUnit .NANOS ;
186
- case "us" -> ChronoUnit .MICROS ;
187
- case "ms" -> ChronoUnit .MILLIS ;
188
- case "s" -> ChronoUnit .SECONDS ;
189
- case "m" -> ChronoUnit .MINUTES ;
190
- case "h" -> ChronoUnit .HOURS ;
191
- case "d" -> ChronoUnit .DAYS ;
192
- default -> throw new IllegalArgumentException ("'" + suffix + "' is not a valid simple duration unit" );
193
- };
194
- }
195
-
196
- /* package-private */ static String suffixFromUnit (ChronoUnit unit ) {
197
- return switch (unit ) {
198
- case NANOS -> "ns" ;
199
- case MICROS -> "us" ;
200
- case MILLIS -> "ms" ;
201
- case SECONDS -> "s" ;
202
- case MINUTES -> "m" ;
203
- case HOURS -> "h" ;
204
- case DAYS -> "d" ;
205
- default -> throw new IllegalArgumentException ("'" + unit .name () + "' is not a supported ChronoUnit for simple duration representation" );
206
- };
164
+ private static String printSimple (Duration duration , @ Nullable DurationFormat .Unit unit ) {
165
+ unit = (unit == null ? DurationFormat .Unit .MILLIS : unit );
166
+ return unit .print (duration );
207
167
}
208
168
209
169
}
0 commit comments