Add method decomposing an EffortDuration into its elements
FEA: ItEr60S19TimeUnitDataType
This commit is contained in:
parent
c15b228904
commit
597989de06
2 changed files with 39 additions and 0 deletions
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
package org.navalplanner.business.workingday;
|
||||
|
||||
import java.util.EnumMap;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
public class EffortDuration {
|
||||
|
|
@ -28,6 +30,10 @@ public class EffortDuration {
|
|||
public enum Granularity {
|
||||
HOURS(3600), MINUTES(60), SECONDS(1);
|
||||
|
||||
static Granularity[] fromMoreCoarseToLessCoarse() {
|
||||
return Granularity.values();
|
||||
}
|
||||
|
||||
private final int secondsPerUnit;
|
||||
|
||||
private Granularity(int secondsPerUnit) {
|
||||
|
|
@ -104,4 +110,17 @@ public class EffortDuration {
|
|||
return getSeconds();
|
||||
}
|
||||
|
||||
public EnumMap<Granularity, Integer> decompose() {
|
||||
EnumMap<Granularity, Integer> result = new EnumMap<EffortDuration.Granularity, Integer>(
|
||||
Granularity.class);
|
||||
int remainder = seconds;
|
||||
for (Granularity each : Granularity.fromMoreCoarseToLessCoarse()) {
|
||||
int value = each.convertFromSeconds(remainder);
|
||||
remainder -= value * each.toSeconds(1);
|
||||
result.put(each, value);
|
||||
}
|
||||
assert remainder == 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ import static org.junit.Assert.fail;
|
|||
import static org.navalplanner.business.workingday.EffortDuration.hours;
|
||||
import static org.navalplanner.business.workingday.EffortDuration.minutes;
|
||||
|
||||
import java.util.EnumMap;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.workingday.EffortDuration;
|
||||
import org.navalplanner.business.workingday.EffortDuration.Granularity;
|
||||
|
|
@ -93,4 +95,22 @@ public class EffortDurationTest {
|
|||
equalTo(ninetyMinutes.hashCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anEffortDurationCanBeDecomposedIntoElements() {
|
||||
EffortDuration duration = hours(1).and(90, Granularity.MINUTES);
|
||||
EnumMap<Granularity, Integer> values = duration.decompose();
|
||||
assertThat(values.get(Granularity.HOURS), equalTo(2));
|
||||
assertThat(values.get(Granularity.MINUTES), equalTo(30));
|
||||
assertThat(values.get(Granularity.SECONDS), equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anEmptyDurationHasZeroElements(){
|
||||
EffortDuration duration = EffortDuration.zero();
|
||||
EnumMap<Granularity, Integer> values = duration.decompose();
|
||||
assertThat(values.get(Granularity.HOURS), equalTo(0));
|
||||
assertThat(values.get(Granularity.MINUTES), equalTo(0));
|
||||
assertThat(values.get(Granularity.SECONDS), equalTo(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue