28
28
import org .springframework .lang .Nullable ;
29
29
30
30
/**
31
- * Declares that a field or method parameter should be formatted as a {@link java.time.Duration},
32
- * according to the specified {@code style}.
31
+ * Declares that a field or method parameter should be formatted as a
32
+ * {@link java.time.Duration}, according to the specified {@code style}.
33
33
*
34
34
* @author Simon Baslé
35
35
* @since 6.2
40
40
public @interface DurationFormat {
41
41
42
42
/**
43
- * Which {@code Style} to use for parsing and printing a {@code Duration}. Defaults to
44
- * the JDK style ({@link Style#ISO8601}).
43
+ * Which {@code Style} to use for parsing and printing a {@code Duration}.
44
+ * Defaults to the JDK style ({@link Style#ISO8601}).
45
45
*/
46
46
Style style () default Style .ISO8601 ;
47
47
48
48
/**
49
49
* Define which {@link Unit} to fall back to in case the {@code style()}
50
- * needs a unit for either parsing or printing, and none is explicitly provided in
51
- * the input ({@code Unit.MILLIS} if unspecified).
50
+ * needs a unit for either parsing or printing, and none is explicitly
51
+ * provided in the input ({@code Unit.MILLIS} if unspecified).
52
52
*/
53
53
Unit defaultUnit () default Unit .MILLIS ;
54
54
@@ -62,10 +62,11 @@ enum Style {
62
62
* Supported unit suffixes are: {@code ns, us, ms, s, m, h, d}.
63
63
* This corresponds to nanoseconds, microseconds, milliseconds, seconds,
64
64
* minutes, hours and days respectively.
65
- * <p>Note that when printing a {@code Duration}, this style can be lossy if the
66
- * selected unit is bigger than the resolution of the duration. For example,
67
- * {@code Duration.ofMillis(5).plusNanos(1234)} would get truncated to {@code "5ms"}
68
- * when printing using {@code ChronoUnit.MILLIS}.
65
+ * <p>Note that when printing a {@code Duration}, this style can be
66
+ * lossy if the selected unit is bigger than the resolution of the
67
+ * duration. For example, * {@code Duration.ofMillis(5).plusNanos(1234)}
68
+ * would get truncated to {@code "5ms"} when printing using
69
+ * {@code ChronoUnit.MILLIS}. Fractional durations are not supported.
69
70
*/
70
71
SIMPLE ,
71
72
@@ -74,13 +75,25 @@ enum Style {
74
75
* <p>This is what the JDK uses in {@link java.time.Duration#parse(CharSequence)}
75
76
* and {@link Duration#toString()}.
76
77
*/
77
- ISO8601
78
+ ISO8601 ,
79
+
80
+ /**
81
+ * Like {@link #SIMPLE}, but allows multiple segments ordered from
82
+ * largest-to-smallest units of time, like {@code 1h12m27s}.
83
+ * <p>
84
+ * A single minus sign ({@code -}) is allowed to indicate the whole
85
+ * duration is negative. Spaces are allowed between segments, and a
86
+ * negative duration with spaced segments can optionally be surrounded
87
+ * by parenthesis after the minus sign, like so: {@code -(34m 57s)}.
88
+ */
89
+ COMPOSITE
78
90
}
79
91
80
92
/**
81
- * Duration format unit, which mirrors a subset of {@link ChronoUnit} and allows conversion to and from
82
- * supported {@code ChronoUnit} as well as converting durations to longs.
83
- * The enum includes its corresponding suffix in the {@link Style#SIMPLE simple} Duration format style.
93
+ * Duration format unit, which mirrors a subset of {@link ChronoUnit} and
94
+ * allows conversion to and from supported {@code ChronoUnit} as well as
95
+ * converting durations to longs. The enum includes its corresponding suffix
96
+ * in the {@link Style#SIMPLE simple} Duration format style.
84
97
*/
85
98
enum Unit {
86
99
/**
@@ -101,7 +114,7 @@ enum Unit {
101
114
/**
102
115
* Seconds ({@code "s"}).
103
116
*/
104
- SECONDS (ChronoUnit .SECONDS , "s" , Duration ::getSeconds ),
117
+ SECONDS (ChronoUnit .SECONDS , "s" , Duration ::toSeconds ),
105
118
106
119
/**
107
120
* Minutes ({@code "m"}).
@@ -131,23 +144,24 @@ enum Unit {
131
144
}
132
145
133
146
/**
134
- * Convert this {@code DurationFormat.Unit} to its {@link ChronoUnit} equivalent.
147
+ * Convert this {@code DurationFormat.Unit} to its {@link ChronoUnit}
148
+ * equivalent.
135
149
*/
136
150
public ChronoUnit asChronoUnit () {
137
151
return this .chronoUnit ;
138
152
}
139
153
140
154
/**
141
- * Convert this {@code DurationFormat.Unit} to a simple {@code String} suffix,
142
- * suitable for the {@link Style#SIMPLE} style.
155
+ * Convert this {@code DurationFormat.Unit} to a simple {@code String}
156
+ * suffix, suitable for the {@link Style#SIMPLE} style.
143
157
*/
144
158
public String asSuffix () {
145
159
return this .suffix ;
146
160
}
147
161
148
162
/**
149
- * Parse a {@code long} from a {@code String} and interpret it to be a {@code Duration}
150
- * in the current unit.
163
+ * Parse a {@code long} from a {@code String} and interpret it to be a
164
+ * {@code Duration} in the current unit.
151
165
* @param value the String representation of the long
152
166
* @return the corresponding {@code Duration}
153
167
*/
@@ -156,22 +170,23 @@ public Duration parse(String value) {
156
170
}
157
171
158
172
/**
159
- * Print a {@code Duration} as a {@code String}, converting it to a long value
160
- * using this unit's precision via {@link #longValue(Duration)} and appending
161
- * this unit's simple {@link #asSuffix() suffix}.
173
+ * Print a {@code Duration} as a {@code String}, converting it to a long
174
+ * value using this unit's precision via {@link #longValue(Duration)}
175
+ * and appending this unit's simple {@link #asSuffix() suffix}.
162
176
* @param value the {@code Duration} to convert to String
163
- * @return the String representation of the {@code Duration} in the {@link Style#SIMPLE SIMPLE style}
177
+ * @return the String representation of the {@code Duration} in the
178
+ * {@link Style#SIMPLE SIMPLE style}
164
179
*/
165
180
public String print (Duration value ) {
166
181
return longValue (value ) + asSuffix ();
167
182
}
168
183
169
184
/**
170
- * Convert the given {@code Duration} to a long value in the resolution of this
171
- * unit. Note that this can be lossy if the current unit is bigger than the
172
- * actual resolution of the duration.
173
- * <p>For example, {@code Duration.ofMillis(5).plusNanos(1234)} would get truncated
174
- * to {@code 5} for unit {@code MILLIS}.
185
+ * Convert the given {@code Duration} to a long value in the resolution
186
+ * of this unit. Note that this can be lossy if the current unit is
187
+ * bigger than the actual resolution of the duration.
188
+ * <p>For example, {@code Duration.ofMillis(5).plusNanos(1234)} would
189
+ * get truncated to {@code 5} for unit {@code MILLIS}.
175
190
* @param value the {@code Duration} to convert to long
176
191
* @return the long value for the Duration in this Unit
177
192
*/
@@ -181,7 +196,8 @@ public long longValue(Duration value) {
181
196
182
197
/**
183
198
* Get the {@code Unit} corresponding to the given {@code ChronoUnit}.
184
- * @throws IllegalArgumentException if that particular ChronoUnit isn't supported
199
+ * @throws IllegalArgumentException if that particular ChronoUnit isn't
200
+ * supported
185
201
*/
186
202
public static Unit fromChronoUnit (@ Nullable ChronoUnit chronoUnit ) {
187
203
if (chronoUnit == null ) {
0 commit comments