From ee709d806c7bc45435f132ce1ed73a22f25a2ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Sun, 24 Jan 2010 20:45:45 +0100 Subject: [PATCH] ItEr44S16CUAsignacionRecursosEspecificosAPlanificacionItEr37S10: Using type as a strategy for doing the right thing when applying the function --- .../planner/entities/StretchesFunction.java | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java index 5096787ef..26e8b8d06 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java @@ -40,7 +40,32 @@ import org.joda.time.LocalDate; public class StretchesFunction extends AssignmentFunction { public enum Type { - DEFAULT; + DEFAULT { + @Override + public void apply(ResourceAllocation allocation, + List intervalsDefinedByStreches, + LocalDate startInclusive, LocalDate endExclusive, + int totalHours) { + Interval.apply(allocation, intervalsDefinedByStreches, + startInclusive, endExclusive, totalHours); + + } + }; + + public void applyTo(ResourceAllocation resourceAllocation, + StretchesFunction stretchesFunction) { + List intervalsDefinedByStreches = stretchesFunction + .getIntervalsDefinedByStreches(); + int totalHours = resourceAllocation.getAssignedHours(); + LocalDate start = resourceAllocation.getStartDate(); + LocalDate end = resourceAllocation.getEndDate(); + apply(resourceAllocation, intervalsDefinedByStreches, start, end, + totalHours); + } + + protected abstract void apply(ResourceAllocation allocation, + List intervalsDefinedByStreches, + LocalDate startInclusive, LocalDate endExclusive, int totalHours); } public static class Interval { @@ -96,11 +121,11 @@ public class StretchesFunction extends AssignmentFunction { } private void apply(ResourceAllocation resourceAllocation, - LocalDate allocationStart, LocalDate allocationEnd, + LocalDate startInclusive, LocalDate endExclusive, int intervalHours) { resourceAllocation.withPreviousAssociatedResources() - .onInterval(getStartFor(allocationStart), - getEndFor(allocationEnd)) + .onInterval(getStartFor(startInclusive), + getEndFor(endExclusive)) .allocateHours(intervalHours); } @@ -157,6 +182,10 @@ public class StretchesFunction extends AssignmentFunction { private Type type; + /** + * This is a transient field. Not stored + */ + private Type desiredType; public void setStretches(List stretches) { this.stretches = stretches; @@ -181,6 +210,14 @@ public class StretchesFunction extends AssignmentFunction { return type == null ? Type.DEFAULT : type; } + private Type getDesiredType() { + return desiredType == null ? getType() : desiredType; + } + + public void changeTypeTo(Type type) { + desiredType = type; + } + public void addStretch(Stretch stretch) { stretches.add(stretch); } @@ -251,12 +288,8 @@ public class StretchesFunction extends AssignmentFunction { if (!resourceAllocation.hasAssignments()) { return; } - List intervalsDefinedByStreches = getIntervalsDefinedByStreches(); - int totalHours = resourceAllocation.getAssignedHours(); - LocalDate start = resourceAllocation.getStartDate(); - LocalDate end = resourceAllocation.getEndDate(); - Interval.apply(resourceAllocation, intervalsDefinedByStreches, start, - end, totalHours); + getDesiredType().applyTo(resourceAllocation, this); + type = getDesiredType(); } public List getIntervalsDefinedByStreches() {