Skip to content

Commit b523f3c

Browse files
committed
Polish documentation and exception message for @⁠DurationFormat
1 parent 7f9901d commit b523f3c

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

framework-docs/modules/ROOT/pages/core/validation/format.adoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Kotlin::
281281

282282
A portable format annotation API exists in the `org.springframework.format.annotation`
283283
package. You can use `@NumberFormat` to format `Number` fields such as `Double` and
284-
`Long`, `@DurationFormat` to format `Duration` fields in ISO8601 and simplified styles,
284+
`Long`, `@DurationFormat` to format `Duration` fields in ISO-8601 and simplified styles,
285285
and `@DateTimeFormat` to format fields such as `java.util.Date`, `java.util.Calendar`,
286286
and `Long` (for millisecond timestamps) as well as JSR-310 `java.time` types.
287287

@@ -312,7 +312,8 @@ Kotlin::
312312
======
313313

314314
For further details, see the javadoc for
315-
{spring-framework-api}/format/annotation/DateTimeFormat.html[`@DateTimeFormat`] and
315+
{spring-framework-api}/format/annotation/DateTimeFormat.html[`@DateTimeFormat`],
316+
{spring-framework-api}/format/annotation/DurationFormat.html[`@DurationFormat`], and
316317
{spring-framework-api}/format/annotation/NumberFormat.html[`@NumberFormat`].
317318

318319
[WARNING]

spring-context/src/main/java/org/springframework/format/annotation/DurationFormat.java

+29-26
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* Declares that a field or method parameter should be formatted as a
32-
* {@link java.time.Duration}, according to the specified {@code style}.
32+
* {@link java.time.Duration}, according to the specified {@link #style style}.
3333
*
3434
* @author Simon Baslé
3535
* @since 6.2
@@ -40,15 +40,15 @@
4040
public @interface DurationFormat {
4141

4242
/**
43-
* Which {@code Style} to use for parsing and printing a {@code Duration}.
44-
* Defaults to the JDK style ({@link Style#ISO8601}).
43+
* The {@code Style} to use for parsing and printing a {@code Duration}.
44+
* <p>Defaults to the JDK style ({@link Style#ISO8601}).
4545
*/
4646
Style style() default Style.ISO8601;
4747

4848
/**
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
51-
* provided in the input ({@code Unit.MILLIS} if unspecified).
49+
* The {@link Unit} to fall back to in case the {@code style()} needs a unit
50+
* for either parsing or printing, and none is explicitly provided in the input.
51+
* <p>Defaults to {@link Unit#MILLIS} if unspecified.
5252
*/
5353
Unit defaultUnit() default Unit.MILLIS;
5454

@@ -59,14 +59,15 @@ enum Style {
5959

6060
/**
6161
* Simple formatting based on a short suffix, for example '1s'.
62-
* Supported unit suffixes are: {@code ns, us, ms, s, m, h, d}.
63-
* This corresponds to nanoseconds, microseconds, milliseconds, seconds,
64-
* minutes, hours and days respectively.
62+
* <p>Supported unit suffixes include: {@code ns, us, ms, s, m, h, d}.
63+
* Those correspond to nanoseconds, microseconds, milliseconds, seconds,
64+
* minutes, hours, and days, respectively.
6565
* <p>Note that when printing a {@code Duration}, this style can be
6666
* lossy if the selected unit is bigger than the resolution of the
67-
* duration. For example, * {@code Duration.ofMillis(5).plusNanos(1234)}
67+
* duration. For example, {@code Duration.ofMillis(5).plusNanos(1234)}
6868
* would get truncated to {@code "5ms"} when printing using
69-
* {@code ChronoUnit.MILLIS}. Fractional durations are not supported.
69+
* {@code ChronoUnit.MILLIS}.
70+
* <p>Fractional durations are not supported.
7071
*/
7172
SIMPLE,
7273

@@ -80,22 +81,24 @@ enum Style {
8081
/**
8182
* Like {@link #SIMPLE}, but allows multiple segments ordered from
8283
* largest-to-smallest units of time, like {@code 1h12m27s}.
83-
* <p>
84-
* A single minus sign ({@code -}) is allowed to indicate the whole
84+
* <p>A single minus sign ({@code -}) is allowed to indicate the whole
8585
* duration is negative. Spaces are allowed between segments, and a
8686
* negative duration with spaced segments can optionally be surrounded
87-
* by parenthesis after the minus sign, like so: {@code -(34m 57s)}.
87+
* by parentheses after the minus sign, like so: {@code -(34m 57s)}.
8888
*/
8989
COMPOSITE
9090
}
9191

9292
/**
9393
* 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.
94+
* allows conversion to and from a supported {@code ChronoUnit} as well as
95+
* conversion from durations to longs.
96+
*
97+
* <p>The enum includes its corresponding suffix in the {@link Style#SIMPLE simple}
98+
* {@code Duration} format style.
9799
*/
98100
enum Unit {
101+
99102
/**
100103
* Nanoseconds ({@code "ns"}).
101104
*/
@@ -153,7 +156,7 @@ public ChronoUnit asChronoUnit() {
153156

154157
/**
155158
* Convert this {@code DurationFormat.Unit} to a simple {@code String}
156-
* suffix, suitable for the {@link Style#SIMPLE} style.
159+
* suffix, suitable for the {@link Style#SIMPLE SIMPLE} style.
157160
*/
158161
public String asSuffix() {
159162
return this.suffix;
@@ -173,9 +176,9 @@ public Duration parse(String value) {
173176
* Print a {@code Duration} as a {@code String}, converting it to a long
174177
* value using this unit's precision via {@link #longValue(Duration)}
175178
* and appending this unit's simple {@link #asSuffix() suffix}.
176-
* @param value the {@code Duration} to convert to String
179+
* @param value the {@code Duration} to convert to a String
177180
* @return the String representation of the {@code Duration} in the
178-
* {@link Style#SIMPLE SIMPLE style}
181+
* {@link Style#SIMPLE SIMPLE} style
179182
*/
180183
public String print(Duration value) {
181184
return longValue(value) + asSuffix();
@@ -187,17 +190,17 @@ public String print(Duration value) {
187190
* bigger than the actual resolution of the duration.
188191
* <p>For example, {@code Duration.ofMillis(5).plusNanos(1234)} would
189192
* get truncated to {@code 5} for unit {@code MILLIS}.
190-
* @param value the {@code Duration} to convert to long
191-
* @return the long value for the Duration in this Unit
193+
* @param value the {@code Duration} to convert to a long
194+
* @return the long value for the {@code Duration} in this {@code Unit}
192195
*/
193196
public long longValue(Duration value) {
194197
return this.longValue.apply(value);
195198
}
196199

197200
/**
198201
* Get the {@code Unit} corresponding to the given {@code ChronoUnit}.
199-
* @throws IllegalArgumentException if that particular ChronoUnit isn't
200-
* supported
202+
* @throws IllegalArgumentException if the given {@code ChronoUnit} is
203+
* not supported
201204
*/
202205
public static Unit fromChronoUnit(@Nullable ChronoUnit chronoUnit) {
203206
if (chronoUnit == null) {
@@ -208,12 +211,12 @@ public static Unit fromChronoUnit(@Nullable ChronoUnit chronoUnit) {
208211
return candidate;
209212
}
210213
}
211-
throw new IllegalArgumentException("No matching Unit for ChronoUnit." + chronoUnit.name());
214+
throw new IllegalArgumentException("No matching Unit for ChronoUnit: " + chronoUnit.name());
212215
}
213216

214217
/**
215218
* Get the {@code Unit} corresponding to the given {@code String} suffix.
216-
* @throws IllegalArgumentException if that particular suffix is unknown
219+
* @throws IllegalArgumentException if the given suffix is not supported
217220
*/
218221
public static Unit fromSuffix(String suffix) {
219222
for (Unit candidate : values()) {

spring-context/src/main/java/org/springframework/format/annotation/package-info.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Annotations for declaratively configuring field formatting rules.
2+
* Annotations for declaratively configuring field and parameter formatting rules.
33
*/
44
@NonNullApi
55
@NonNullFields

0 commit comments

Comments
 (0)