|
|
@@ -13,7 +13,7 @@ import lombok.Setter;
|
|
|
public class Duration {
|
|
|
@AllArgsConstructor
|
|
|
public enum Display {
|
|
|
- SECONDS("s", 1), MINUTES("min", 60), HOURS("hr", 3600);
|
|
|
+ SECONDS("s", 1), MINUTES("min", 60), HALF_HOURS("hr", 3600), HOURS("hr", 3600);
|
|
|
String abbreviation;
|
|
|
int inSeconds;
|
|
|
}
|
|
|
@@ -29,9 +29,27 @@ public class Duration {
|
|
|
final Display newDisplayAs = displayAs.ordinal() < rhs.displayAs.ordinal() ?
|
|
|
displayAs : rhs.displayAs;
|
|
|
return new Duration(newDisplayAs, isApproximate || rhs.isApproximate,
|
|
|
- minSeconds + rhs.minSeconds, maxSeconds + rhs.maxSeconds);
|
|
|
+ minSeconds + rhs.minSeconds, maxSeconds + rhs.maxSeconds).smartScale();
|
|
|
}
|
|
|
|
|
|
+ public Duration round(int to) {
|
|
|
+ to *= displayAs.inSeconds;
|
|
|
+ return new Duration(displayAs, isApproximate,
|
|
|
+ to * Math.round(minSeconds / (float) to),
|
|
|
+ to * Math.round(maxSeconds / (float) to));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Duration smartScale() {
|
|
|
+ if (minSeconds > Display.HOURS.inSeconds * 4) {
|
|
|
+ displayAs = Display.HOURS;
|
|
|
+ } else if (minSeconds > Display.HOURS.inSeconds * 1) {
|
|
|
+ displayAs = Display.HALF_HOURS;
|
|
|
+ } else if (minSeconds > Display.MINUTES.inSeconds * 2) {
|
|
|
+ displayAs = Display.MINUTES;
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
StringBuilder build = new StringBuilder();
|
|
|
@@ -48,7 +66,10 @@ public class Duration {
|
|
|
return build.toString();
|
|
|
}
|
|
|
|
|
|
- private static int convert(int seconds, Display as) {
|
|
|
- return seconds / as.inSeconds;
|
|
|
+ private static String convert(int seconds, Display as) {
|
|
|
+ if (as == Display.HALF_HOURS && ((2 * seconds / as.inSeconds) % 2) == 1) {
|
|
|
+ return Float.toString((2 * seconds / as.inSeconds) / 2.f);
|
|
|
+ }
|
|
|
+ return Integer.toString(seconds / as.inSeconds);
|
|
|
}
|
|
|
}
|