001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.algorithm.buffer;
051:
052: import java.io.Serializable;
053: import java.util.ArrayList;
054: import java.util.Iterator;
055: import java.util.ListIterator;
056: import org.apache.commons.lang.time.DateUtils;
057: import com.projity.pm.calendar.WorkCalendar;
058:
059: /**
060: * Calculated values that are grouped by time buckets, such as a histogram
061: */
062: public class GroupedCalculatedValues implements CalculatedValues,
063: Serializable {
064: static final long serialVersionUID = 8900927827L;
065: ArrayList values = new ArrayList(); //(x,y pairs) //TODO a set would be better because this is often sparse
066: double yScale;
067: private static final Double ZERO = new Double(0.0D);
068:
069: // boolean dayByDay;
070: /**
071: *
072: */
073: public GroupedCalculatedValues(double yScale) {
074: super ();
075: this .yScale = yScale;
076: }
077:
078: public GroupedCalculatedValues() {
079: this (1.0D);
080: }
081:
082: public int size() {
083: return values.size();
084: }
085:
086: public void set(final int index, final long date,
087: final long endDate, final double value,
088: final WorkCalendar assignmentCalendar) {
089: if (date == 0)
090: return;
091: Point point;
092: if (index > values.size() - 1) {
093: // System.out.println("add index " + index + new java.util.Date(date) + " - " + new java.util.Date(endDate) + " value " + value);
094: values.add(index, new Point(date, value));
095: } else {
096: point = (Point) values.get(index);
097: if (point == null) {
098: values.set(index, new Point(date, value));
099: // System.out.println("add indexb " + index + new java.util.Date(date) + " - " + new java.util.Date(endDate) + " value " + value);
100: } else {
101: point.addValue(value);
102: // System.out.println("add value " + index + new java.util.Date(date) + " - " + new java.util.Date(endDate) + " value " + value);
103: }
104: }
105: }
106:
107: public Long getDate(int index) {
108: Point point = (Point) values.get(index);
109: if (point == null)
110: return null;
111: return new Long(point.date);
112: }
113:
114: public void setValue(int index, double value) {
115: Point point = (Point) values.get(index);
116: if (point == null)
117: return;
118: point.value = value;
119:
120: }
121:
122: final public double getUnscaledValue(int index) {
123: if (values.isEmpty()) {
124: System.out
125: .println("empty values in GroupedCalculatedValues");
126: return 0;
127: } else if (index >= values.size()) {
128: System.out
129: .println("index out of bounds in GroupedCalculatedValues "
130: + index);
131: return 0;
132: }
133: Point point = (Point) values.get(index);
134: if (point == null)
135: return 0;
136: return point.value;
137: }
138:
139: public Double getValue(int index) {
140: if (values.isEmpty()) {
141: System.out
142: .println("empty values in GroupedCalculatedValues");
143: return ZERO;
144: } else if (index >= values.size()) {
145: System.out
146: .println("index out of bounds in GroupedCalculatedValues "
147: + index);
148: return ZERO;
149: }
150: Point point = (Point) values.get(index);
151: if (point == null)
152: return null;
153: return new Double(point.value / yScale);
154: }
155:
156: public void makeSeries(boolean cumulative, SeriesCallback callback) {
157: Long[] d = new Long[values.size()];
158: Double[] v = new Double[values.size()];
159: Point point = null;
160: //long lastDate=-10L;
161: double sum = 0;
162: //int deltai=0;
163: for (int i = 0; i < values.size(); i++) {
164: point = (Point) values.get(i);
165: // lc hack to enable day by day values
166: // if (dayByDay&&point.date-lastDate>DateUtils.MILLIS_PER_DAY+12*DateUtils.MILLIS_PER_HOUR){
167: // if (lastDate>0L&&point.date-lastDate>2*DateUtils.MILLIS_PER_DAY+12*DateUtils.MILLIS_PER_HOUR)
168: // callback.add(i+deltai++,lastDate+DateUtils.MILLIS_PER_DAY,0.0);
169: // callback.add(i+deltai++,point.date-DateUtils.MILLIS_PER_DAY,0.0);
170: // }
171: callback.add(i/*+deltai*/, point.date, point.value
172: + (cumulative ? sum : 0));
173: sum += point.value;
174: // lastDate=point.date;
175: }
176: // if (dayByDay&&point!=null) callback.add(values.size()+deltai,point.date+DateUtils.MILLIS_PER_DAY,0.0);
177: }
178:
179: public void makeRectilinearSeries(SeriesCallback callback) {
180: double previous = 0.0D;
181: Point point;
182: for (int i = 0; i < values.size(); i++) {
183: point = (Point) values.get(i);
184: callback.add(2 * i, point.getDate(), previous);
185: previous = point.getValue();
186: callback.add(2 * i + 1, point.getDate(), previous);
187: }
188: }
189:
190: /**
191: * Transforms values into cumulative values or back to non cumulative
192: *
193: */
194: public void makeCumulative(boolean cumulative) {
195: double sum = 0;
196: Point point;
197: for (int i = 0; i < values.size(); i++) {
198: point = (Point) values.get(i);
199: if (cumulative) {
200: sum += point.value;
201: point.value = sum;
202: } else {
203: point.value -= sum;
204: sum += point.value;
205: }
206: }
207:
208: }
209:
210: public void dump() {
211: for (int i = 0; i < values.size(); i++)
212: System.out.println(i + " "
213: + new java.util.Date(getDate(i).longValue()) + " "
214: + getValue(i));
215: }
216:
217: public ListIterator iterator(int index) {
218: return values.listIterator(index);
219: }
220:
221: public static GroupedCalculatedValues union(
222: GroupedCalculatedValues values1,
223: GroupedCalculatedValues values2) {
224: GroupedCalculatedValues c1, c2;
225: if (values1.size() >= values2.size()) {
226: c1 = values1;
227: c2 = values2;
228: } else {
229: c1 = values2;
230: c2 = values1;
231: }
232: GroupedCalculatedValues c = new GroupedCalculatedValues();
233: ListIterator i1 = c1.values.listIterator();
234: ListIterator i2 = c2.values.listIterator();
235: Point p1, p2 = null;
236: while (i1.hasNext()) {
237: p1 = (Point) i1.next();
238: while (i2.hasNext()) {
239: p2 = (Point) i2.next();
240: if (p2.date < p1.date) {
241: c.values.add(p2);
242: } else if (p2.date > p1.date) {
243: i2.previous();
244: break;
245: } else
246: break;
247: }
248: if (p2 != null && p1.date == p2.date)
249: c.values.add(new Point(p1.date, p1.value + p2.value));
250: else
251: c.values.add(p1);
252: }
253: while (i2.hasNext()) {
254: c.values.add((Point) i2.next());
255: }
256: return c;
257: }
258:
259: public void mergeIn(GroupedCalculatedValues add) {
260: ListIterator baseIterator = values.listIterator();
261: ListIterator addIterator = add.values.listIterator();
262: Point basePoint = baseIterator.hasNext() ? (Point) baseIterator
263: .next() : null;
264: long start = basePoint.date;
265: Point previousAddPoint = null;
266: Point addPoint = addIterator.hasNext() ? (Point) addIterator
267: .next() : null;
268: while (basePoint != null && addPoint != null) {
269: //TODO handle overlaps
270: if (basePoint.compareTo(addPoint) >= 0) {
271: if (addPoint.date >= start) {
272: basePoint.value += addPoint.value;
273: if (basePoint.date == start
274: && previousAddPoint != null) { // if first time
275: double proratedAmount = ((double) addPoint.date - start)
276: / (addPoint.date - previousAddPoint.date);
277: if (proratedAmount > 0)
278: basePoint.value += (previousAddPoint.value * proratedAmount);
279: }
280: }
281: previousAddPoint = addPoint;
282: addPoint = addIterator.hasNext() ? (Point) addIterator
283: .next() : null;
284: continue;
285: }
286:
287: if (baseIterator.hasNext()) {
288: basePoint = (Point) baseIterator.next();
289: } else {
290: if (previousAddPoint != null) {// handle end boundary
291: double proratedAmount = ((double) (basePoint.date - previousAddPoint.date))
292: / (addPoint.date - previousAddPoint.date);
293: if (proratedAmount > 0)
294: basePoint.value += (addPoint.value * proratedAmount);
295: }
296: basePoint = null;
297: }
298: }
299: }
300:
301: public GroupedCalculatedValues dayByDayConvert() {
302: GroupedCalculatedValues c = new GroupedCalculatedValues();
303: //c.setDayByDay(true);
304: for (Iterator i = values.iterator(); i.hasNext();) {
305: Point p = (Point) i.next();
306: c.values.add(new Point(p.date, p.value
307: * DateUtils.MILLIS_PER_HOUR));
308: }
309: return c;
310: }
311:
312: public final ArrayList getValues() {
313: return values;
314: }
315:
316: }
|