Deprecate toHours method. Add asDurationOn method instead

FEA: ItEr60S19TimeUnitDataType
This commit is contained in:
Óscar González Fernández 2010-09-15 13:11:26 +02:00
parent 7feef61085
commit ff38c78835
7 changed files with 100 additions and 25 deletions

View file

@ -885,6 +885,7 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
}
@Override
@Deprecated
public Integer toHours(LocalDate day, ResourcesPerDay resourcesPerDay) {
final EffortDuration workableHours = getWorkableTimeAt(day);
return limitOverAssignability(day,
@ -892,6 +893,15 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
workableHours).roundToHours();
}
@Override
public EffortDuration asDurationOn(LocalDate day,
ResourcesPerDay resourcesPerDay) {
final EffortDuration workableHours = getWorkableTimeAt(day);
return limitOverAssignability(day,
resourcesPerDay.asDurationGivenWorkingDayOf(workableHours),
workableHours);
}
private EffortDuration limitOverAssignability(LocalDate day,
EffortDuration effortInitiallyCalculated,
EffortDuration workableHoursAtDay) {

View file

@ -21,6 +21,8 @@
package org.navalplanner.business.calendars.entities;
import static java.util.Arrays.asList;
import static org.navalplanner.business.workingday.EffortDuration.max;
import static org.navalplanner.business.workingday.EffortDuration.min;
import java.util.ArrayList;
import java.util.Collection;
@ -81,13 +83,19 @@ public abstract class CombinedWorkHours implements IWorkHours {
}
@Override
@Deprecated
public Integer toHours(LocalDate day, ResourcesPerDay amount) {
Integer current = null;
return asDurationOn(day, amount).roundToHours();
}
@Override
public EffortDuration asDurationOn(LocalDate day, ResourcesPerDay amount) {
EffortDuration result = null;
for (IWorkHours each : workHours) {
current = current == null ? each.toHours(day, amount)
: updateHours(current, each.toHours(day, amount));
result = result == null ? each.asDurationOn(day, amount)
: updateDuration(result, each.asDurationOn(day, amount));
}
return current;
return result;
}
@Override
@ -102,7 +110,8 @@ public abstract class CombinedWorkHours implements IWorkHours {
protected abstract AvailabilityTimeLine compoundAvailability(
AvailabilityTimeLine accumulated, AvailabilityTimeLine each);
protected abstract Integer updateHours(Integer current, Integer each);
protected abstract EffortDuration updateDuration(EffortDuration current,
EffortDuration each);
protected abstract EffortDuration updateCapacity(EffortDuration current,
EffortDuration each);
@ -128,8 +137,9 @@ class Min extends CombinedWorkHours {
}
@Override
protected Integer updateHours(Integer current, Integer each) {
return Math.min(current, each);
protected EffortDuration updateDuration(EffortDuration current,
EffortDuration each) {
return min(current, each);
}
@Override
@ -153,8 +163,9 @@ class Max extends CombinedWorkHours {
}
@Override
protected Integer updateHours(Integer current, Integer each) {
return Math.max(current, each);
protected EffortDuration updateDuration(EffortDuration current,
EffortDuration each) {
return max(current, each);
}
@Override

View file

@ -29,12 +29,25 @@ public interface IWorkHours {
/**
* Translates the received amount into the corresponding hours at the given
* date
*
* @deprecated use asDurationOn
* @param day
* @param amount
* @return
*/
@Deprecated
public Integer toHours(LocalDate day, ResourcesPerDay amount);
/**
* Translates the received amount into the corresponding duration at the
* given date
*
* @param day
* @param amount
* @return
*/
public EffortDuration asDurationOn(LocalDate day, ResourcesPerDay amount);
/**
* Calculates the capacity duration at a given date. It means all the time
* that could be worked without having overtime

View file

@ -47,9 +47,14 @@ public class SameWorkHoursEveryDay implements IWorkHours {
}
@Override
@Deprecated
public Integer toHours(LocalDate day, ResourcesPerDay amount) {
return amount.asDurationGivenWorkingDayOf(getCapacityDurationAt(day))
.roundToHours();
return asDurationOn(day, amount).roundToHours();
}
@Override
public EffortDuration asDurationOn(LocalDate day, ResourcesPerDay amount) {
return amount.asDurationGivenWorkingDayOf(getCapacityDurationAt(day));
}
@Override

View file

@ -778,6 +778,12 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
return getSubyacent().toHours(day, amount);
}
@Override
public EffortDuration asDurationOn(LocalDate day,
ResourcesPerDay amount) {
return getSubyacent().asDurationOn(day, amount);
}
@Override
public boolean thereAreHoursOn(AvailabilityTimeLine availability,
ResourcesPerDay resourcesPerDay, int hoursToAllocate) {

View file

@ -230,6 +230,10 @@ public class EffortDuration implements Comparable<EffortDuration> {
return Collections.min(Arrays.asList(durations));
}
public static EffortDuration max(EffortDuration... durations) {
return Collections.max(Arrays.asList(durations));
}
private static int roundHalfUpToHours(
EnumMap<Granularity, Integer> components) {
int seconds = components.get(Granularity.SECONDS);

View file

@ -36,6 +36,7 @@ import static org.navalplanner.business.test.planner.entities.DayAssignmentMatch
import static org.navalplanner.business.test.planner.entities.DayAssignmentMatchers.from;
import static org.navalplanner.business.test.planner.entities.DayAssignmentMatchers.haveHours;
import static org.navalplanner.business.test.planner.entities.DayAssignmentMatchers.haveResourceAllocation;
import static org.navalplanner.business.workingday.EffortDuration.hours;
import java.util.HashMap;
import java.util.List;
@ -82,20 +83,35 @@ public class SpecificResourceAllocationTest {
expect(this.calendar.toHours(isA(LocalDate.class),
isA(ResourcesPerDay.class))).andAnswer(
toHoursAnswer(hours)).anyTimes();
expect(
this.calendar.asDurationOn(isA(LocalDate.class),
isA(ResourcesPerDay.class))).andAnswer(
asDurationOnAnswer(hours(hours))).anyTimes();
expect(this.calendar.getAvailability()).andReturn(
AvailabilityTimeLine.allValid()).anyTimes();
replay(this.calendar);
}
private IAnswer<Integer> toHoursAnswer(final int hours) {
final IAnswer<? extends EffortDuration> durationOnAnswer = asDurationOnAnswer(hours(hours));
return new IAnswer<Integer>() {
@Override
public Integer answer() throws Throwable {
return durationOnAnswer.answer().roundToHours();
}
};
}
private IAnswer<? extends EffortDuration> asDurationOnAnswer(
final EffortDuration duration) {
return new IAnswer<EffortDuration>() {
@Override
public EffortDuration answer() throws Throwable {
ResourcesPerDay perDay = (ResourcesPerDay) EasyMock
.getCurrentArguments()[1];
return perDay.asDurationGivenWorkingDayOf(
EffortDuration.hours(hours)).roundToHours();
return perDay.asDurationGivenWorkingDayOf(duration);
}
};
}
@ -116,22 +132,32 @@ public class SpecificResourceAllocationTest {
return EffortDuration.hours(defaultAnswer);
}
}).anyTimes();
final IAnswer<Integer> toHoursAnswer = new IAnswer<Integer>() {
@Override
public Integer answer() throws Throwable {
LocalDate date = (LocalDate) EasyMock
.getCurrentArguments()[0];
int hours;
if (answersForDates.containsKey(date)) {
hours = answersForDates.get(date);
} else {
hours = defaultAnswer;
}
return toHoursAnswer(hours).answer();
}
};
expect(
this.calendar.toHours(isA(LocalDate.class),
isA(ResourcesPerDay.class))).andAnswer(toHoursAnswer)
.anyTimes();
expect(
this.calendar.asDurationOn(isA(LocalDate.class),
isA(ResourcesPerDay.class))).andAnswer(
new IAnswer<Integer>() {
new IAnswer<EffortDuration>() {
@Override
public Integer answer() throws Throwable {
LocalDate date = (LocalDate) EasyMock
.getCurrentArguments()[0];
int hours;
if (answersForDates.containsKey(date)) {
hours = answersForDates.get(date);
} else {
hours = defaultAnswer;
}
return toHoursAnswer(hours).answer();
public EffortDuration answer() throws Throwable {
return hours(toHoursAnswer.answer());
}
}).anyTimes();
expect(this.calendar.getAvailability()).andReturn(