ItEr37S17CUCalculoValorGanadoItEr36S19: Added method "calculatedValueForEveryDay" to ChartFiller class.

This commit is contained in:
Manuel Rego Casasnovas 2009-12-01 12:43:00 +01:00 committed by Javier Moran Rua
parent 373bd1a6b0
commit b0ff416c65
2 changed files with 140 additions and 0 deletions

View file

@ -23,6 +23,7 @@ package org.navalplanner.web.planner.chart;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@ -38,6 +39,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.ResourceCalendar;
import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay;
@ -480,4 +482,53 @@ public abstract class ChartFiller implements IChartFiller {
return result;
}
protected SortedMap<LocalDate, BigDecimal> calculatedValueForEveryDay(
SortedMap<LocalDate, BigDecimal> map, Date start, Date finish) {
return calculatedValueForEveryDay(map, new LocalDate(start),
new LocalDate(finish));
}
protected SortedMap<LocalDate, BigDecimal> calculatedValueForEveryDay(
SortedMap<LocalDate, BigDecimal> map, LocalDate start,
LocalDate finish) {
SortedMap<LocalDate, BigDecimal> result = new TreeMap<LocalDate, BigDecimal>();
LocalDate previousDay = start;
BigDecimal previousValue = BigDecimal.ZERO;
for (LocalDate day : map.keySet()) {
BigDecimal value = map.get(day);
fillValues(result, previousDay, day, previousValue, value);
previousDay = day;
previousValue = value;
}
if (previousDay.compareTo(finish) < 0) {
fillValues(result, previousDay, finish, previousValue,
previousValue);
}
return result;
}
private void fillValues(SortedMap<LocalDate, BigDecimal> map,
LocalDate firstDay, LocalDate lastDay, BigDecimal firstValue,
BigDecimal lastValue) {
Integer days = Days.daysBetween(firstDay, lastDay).getDays();
if (days > 0) {
BigDecimal ammount = lastValue.subtract(firstValue);
BigDecimal ammountPerDay = ammount.setScale(2).divide(
new BigDecimal(days), RoundingMode.DOWN);
BigDecimal value = firstValue.setScale(2);
for (LocalDate day = firstDay; day.compareTo(lastDay) <= 0; day = day
.plusDays(1)) {
map.put(day, value);
value = value.add(ammountPerDay);
}
}
}
}

View file

@ -0,0 +1,89 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.planner.chart;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.math.BigDecimal;
import java.util.SortedMap;
import java.util.TreeMap;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.zkforge.timeplot.Timeplot;
import org.zkoss.ganttz.util.Interval;
/**
* Tests for {@link ChartFiller}
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
public class ChartFillerTest {
private ChartFiller chartFiller = new ChartFiller() {
@Override
public void fillChart(Timeplot chart, Interval interval, Integer size) {
}
};
private static final LocalDate START_DAY = new LocalDate(2009, 12, 1);
private static final LocalDate FIRST_DAY = new LocalDate(2009, 12, 5);
private static final LocalDate LAST_DAY = new LocalDate(2009, 12, 15);
private static final LocalDate FINISH_DAY = new LocalDate(2009, 12, 30);
private SortedMap<LocalDate, BigDecimal> givenExampleMap() {
SortedMap<LocalDate, BigDecimal> result = new TreeMap<LocalDate, BigDecimal>();
result.put(FIRST_DAY, new BigDecimal(100));
result.put(LAST_DAY, new BigDecimal(150));
return result;
}
@Test
public void testCalculatedValueForEveryDay() {
SortedMap<LocalDate, BigDecimal> result = chartFiller
.calculatedValueForEveryDay(givenExampleMap(), START_DAY,
FINISH_DAY);
assertThat(result.get(START_DAY), equalTo(BigDecimal.ZERO.setScale(2)));
assertThat(result.get(START_DAY.plusDays(1)),
equalTo(new BigDecimal(25).setScale(2)));
assertThat(result.get(FIRST_DAY), equalTo(new BigDecimal(100)
.setScale(2)));
assertThat(result.get(FIRST_DAY.plusDays(1)), equalTo(new BigDecimal(
105).setScale(2)));
assertThat(result.get(LAST_DAY), equalTo(new BigDecimal(150)
.setScale(2)));
assertThat(result.get(LAST_DAY.plusDays(1)),
equalTo(new BigDecimal(150).setScale(2)));
assertThat(result.get(FINISH_DAY), equalTo(new BigDecimal(150)
.setScale(2)));
System.out.println("HERE");
}
}