001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import net.sf.jasperreports.engine.JRVariable;
031:
032: /**
033: * @author Teodor Danciu (teodord@users.sourceforge.net)
034: * @version $Id: JRDoubleIncrementerFactory.java 1347 2006-07-19 12:31:07Z teodord $
035: */
036: public class JRDoubleIncrementerFactory extends
037: JRAbstractExtendedIncrementerFactory {
038:
039: /**
040: *
041: */
042: protected static final Double ZERO = new Double(0);
043:
044: /**
045: *
046: */
047: private static JRDoubleIncrementerFactory mainInstance = new JRDoubleIncrementerFactory();
048:
049: /**
050: *
051: */
052: private JRDoubleIncrementerFactory() {
053: }
054:
055: /**
056: *
057: */
058: public static JRDoubleIncrementerFactory getInstance() {
059: return mainInstance;
060: }
061:
062: /**
063: *
064: */
065: public JRExtendedIncrementer getExtendedIncrementer(byte calculation) {
066: JRExtendedIncrementer incrementer = null;
067:
068: switch (calculation) {
069: case JRVariable.CALCULATION_COUNT: {
070: incrementer = JRDoubleCountIncrementer.getInstance();
071: break;
072: }
073: case JRVariable.CALCULATION_SUM: {
074: incrementer = JRDoubleSumIncrementer.getInstance();
075: break;
076: }
077: case JRVariable.CALCULATION_AVERAGE: {
078: incrementer = JRDoubleAverageIncrementer.getInstance();
079: break;
080: }
081: case JRVariable.CALCULATION_LOWEST:
082: case JRVariable.CALCULATION_HIGHEST: {
083: incrementer = JRComparableIncrementerFactory.getInstance()
084: .getExtendedIncrementer(calculation);
085: break;
086: }
087: case JRVariable.CALCULATION_STANDARD_DEVIATION: {
088: incrementer = JRDoubleStandardDeviationIncrementer
089: .getInstance();
090: break;
091: }
092: case JRVariable.CALCULATION_VARIANCE: {
093: incrementer = JRDoubleVarianceIncrementer.getInstance();
094: break;
095: }
096: case JRVariable.CALCULATION_DISTINCT_COUNT: {
097: incrementer = JRDoubleDistinctCountIncrementer
098: .getInstance();
099: break;
100: }
101: case JRVariable.CALCULATION_SYSTEM:
102: case JRVariable.CALCULATION_NOTHING:
103: case JRVariable.CALCULATION_FIRST:
104: default: {
105: incrementer = JRDefaultIncrementerFactory.getInstance()
106: .getExtendedIncrementer(calculation);
107: break;
108: }
109: }
110:
111: return incrementer;
112: }
113:
114: }
115:
116: /**
117: *
118: */
119: class JRDoubleCountIncrementer extends JRAbstractExtendedIncrementer {
120: /**
121: *
122: */
123: private static JRDoubleCountIncrementer mainInstance = new JRDoubleCountIncrementer();
124:
125: /**
126: *
127: */
128: private JRDoubleCountIncrementer() {
129: }
130:
131: /**
132: *
133: */
134: public static JRDoubleCountIncrementer getInstance() {
135: return mainInstance;
136: }
137:
138: /**
139: *
140: */
141: public Object increment(JRCalculable variable,
142: Object expressionValue, AbstractValueProvider valueProvider) {
143: Number value = (Number) variable.getIncrementedValue();
144:
145: if (value == null || variable.isInitialized()) {
146: value = JRDoubleIncrementerFactory.ZERO;
147: }
148:
149: if (expressionValue == null) {
150: return value;
151: }
152:
153: return new Double(value.doubleValue() + 1);
154: }
155:
156: public Object combine(JRCalculable calculable,
157: JRCalculable calculableValue,
158: AbstractValueProvider valueProvider) {
159: Number value = (Number) calculable.getIncrementedValue();
160: Number combineValue = (Number) calculableValue.getValue();
161:
162: if (value == null || calculable.isInitialized()) {
163: value = JRDoubleIncrementerFactory.ZERO;
164: }
165:
166: if (combineValue == null) {
167: return value;
168: }
169:
170: return new Double(value.doubleValue()
171: + combineValue.doubleValue());
172: }
173:
174: public Object initialValue() {
175: return JRDoubleIncrementerFactory.ZERO;
176: }
177: }
178:
179: /**
180: *
181: */
182: class JRDoubleDistinctCountIncrementer extends
183: JRAbstractExtendedIncrementer {
184: /**
185: *
186: */
187: private static JRDoubleDistinctCountIncrementer mainInstance = new JRDoubleDistinctCountIncrementer();
188:
189: /**
190: *
191: */
192: private JRDoubleDistinctCountIncrementer() {
193: }
194:
195: /**
196: *
197: */
198: public static JRDoubleDistinctCountIncrementer getInstance() {
199: return mainInstance;
200: }
201:
202: /**
203: *
204: */
205: public Object increment(JRCalculable variable,
206: Object expressionValue, AbstractValueProvider valueProvider) {
207: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
208: .getValue(variable
209: .getHelperVariable(JRCalculable.HELPER_COUNT));
210:
211: if (variable.isInitialized()) {
212: holder.init();
213: }
214:
215: return new Double(holder.getCount());
216: }
217:
218: public Object combine(JRCalculable calculable,
219: JRCalculable calculableValue,
220: AbstractValueProvider valueProvider) {
221: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
222: .getValue(calculable
223: .getHelperVariable(JRCalculable.HELPER_COUNT));
224:
225: return new Double(holder.getCount());
226: }
227:
228: public Object initialValue() {
229: return JRDoubleIncrementerFactory.ZERO;
230: }
231: }
232:
233: /**
234: *
235: */
236: class JRDoubleSumIncrementer extends JRAbstractExtendedIncrementer {
237: /**
238: *
239: */
240: private static JRDoubleSumIncrementer mainInstance = new JRDoubleSumIncrementer();
241:
242: /**
243: *
244: */
245: private JRDoubleSumIncrementer() {
246: }
247:
248: /**
249: *
250: */
251: public static JRDoubleSumIncrementer getInstance() {
252: return mainInstance;
253: }
254:
255: /**
256: *
257: */
258: public Object increment(JRCalculable variable,
259: Object expressionValue, AbstractValueProvider valueProvider) {
260: Number value = (Number) variable.getIncrementedValue();
261: Number newValue = (Number) expressionValue;
262:
263: if (newValue == null) {
264: if (variable.isInitialized()) {
265: return null;
266: }
267:
268: return value;
269: }
270:
271: if (value == null || variable.isInitialized()) {
272: value = JRDoubleIncrementerFactory.ZERO;
273: }
274:
275: return new Double(value.doubleValue() + newValue.doubleValue());
276: }
277:
278: public Object initialValue() {
279: return JRDoubleIncrementerFactory.ZERO;
280: }
281: }
282:
283: /**
284: *
285: */
286: class JRDoubleAverageIncrementer extends JRAbstractExtendedIncrementer {
287: /**
288: *
289: */
290: private static JRDoubleAverageIncrementer mainInstance = new JRDoubleAverageIncrementer();
291:
292: /**
293: *
294: */
295: private JRDoubleAverageIncrementer() {
296: }
297:
298: /**
299: *
300: */
301: public static JRDoubleAverageIncrementer getInstance() {
302: return mainInstance;
303: }
304:
305: /**
306: *
307: */
308: public Object increment(JRCalculable variable,
309: Object expressionValue, AbstractValueProvider valueProvider) {
310: if (expressionValue == null) {
311: if (variable.isInitialized()) {
312: return null;
313: }
314: return variable.getValue();
315: }
316: Number countValue = (Number) valueProvider.getValue(variable
317: .getHelperVariable(JRCalculable.HELPER_COUNT));
318: Number sumValue = (Number) valueProvider.getValue(variable
319: .getHelperVariable(JRCalculable.HELPER_SUM));
320: return new Double(sumValue.doubleValue()
321: / countValue.doubleValue());
322: }
323:
324: public Object initialValue() {
325: return JRDoubleIncrementerFactory.ZERO;
326: }
327: }
328:
329: /**
330: *
331: */
332: class JRDoubleStandardDeviationIncrementer extends
333: JRAbstractExtendedIncrementer {
334: /**
335: *
336: */
337: private static JRDoubleStandardDeviationIncrementer mainInstance = new JRDoubleStandardDeviationIncrementer();
338:
339: /**
340: *
341: */
342: private JRDoubleStandardDeviationIncrementer() {
343: }
344:
345: /**
346: *
347: */
348: public static JRDoubleStandardDeviationIncrementer getInstance() {
349: return mainInstance;
350: }
351:
352: /**
353: *
354: */
355: public Object increment(JRCalculable variable,
356: Object expressionValue, AbstractValueProvider valueProvider) {
357: if (expressionValue == null) {
358: if (variable.isInitialized()) {
359: return null;
360: }
361: return variable.getValue();
362: }
363: Number varianceValue = (Number) valueProvider.getValue(variable
364: .getHelperVariable(JRCalculable.HELPER_VARIANCE));
365: return new Double(Math.sqrt(varianceValue.doubleValue()));
366: }
367:
368: public Object initialValue() {
369: return JRDoubleIncrementerFactory.ZERO;
370: }
371: }
372:
373: /**
374: *
375: */
376: class JRDoubleVarianceIncrementer extends JRAbstractExtendedIncrementer {
377: /**
378: *
379: */
380: private static JRDoubleVarianceIncrementer mainInstance = new JRDoubleVarianceIncrementer();
381:
382: /**
383: *
384: */
385: private JRDoubleVarianceIncrementer() {
386: }
387:
388: /**
389: *
390: */
391: public static JRDoubleVarianceIncrementer getInstance() {
392: return mainInstance;
393: }
394:
395: /**
396: *
397: */
398: public Object increment(JRCalculable variable,
399: Object expressionValue, AbstractValueProvider valueProvider) {
400: Number value = (Number) variable.getIncrementedValue();
401: Number newValue = (Number) expressionValue;
402:
403: if (newValue == null) {
404: if (variable.isInitialized()) {
405: return null;
406: }
407: return value;
408: } else if (value == null || variable.isInitialized()) {
409: return JRDoubleIncrementerFactory.ZERO;
410: } else {
411: Number countValue = (Number) valueProvider
412: .getValue(variable
413: .getHelperVariable(JRCalculable.HELPER_COUNT));
414: Number sumValue = (Number) valueProvider.getValue(variable
415: .getHelperVariable(JRCalculable.HELPER_SUM));
416: return new Double((countValue.doubleValue() - 1)
417: * value.doubleValue()
418: / countValue.doubleValue()
419: + (sumValue.doubleValue()
420: / countValue.doubleValue() - newValue
421: .doubleValue())
422: * (sumValue.doubleValue()
423: / countValue.doubleValue() - newValue
424: .doubleValue())
425: / (countValue.doubleValue() - 1));
426: }
427: }
428:
429: public Object combine(JRCalculable calculable,
430: JRCalculable calculableValue,
431: AbstractValueProvider valueProvider) {
432: Number value = (Number) calculable.getIncrementedValue();
433:
434: if (calculableValue.getValue() == null) {
435: if (calculable.isInitialized()) {
436: return null;
437: }
438:
439: return value;
440: } else if (value == null || calculable.isInitialized()) {
441: return new Double(((Number) calculableValue
442: .getIncrementedValue()).doubleValue());
443: }
444:
445: double v1 = value.doubleValue();
446: double c1 = ((Number) valueProvider.getValue(calculable
447: .getHelperVariable(JRCalculable.HELPER_COUNT)))
448: .doubleValue();
449: double s1 = ((Number) valueProvider.getValue(calculable
450: .getHelperVariable(JRCalculable.HELPER_SUM)))
451: .doubleValue();
452:
453: double v2 = ((Number) calculableValue.getIncrementedValue())
454: .doubleValue();
455: double c2 = ((Number) valueProvider.getValue(calculableValue
456: .getHelperVariable(JRCalculable.HELPER_COUNT)))
457: .doubleValue();
458: double s2 = ((Number) valueProvider.getValue(calculableValue
459: .getHelperVariable(JRCalculable.HELPER_SUM)))
460: .doubleValue();
461:
462: c1 -= c2;
463: s1 -= s2;
464:
465: double c = c1 + c2;
466:
467: return new Double(c1 / c * v1 + c2 / c * v2 + c2 / c1 * s1 / c
468: * s1 / c + c1 / c2 * s2 / c * s2 / c - 2 * s1 / c * s2
469: / c);
470: }
471:
472: public Object initialValue() {
473: return JRDoubleIncrementerFactory.ZERO;
474: }
475: }
|