ItEr44S16CUAsignacionRecursosEspecificosAPlanificacionItEr37S10: Not adding last interval with remaining load.

Checking instead last strech has all the load accumulated.
This commit is contained in:
Óscar González Fernández 2010-01-25 00:55:11 +01:00
parent 77e6fd1b0c
commit dcee214251
2 changed files with 29 additions and 40 deletions

View file

@ -92,7 +92,7 @@ public class StretchesFunction extends AssignmentFunction {
LocalDate end) {
Validate.notNull(loadProportion);
Validate.isTrue(loadProportion.signum() >= 0);
Validate.isTrue(start != null || end != null);
Validate.notNull(end);
this.loadProportion = loadProportion.setScale(2,
RoundingMode.HALF_UP);
this.start = start;
@ -115,10 +115,6 @@ public class StretchesFunction extends AssignmentFunction {
return start;
}
public boolean hasNoEnd() {
return end == null;
}
public int getHoursFor(int totalHours) {
return loadProportion.multiply(new BigDecimal(totalHours))
.intValue();
@ -128,16 +124,12 @@ public class StretchesFunction extends AssignmentFunction {
return hasNoStart() ? allocationStart : start;
}
public LocalDate getEndFor(LocalDate allocationEnd) {
return hasNoEnd() ? allocationEnd : end;
}
private void apply(ResourceAllocation<?> resourceAllocation,
LocalDate startInclusive, LocalDate endExclusive,
int intervalHours) {
resourceAllocation.withPreviousAssociatedResources()
.onInterval(getStartFor(startInclusive),
getEndFor(endExclusive))
getEnd())
.allocateHours(intervalHours);
}
@ -340,13 +332,15 @@ public class StretchesFunction extends AssignmentFunction {
previous = strechDate;
}
BigDecimal left = calculateLeftFor(sumOfProportions);
result.add(new Interval(left, previous, null));
if (!left.equals(BigDecimal.ZERO)) {
throw new IllegalStateException("the streches must sum the 100%");
}
return result;
}
private BigDecimal calculateLeftFor(BigDecimal sumOfProportions) {
BigDecimal left = BigDecimal.ONE.subtract(sumOfProportions);
left = left.signum() < 0 ? BigDecimal.ZERO : left;
left = left.signum() <= 0 ? BigDecimal.ZERO : left;
return left;
}

View file

