diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ValleyFiller.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ValleyFiller.java new file mode 100644 index 000000000..ebc3b6496 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ValleyFiller.java @@ -0,0 +1,46 @@ +/* + * 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; + +/** + * It fills valleys in the discrete function represented by hours. A valley is + * the portion between two maximum points. Besides the last value must be the + * maximum for all the function + * @author Óscar González Fernández + */ +public class ValleyFiller { + + private ValleyFiller() { + } + + public static int[] fillValley(int... hours) { + int[] result = new int[hours.length]; + int lastMaximum = Integer.MIN_VALUE; + int globalMaximum = hours[hours.length - 1]; + for (int i = 0; i < hours.length; i++) { + if (hours[i] > lastMaximum) { + lastMaximum = hours[i]; + } + result[i] = Math.min(lastMaximum, globalMaximum); + } + return result; + } + +} diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/ValleyFillerTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/ValleyFillerTest.java new file mode 100644 index 000000000..2f3e3a5bd --- /dev/null +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/ValleyFillerTest.java @@ -0,0 +1,60 @@ +/* + * 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.test.planner.entities; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.navalplanner.business.planner.entities.ValleyFiller; + +/** + * @author Óscar González Fernández + * + */ +public class ValleyFillerTest { + + @Test + public void ifIsMonotonicIncreasingDoesntDoAnything() { + int[] param = { 2, 4, 8 }; + int[] result = ValleyFiller.fillValley(param); + assertThat(result, equalTo(param)); + } + + @Test + public void ifHasValleyItFillsIt() { + int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6, 10); + assertThat(result, equalTo(new int[] { 2, 4, 8, 8, 8, 10 })); + } + + @Test + public void ifEndsInValleyEndsAtLastValue() { + int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6); + assertThat(result, equalTo(new int[] { 2, 4, 6, 6, 6 })); + } + + @Test + public void ifHasSeveralValleysWorksOk() { + int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6, 7, 9, 8, 11); + int[] expectedResult = { 2, 4, 8, 8, 8, 8, 9, 9, 11 }; + assertThat(result, equalTo(expectedResult)); + } + +}