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: JRFloatIncrementerFactory.java 1347 2006-07-19 12:31:07Z teodord $
035: */
036: public class JRFloatIncrementerFactory extends
037: JRAbstractExtendedIncrementerFactory {
038:
039: /**
040: *
041: */
042: protected static final Float ZERO = new Float(0);
043:
044: /**
045: *
046: */
047: private static JRFloatIncrementerFactory mainInstance = new JRFloatIncrementerFactory();
048:
049: /**
050: *
051: */
052: public JRFloatIncrementerFactory() {
053: }
054:
055: /**
056: *
057: */
058: public static JRFloatIncrementerFactory 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 = JRFloatCountIncrementer.getInstance();
071: break;
072: }
073: case JRVariable.CALCULATION_SUM: {
074: incrementer = JRFloatSumIncrementer.getInstance();
075: break;
076: }
077: case JRVariable.CALCULATION_AVERAGE: {
078: incrementer = JRFloatAverageIncrementer.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 = JRFloatStandardDeviationIncrementer
089: .getInstance();
090: break;
091: }
092: case JRVariable.CALCULATION_VARIANCE: {
093: incrementer = JRFloatVarianceIncrementer.getInstance();
094: break;
095: }
096: case JRVariable.CALCULATION_DISTINCT_COUNT: {
097: incrementer = JRFloatDistinctCountIncrementer.getInstance();
098: break;
099: }
100: case JRVariable.CALCULATION_SYSTEM:
101: case JRVariable.CALCULATION_NOTHING:
102: case JRVariable.CALCULATION_FIRST:
103: default: {
104: incrementer = JRDefaultIncrementerFactory.getInstance()
105: .getExtendedIncrementer(calculation);
106: break;
107: }
108: }
109:
110: return incrementer;
111: }
112:
113: }
114:
115: /**
116: *
117: */
118: class JRFloatCountIncrementer extends JRAbstractExtendedIncrementer {
119: /**
120: *
121: */
122: private static JRFloatCountIncrementer mainInstance = new JRFloatCountIncrementer();
123:
124: /**
125: *
126: */
127: private JRFloatCountIncrementer() {
128: }
129:
130: /**
131: *
132: */
133: public static JRFloatCountIncrementer getInstance() {
134: return mainInstance;
135: }
136:
137: /**
138: *
139: */
140: public Object increment(JRCalculable variable,
141: Object expressionValue, AbstractValueProvider valueProvider) {
142: Number value = (Number) variable.getIncrementedValue();
143:
144: if (value == null || variable.isInitialized()) {
145: value = JRFloatIncrementerFactory.ZERO;
146: }
147:
148: if (expressionValue == null) {
149: return value;
150: }
151:
152: return new Float(value.floatValue() + 1);
153: }
154:
155: public Object combine(JRCalculable calculable,
156: JRCalculable calculableValue,
157: AbstractValueProvider valueProvider) {
158: Number value = (Number) calculable.getIncrementedValue();
159: Number combineValue = (Number) calculableValue.getValue();
160:
161: if (value == null || calculable.isInitialized()) {
162: value = JRFloatIncrementerFactory.ZERO;
163: }
164:
165: if (combineValue == null) {
166: return value;
167: }
168:
169: return new Float(value.floatValue() + combineValue.floatValue());
170: }
171:
172: public Object initialValue() {
173: return JRFloatIncrementerFactory.ZERO;
174: }
175: }
176:
177: /**
178: *
179: */
180: class JRFloatDistinctCountIncrementer extends
181: JRAbstractExtendedIncrementer {
182: /**
183: *
184: */
185: private static JRFloatDistinctCountIncrementer mainInstance = new JRFloatDistinctCountIncrementer();
186:
187: /**
188: *
189: */
190: private JRFloatDistinctCountIncrementer() {
191: }
192:
193: /**
194: *
195: */
196: public static JRFloatDistinctCountIncrementer getInstance() {
197: return mainInstance;
198: }
199:
200: /**
201: *
202: */
203: public Object increment(JRCalculable variable,
204: Object expressionValue, AbstractValueProvider valueProvider) {
205: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
206: .getValue(variable
207: .getHelperVariable(JRCalculable.HELPER_COUNT));
208:
209: if (variable.isInitialized()) {
210: holder.init();
211: }
212:
213: return new Float(holder.getCount());
214: }
215:
216: public Object combine(JRCalculable calculable,
217: JRCalculable calculableValue,
218: AbstractValueProvider valueProvider) {
219: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
220: .getValue(calculable
221: .getHelperVariable(JRCalculable.HELPER_COUNT));
222:
223: return new Float(holder.getCount());
224: }
225:
226: public Object initialValue() {
227: return JRFloatIncrementerFactory.ZERO;
228: }
229: }
230:
231: /**
232: *
233: */
234: class JRFloatSumIncrementer extends JRAbstractExtendedIncrementer {
235: /**
236: *
237: */
238: private static JRFloatSumIncrementer mainInstance = new JRFloatSumIncrementer();
239:
240: /**
241: *
242: */
243: private JRFloatSumIncrementer() {
244: }
245:
246: /**
247: *
248: */
249: public static JRFloatSumIncrementer getInstance() {
250: return mainInstance;
251: }
252:
253: /**
254: *
255: */
256: public Object increment(JRCalculable variable,
257: Object expressionValue, AbstractValueProvider valueProvider) {
258: Number value = (Number) variable.getIncrementedValue();
259: Number newValue = (Number) expressionValue;
260:
261: if (newValue == null) {
262: if (variable.isInitialized()) {
263: return null;
264: }
265:
266: return value;
267: }
268:
269: if (value == null || variable.isInitialized()) {
270: value = JRFloatIncrementerFactory.ZERO;
271: }
272:
273: return new Float(value.floatValue() + newValue.floatValue());
274: }
275:
276: public Object initialValue() {
277: return JRFloatIncrementerFactory.ZERO;
278: }
279: }
280:
281: /**
282: *
283: */
284: class JRFloatAverageIncrementer extends JRAbstractExtendedIncrementer {
285: /**
286: *
287: */
288: private static JRFloatAverageIncrementer mainInstance = new JRFloatAverageIncrementer();
289:
290: /**
291: *
292: */
293: private JRFloatAverageIncrementer() {
294: }
295:
296: /**
297: *
298: */
299: public static JRFloatAverageIncrementer getInstance() {
300: return mainInstance;
301: }
302:
303: /**
304: *
305: */
306: public Object increment(JRCalculable variable,
307: Object expressionValue, AbstractValueProvider valueProvider) {
308: if (expressionValue == null) {
309: if (variable.isInitialized()) {
310: return null;
311: }
312: return variable.getValue();
313: }
314: Number countValue = (Number) valueProvider.getValue(variable
315: .getHelperVariable(JRCalculable.HELPER_COUNT));
316: Number sumValue = (Number) valueProvider.getValue(variable
317: .getHelperVariable(JRCalculable.HELPER_SUM));
318: return new Float(sumValue.floatValue()
319: / countValue.floatValue());
320: }
321:
322: public Object initialValue() {
323: return JRFloatIncrementerFactory.ZERO;
324: }
325: }
326:
327: /**
328: *
329: */
330: class JRFloatStandardDeviationIncrementer extends
331: JRAbstractExtendedIncrementer {
332: /**
333: *
334: */
335: private static JRFloatStandardDeviationIncrementer mainInstance = new JRFloatStandardDeviationIncrementer();
336:
337: /**
338: *
339: */
340: private JRFloatStandardDeviationIncrementer() {
341: }
342:
343: /**
344: *
345: */
346: public static JRFloatStandardDeviationIncrementer getInstance() {
347: return mainInstance;
348: }
349:
350: /**
351: *
352: */
353: public Object increment(JRCalculable variable,
354: Object expressionValue, AbstractValueProvider valueProvider) {
355: if (expressionValue == null) {
356: if (variable.isInitialized()) {
357: return null;
358: }
359: return variable.getValue();
360: }
361: Number varianceValue = (Number) valueProvider.getValue(variable
362: .getHelperVariable(JRCalculable.HELPER_VARIANCE));
363: return new Float(Math.sqrt(varianceValue.doubleValue()));
364: }
365:
366: public Object initialValue() {
367: return JRFloatIncrementerFactory.ZERO;
368: }
369: }
370:
371: /**
372: *
373: */
374: class JRFloatVarianceIncrementer extends JRAbstractExtendedIncrementer {
375: /**
376: *
377: */
378: private static JRFloatVarianceIncrementer mainInstance = new JRFloatVarianceIncrementer();
379:
380: /**
381: *
382: */
383: private JRFloatVarianceIncrementer() {
384: }
385:
386: /**
387: *
388: */
389: public static JRFloatVarianceIncrementer getInstance() {
390: return mainInstance;
391: }
392:
393: /**
394: *
395: */
396: public Object increment(JRCalculable variable,
397: Object expressionValue, AbstractValueProvider valueProvider) {
398: Number value = (Number) variable.getIncrementedValue();
399: Number newValue = (Number) expressionValue;
400:
401: if (newValue == null) {
402: if (variable.isInitialized()) {
403: return null;
404: }
405:
406: return value;
407: } else if (value == null || variable.isInitialized()) {
408: return JRFloatIncrementerFactory.ZERO;
409: } else {
410: Number countValue = (Number) valueProvider
411: .getValue(variable
412: .getHelperVariable(JRCalculable.HELPER_COUNT));
413: Number sumValue = (Number) valueProvider.getValue(variable
414: .getHelperVariable(JRCalculable.HELPER_SUM));
415: return new Float(
416: (countValue.floatValue() - 1)
417: * value.floatValue()
418: / countValue.floatValue()
419: + (sumValue.floatValue()
420: / countValue.floatValue() - newValue
421: .floatValue())
422: * (sumValue.floatValue()
423: / countValue.floatValue() - newValue
424: .floatValue())
425: / (countValue.floatValue() - 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 Float(((Number) calculableValue
442: .getIncrementedValue()).floatValue());
443: }
444:
445: float v1 = value.floatValue();
446: float c1 = ((Number) valueProvider.getValue(calculable
447: .getHelperVariable(JRCalculable.HELPER_COUNT)))
448: .floatValue();
449: float s1 = ((Number) valueProvider.getValue(calculable
450: .getHelperVariable(JRCalculable.HELPER_SUM)))
451: .floatValue();
452:
453: float v2 = ((Number) calculableValue.getIncrementedValue())
454: .floatValue();
455: float c2 = ((Number) valueProvider.getValue(calculableValue
456: .getHelperVariable(JRCalculable.HELPER_COUNT)))
457: .floatValue();
458: float s2 = ((Number) valueProvider.getValue(calculableValue
459: .getHelperVariable(JRCalculable.HELPER_SUM)))
460: .floatValue();
461:
462: c1 -= c2;
463: s1 -= s2;
464:
465: float c = c1 + c2;
466:
467: return new Float(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 JRFloatIncrementerFactory.ZERO;
474: }
475: }
|