@ -177,12 +177,19 @@ public class StretchesFunctionTest {
assertTrue(stretchesFunction.getIntervalsDefinedByStreches().isEmpty());
}
@Test
public void oneStrechImpliesTwoIntervals() {
@Test(expected = IllegalStateException.class)
public void theLastStrechMustHaveAllTheLoad() {
givenStretchesFunction();
givenStretchAsChild(new LocalDate().plusMonths(1), new BigDecimal(0.5));
givenStretchAsChild(new LocalDate().plusMonths(1), new BigDecimal(0.6));
stretchesFunction.getIntervalsDefinedByStreches();
}
@Test
public void oneStrechImpliesOneInterval() {
givenStretchesFunction();
givenStretchAsChild(new LocalDate().plusMonths(1), new BigDecimal(1));
assertThat(stretchesFunction.getIntervalsDefinedByStreches().size(),
equalTo(2));
equalTo(1));
}
@Test
@ -191,6 +198,7 @@ public class StretchesFunctionTest {
LocalDate strechDate = new LocalDate().plusMonths(1);
BigDecimal amountOfWorkProportion = new BigDecimal(0.5).setScale(2);
givenStretchAsChild(strechDate, amountOfWorkProportion);
givenStretchAsChild(new LocalDate().plusMonths(2), new BigDecimal(1.0));
Interval firstInterval = stretchesFunction
.getIntervalsDefinedByStreches().get(0);
assertThat(firstInterval.getEnd(), equalTo(strechDate));
@ -204,33 +212,35 @@ public class StretchesFunctionTest {
givenStretchesFunction();
LocalDate strechDate = new LocalDate().plusMonths(1);
givenStretchAsChild(strechDate, new BigDecimal(0.5));
givenStretchAsChild(strechDate.plusDays(20), new BigDecimal(1));
Interval lastInterval = stretchesFunction
.getIntervalsDefinedByStreches().get(1);
assertThat(lastInterval.getStart(), equalTo(strechDate));
}
@Test
public void aIntervalInTheMiddleHasStartAndEnd() {
public void aIntervalInTheMiddleHasStart() {
givenStretchesFunction();
LocalDate start = new LocalDate().plusMonths(1);
givenStretchAsChild(start, new BigDecimal(0.5));
LocalDate middleEnd = start.plusMonths(2);
givenStretchAsChild(middleEnd, new BigDecimal(0.6));
givenStretchAsChild(middleEnd.plusDays(10), new BigDecimal(1));
Interval middle = stretchesFunction.getIntervalsDefinedByStreches().get(
1);
assertFalse(middle.hasNoStart());
assertFalse(middle.hasNoEnd());
assertThat(middle.getStart(), equalTo(start));
assertThat(middle.getEnd(), equalTo(middleEnd));
}
@Test
public void eachIntervalAccumulatesAllTheLoads() {
public void eachIntervalHasTheCorrespondingLoadForThatInterval() {
givenStretchesFunction();
LocalDate start = new LocalDate().plusMonths(1);
givenStretchAsChild(start, new BigDecimal(0.5));
LocalDate middleEnd = start.plusMonths(2);
givenStretchAsChild(middleEnd, new BigDecimal(0.8));
givenStretchAsChild(middleEnd.plusDays(10), new BigDecimal(1));
Interval first = stretchesFunction.getIntervalsDefinedByStreches().get(
0);
Interval middle = stretchesFunction.getIntervalsDefinedByStreches()
@ -245,20 +255,6 @@ public class StretchesFunctionTest {
.setScale(2, RoundingMode.HALF_EVEN)));
}
@Test
public void theLastIntervalHasTheRemainingLoad() {
givenStretchesFunction();
LocalDate start = new LocalDate().plusMonths(1);
givenStretchAsChild(start, new BigDecimal(0.3));
givenStretchAsChild(start.plusMonths(2), new BigDecimal(0.5));
givenStretchAsChild(start.plusMonths(3), new BigDecimal(0.7));
Interval lastInterval = stretchesFunction
.getIntervalsDefinedByStreches().get(3);
BigDecimal expectedRemaining = new BigDecimal(0.3).setScale(2,
RoundingMode.HALF_UP);
assertThat(lastInterval.getLoadProportion(), equalTo(expectedRemaining));
}
@Test
public void ifTheIntervalStartIsNullReturnsThePassedStartDate() {
LocalDate end = new LocalDate().plusMonths(1);
@ -267,18 +263,17 @@ public class StretchesFunctionTest {
assertThat(interval.getStartFor(now), equalTo(now));
}
@Test
public void ifTheIntervalEndIsNullReturnsThePassedStartDate() {
@Test(expected = IllegalArgumentException.class)
public void endDateCannotBeNull() {
LocalDate start = new LocalDate().plusMonths(1);
Interval interval = new Interval(new BigDecimal(0.3), start, null);
LocalDate now = new LocalDate();
assertThat(interval.getEndFor(now), equalTo(now));
new Interval(new BigDecimal(0.3), start, null);
}
@Test
public void ifTheIntervalStartIsNotNullReturnsItsStartDate() {
LocalDate start = new LocalDate().plusMonths(1);
Interval interval = new Interval(new BigDecimal(0.3), start, null);
Interval interval = new Interval(new BigDecimal(0.3), start, start
.plusDays(20));
assertThat(interval.getStartFor(new LocalDate()), equalTo(start));
}
@ -287,7 +282,7 @@ public class StretchesFunctionTest {
LocalDate start = new LocalDate().plusMonths(1);
LocalDate end = new LocalDate().plusMonths(2);
Interval interval = new Interval(new BigDecimal(0.3), start, end);
assertThat(interval.getEndFor(new LocalDate()), equalTo(end));
assertThat(interval.getEnd(), equalTo(end));
}
}