aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-03-06 09:54:29 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-03-06 12:15:22 +0100
commit975dacb594135e449c621d254081f6e179ec51c3 (patch)
treec455844ac17e4968c41f6dd65facad1bcdddf1dc /sonar-plugin-api/src/main/java/org/sonar
parentfdab089af9a50ab912cbf0061e61f00075a6f264 (diff)
downloadsonarqube-975dacb594135e449c621d254081f6e179ec51c3.tar.gz
sonarqube-975dacb594135e449c621d254081f6e179ec51c3.zip
SONAR-5056 Update Duration API : remove days, hours and minutes methods as it depends on number of hours in a day
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java91
1 files changed, 31 insertions, 60 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java
index accff8fc93b..ac61864d84b 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java
@@ -20,6 +20,9 @@
package org.sonar.api.utils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -33,46 +36,24 @@ public class Duration implements Serializable {
public static final String HOUR = "h";
public static final String MINUTE = "min";
- private static final short HOURS_IN_ONE_DAY = 24;
private static final short MINUTES_IN_ONE_HOUR = 60;
private final long durationInMinutes;
- private int days;
- private int hours;
- private int minutes;
-
private Duration(long durationInMinutes) {
this.durationInMinutes = durationInMinutes;
- this.days = ((Double) ((double) durationInMinutes / HOURS_IN_ONE_DAY / MINUTES_IN_ONE_HOUR)).intValue();
- Long currentDurationInMinutes = durationInMinutes - (days * HOURS_IN_ONE_DAY * MINUTES_IN_ONE_HOUR);
- this.hours = ((Double) (currentDurationInMinutes.doubleValue() / MINUTES_IN_ONE_HOUR)).intValue();
- currentDurationInMinutes = currentDurationInMinutes - (hours * MINUTES_IN_ONE_HOUR);
- this.minutes = currentDurationInMinutes.intValue();
- }
-
- private Duration(int days, int hours, int minutes) {
- this(calculateDurationInMinutes(days, hours, minutes, HOURS_IN_ONE_DAY));
- }
-
- public static Duration ofDays(int days) {
- return new Duration(days, 0, 0);
}
- public static Duration ofHours(int hours) {
- return new Duration(0, hours, 0);
- }
-
- public static Duration ofMinutes(int minutes) {
- return new Duration(0, 0, minutes);
+ private Duration(int days, int hours, int minutes, int hoursInDay) {
+ this(((long) days * hoursInDay * MINUTES_IN_ONE_HOUR) + (hours * MINUTES_IN_ONE_HOUR) + minutes);
}
public static Duration create(long durationInMinutes) {
return new Duration(durationInMinutes);
}
- public static Duration decode(String text) {
- return new Duration(extractValue(text, DAY), extractValue(text, HOUR), extractValue(text, MINUTE));
+ public static Duration decode(String text, int hoursInDay) {
+ return new Duration(extractValue(text, DAY), extractValue(text, HOUR), extractValue(text, MINUTE), hoursInDay);
}
private static int extractValue(String text, String unit) {
@@ -85,36 +66,39 @@ public class Duration implements Serializable {
}
return 0;
} catch (NumberFormatException e) {
- throw new IllegalArgumentException(String.format("'%s' is invalid, it should use the following sample format : 2d 10h 15min", text), e);
+ throw new IllegalArgumentException(String.format("Duration '%s' is invalid, it should use the following sample format : 2d 10h 15min", text), e);
}
}
- public String encode() {
- return toString();
- }
-
- public int days() {
- return days;
- }
-
- public int hours() {
- return hours;
- }
+ public String encode(int hoursInDay) {
+ int days = ((Double) ((double) durationInMinutes / hoursInDay / MINUTES_IN_ONE_HOUR)).intValue();
+ Long remainingDuration = durationInMinutes - (days * hoursInDay * MINUTES_IN_ONE_HOUR);
+ int hours = ((Double) (remainingDuration.doubleValue() / MINUTES_IN_ONE_HOUR)).intValue();
+ remainingDuration = remainingDuration - (hours * MINUTES_IN_ONE_HOUR);
+ int minutes = remainingDuration.intValue();
- public int minutes() {
- return minutes;
+ StringBuilder stringBuilder = new StringBuilder();
+ if (days > 0) {
+ stringBuilder.append(days);
+ stringBuilder.append(DAY);
+ }
+ if (hours > 0) {
+ stringBuilder.append(hours);
+ stringBuilder.append(HOUR);
+ }
+ if (minutes > 0) {
+ stringBuilder.append(minutes);
+ stringBuilder.append(MINUTE);
+ }
+ return stringBuilder.toString();
}
public long toMinutes() {
return durationInMinutes;
}
- public long toMinutes(int hoursInDay) {
- return calculateDurationInMinutes(days, hours, minutes, hoursInDay);
- }
-
- private static long calculateDurationInMinutes(int days, int hours, int minutes, int hoursInDay){
- return ((long) days * hoursInDay * MINUTES_IN_ONE_HOUR) + (hours * MINUTES_IN_ONE_HOUR) + minutes;
+ public boolean isGreaterThan(Duration other){
+ return toMinutes() > other.toMinutes();
}
@Override
@@ -141,19 +125,6 @@ public class Duration implements Serializable {
@Override
public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- if (days > 0) {
- stringBuilder.append(days);
- stringBuilder.append(DAY);
- }
- if (hours > 0) {
- stringBuilder.append(hours);
- stringBuilder.append(HOUR);
- }
- if (minutes > 0) {
- stringBuilder.append(minutes);
- stringBuilder.append(MINUTE);
- }
- return stringBuilder.toString();
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}