From 97470cf0c406eefeefcb2e593469e4d18d27d3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Sat, 19 Dec 2009 20:45:19 +0100 Subject: [PATCH] ItEr39S16CUConfiguracionMaquinasItEr35S09: Extracting class with logic for distributing some hours to several resources on some day --- .../entities/GenericResourceAllocation.java | 48 ++-------- .../planner/entities/HoursDistributor.java | 95 +++++++++++++++++++ 2 files changed, 103 insertions(+), 40 deletions(-) create mode 100644 navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/HoursDistributor.java diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java index 9ae1641ef..ea12dfe5e 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java @@ -32,7 +32,7 @@ import java.util.Set; import org.apache.commons.lang.Validate; import org.joda.time.LocalDate; import org.navalplanner.business.calendars.entities.IWorkHours; -import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay; +import org.navalplanner.business.planner.entities.HoursDistributor.ResourceWithAssignedHours; import org.navalplanner.business.planner.entities.allocationalgorithms.HoursModification; import org.navalplanner.business.planner.entities.allocationalgorithms.ResourcesPerDayModification; import org.navalplanner.business.resources.entities.Criterion; @@ -130,57 +130,25 @@ public class GenericResourceAllocation extends private class GenericAllocation extends AssignmentsAllocation { - private final List resources; - - private final List workHours; + private HoursDistributor hoursDistributor; public GenericAllocation(List resources) { - this.resources = resources; - this.workHours = new ArrayList(); - for (Resource resource : resources) { - this.workHours.add(generateWorkHoursFor(resource)); - } - } - - private final IWorkHours generateWorkHoursFor(Resource resource) { - if (resource.getCalendar() != null) { - return resource.getCalendar(); - } else { - return SameWorkHoursEveryDay.getDefaultWorkingDay(); - } + hoursDistributor = new HoursDistributor( + resources); } @Override protected List distributeForDay(LocalDate day, int totalHours) { List result = new ArrayList(); - List shares = currentSharesFor(day); - ShareDivision currentDivision = ShareDivision.create(shares); - ShareDivision newDivison = currentDivision.plus(totalHours); - int[] differences = currentDivision.to(newDivison); - for (int i = 0; i < differences.length; i++) { - assert differences[i] >= 0; - GenericDayAssignment dayAssignment = GenericDayAssignment - .create(day, differences[i], resources.get(i)); - result.add(dayAssignment); + for (ResourceWithAssignedHours each : hoursDistributor + .distributeForDay(day, totalHours)) { + result.add(GenericDayAssignment.create(day, each.hours, + each.resource)); } return result; } - private List currentSharesFor(LocalDate day) { - List shares = new ArrayList(); - for (int i = 0; i < resources.size(); i++) { - Resource resource = resources.get(i); - IWorkHours workHoursForResource = workHours.get(i); - int alreadyAssignedHours = resource.getAssignedHours(day); - Integer workableHours = workHoursForResource - .getWorkableHours(day); - // a resource would have a zero share if all it's hours for a - // given day are filled - shares.add(new Share(alreadyAssignedHours - workableHours)); - } - return shares; - } } @Override diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/HoursDistributor.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/HoursDistributor.java new file mode 100644 index 000000000..30e9cf072 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/HoursDistributor.java @@ -0,0 +1,95 @@ +/* + * This file is part of ###PROJECT_NAME### + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.navalplanner.business.planner.entities; + +import java.util.ArrayList; +import java.util.List; + +import org.joda.time.LocalDate; +import org.navalplanner.business.calendars.entities.IWorkHours; +import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay; +import org.navalplanner.business.resources.entities.Resource; + +/** + * @author Óscar González Fernández + */ +public class HoursDistributor { + + public static class ResourceWithAssignedHours { + public final Integer hours; + + public final Resource resource; + + private ResourceWithAssignedHours(Integer hours, Resource resource) { + this.hours = hours; + this.resource = resource; + } + } + + private final List resources; + + private final List workHours; + + public HoursDistributor(List resources) { + this.resources = resources; + this.workHours = new ArrayList(); + for (Resource resource : resources) { + this.workHours.add(generateWorkHoursFor(resource)); + } + } + + private final IWorkHours generateWorkHoursFor(Resource resource) { + if (resource.getCalendar() != null) { + return resource.getCalendar(); + } else { + return SameWorkHoursEveryDay.getDefaultWorkingDay(); + } + } + + public List distributeForDay(LocalDate day, + int totalHours) { + List result = new ArrayList(); + List shares = currentSharesFor(day); + ShareDivision currentDivision = ShareDivision.create(shares); + ShareDivision newDivison = currentDivision.plus(totalHours); + int[] differences = currentDivision.to(newDivison); + for (int i = 0; i < differences.length; i++) { + assert differences[i] >= 0; + result.add(new ResourceWithAssignedHours(differences[i], resources + .get(i))); + } + return result; + } + + public List currentSharesFor(LocalDate day) { + List shares = new ArrayList(); + for (int i = 0; i < resources.size(); i++) { + Resource resource = resources.get(i); + IWorkHours workHoursForResource = workHours.get(i); + int alreadyAssignedHours = resource.getAssignedHours(day); + Integer workableHours = workHoursForResource.getWorkableHours(day); + // a resource would have a zero share if all it's hours for a + // given day are filled + shares.add(new Share(alreadyAssignedHours - workableHours)); + } + return shares; + } + +}