Add min and max methods to Capacity

This commit is contained in:
Óscar González Fernández 2011-02-22 16:51:36 +01:00
parent 834d29e887
commit 219b3c1f16
2 changed files with 75 additions and 0 deletions

View file

@ -37,6 +37,36 @@ import org.navalplanner.business.workingday.EffortDuration.Granularity;
*/
public class Capacity {
public static Capacity min(Capacity a, Capacity b) {
return new Capacity(EffortDuration.min(a.getStandardEffort(),
b.getStandardEffort()), minExtraEffort(a, b));
}
private static EffortDuration minExtraEffort(Capacity a, Capacity b) {
if (a.isOverAssignableWithoutLimit()) {
return b.getAllowedExtraEffort();
}
if (b.isOverAssignableWithoutLimit()) {
return a.getAllowedExtraEffort();
}
return EffortDuration.min(a.getAllowedExtraEffort(),
b.getAllowedExtraEffort());
}
public static Capacity max(Capacity a, Capacity b) {
return new Capacity(EffortDuration.max(a.getStandardEffort(),
b.getStandardEffort()), maxExtraEffort(a, b));
}
private static EffortDuration maxExtraEffort(Capacity a, Capacity b) {
if (a.isOverAssignableWithoutLimit()
|| b.isOverAssignableWithoutLimit()) {
return null;
}
return EffortDuration.max(a.getAllowedExtraEffort(),
b.getAllowedExtraEffort());
}
public static Capacity create(EffortDuration standardEffort) {
return new Capacity(standardEffort, null);
}

View file

@ -136,4 +136,49 @@ public class CapacityTest {
assertThat(multiplied.getStandardEffort(), equalTo(hours(16)));
assertThat(multiplied.getAllowedExtraEffort(), equalTo(hours(4)));
}
private Capacity a = Capacity.create(hours(8));
private Capacity b = Capacity.create(hours(4));
@Test
public void theMinOfTwoCapacitiesReturnsTheMinimumStandardEffort() {
Capacity min = Capacity.min(a, b);
assertThat(min.getStandardEffort(), equalTo(b.getStandardEffort()));
Capacity max = Capacity.max(a, b);
assertThat(max.getStandardEffort(), equalTo(a.getStandardEffort()));
}
@Test
public void theMaxOfTwoCapacitiesReturnsTheMaximumStandardEffort() {
Capacity max = Capacity.max(a, b);
assertThat(max.getStandardEffort(), equalTo(a.getStandardEffort()));
}
@Test
public void theExtraEffortIsAlsoMinimized() {
assertThat(
Capacity.min(a.withAllowedExtraEffort(hours(2)),
b.overAssignableWithoutLimit(true))
.getAllowedExtraEffort(), equalTo(hours(2)));
assertThat(
Capacity.min(a.withAllowedExtraEffort(hours(2)),
a.withAllowedExtraEffort(hours(4)))
.getAllowedExtraEffort(), equalTo(hours(2)));
}
@Test
public void theExtraEffortIsMaximized() {
assertThat(
Capacity.max(a.withAllowedExtraEffort(hours(2)),
b.overAssignableWithoutLimit(true))
.getAllowedExtraEffort(), nullValue());
assertThat(
Capacity.max(a.withAllowedExtraEffort(hours(2)),
a.withAllowedExtraEffort(hours(4)))
.getAllowedExtraEffort(), equalTo(hours(4)));
}
}