Skip to content

Commit d9e1ecf

Browse files
committed
extract public method for longValueFromUnit conversion, polish
1 parent d0bcb7c commit d9e1ecf

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatterUtils.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,20 @@ public static DurationFormat.Style detect(String value) {
131131
if (SIMPLE_PATTERN.matcher(value).matches()) {
132132
return DurationFormat.Style.SIMPLE;
133133
}
134-
throw new IllegalArgumentException("'" + value + "' is not a valid duration");
134+
throw new IllegalArgumentException("'" + value + "' is not a valid duration, cannot detect any known style");
135+
}
136+
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+
};
135148
}
136149

137150
private static final Pattern ISO_8601_PATTERN = Pattern.compile("^[+-]?[pP].*$");
@@ -162,6 +175,11 @@ private static Duration parseSimple(String text, @Nullable ChronoUnit fallbackUn
162175
}
163176
}
164177

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+
165183
/* package-private */ static ChronoUnit unitFromSuffix(String suffix) {
166184
return switch (suffix.toLowerCase()) {
167185
case "ns" -> ChronoUnit.NANOS;
@@ -184,22 +202,8 @@ private static Duration parseSimple(String text, @Nullable ChronoUnit fallbackUn
184202
case MINUTES -> "m";
185203
case HOURS -> "h";
186204
case DAYS -> "d";
187-
default -> throw new IllegalArgumentException("'" + unit + "' is not a supported ChronoUnit for simple duration representation");
205+
default -> throw new IllegalArgumentException("'" + unit.name() + "' is not a supported ChronoUnit for simple duration representation");
188206
};
189207
}
190208

191-
private static String printSimple(Duration duration, ChronoUnit unit) {
192-
long longValue = switch (unit) {
193-
case NANOS -> duration.toNanos();
194-
case MICROS -> duration.toNanos() / 1000L;
195-
case MILLIS -> duration.toMillis();
196-
case SECONDS -> duration.toSeconds();
197-
case MINUTES -> duration.toMinutes();
198-
case HOURS -> duration.toHours();
199-
case DAYS -> duration.toDays();
200-
default -> throw new IllegalArgumentException("'" + unit + "' is not a supported ChronoUnit for simple duration representation");
201-
};
202-
203-
return longValue + suffixFromUnit(unit);
204-
}
205209
}

0 commit comments

Comments
 (0)