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: JRIntegerIncrementerFactory.java 1329 2006-07-07 15:42:53Z teodord $
035: */
036: public class JRIntegerIncrementerFactory extends
037: JRAbstractExtendedIncrementerFactory {
038:
039: /**
040: *
041: */
042: protected static final Integer ZERO = new Integer(0);
043:
044: /**
045: *
046: */
047: private static JRIntegerIncrementerFactory mainInstance = new JRIntegerIncrementerFactory();
048:
049: /**
050: *
051: */
052: private JRIntegerIncrementerFactory() {
053: }
054:
055: /**
056: *
057: */
058: public static JRIntegerIncrementerFactory 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 = JRIntegerCountIncrementer.getInstance();
071: break;
072: }
073: case JRVariable.CALCULATION_SUM: {
074: incrementer = JRIntegerSumIncrementer.getInstance();
075: break;
076: }
077: case JRVariable.CALCULATION_AVERAGE: {
078: incrementer = JRIntegerAverageIncrementer.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 = JRIntegerStandardDeviationIncrementer
089: .getInstance();
090: break;
091: }
092: case JRVariable.CALCULATION_VARIANCE: {
093: incrementer = JRIntegerVarianceIncrementer.getInstance();
094: break;
095: }
096: case JRVariable.CALCULATION_DISTINCT_COUNT: {
097: incrementer = JRIntegerDistinctCountIncrementer
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 JRIntegerCountIncrementer extends JRAbstractExtendedIncrementer {
120: /**
121: *
122: */
123: private static JRIntegerCountIncrementer mainInstance = new JRIntegerCountIncrementer();
124:
125: /**
126: *
127: */
128: private JRIntegerCountIncrementer() {
129: }
130:
131: /**
132: *
133: */
134: public static JRIntegerCountIncrementer 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 = JRIntegerIncrementerFactory.ZERO;
147: }
148:
149: if (expressionValue == null) {
150: return value;
151: }
152:
153: return new Integer(value.intValue() + 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 = JRIntegerIncrementerFactory.ZERO;
164: }
165:
166: if (combineValue == null) {
167: return value;
168: }
169:
170: return new Integer(value.intValue() + combineValue.intValue());
171: }
172:
173: public Object initialValue() {
174: return JRIntegerIncrementerFactory.ZERO;
175: }
176: }
177:
178: /**
179: *
180: */
181: class JRIntegerDistinctCountIncrementer extends
182: JRAbstractExtendedIncrementer {
183: /**
184: *
185: */
186: private static JRIntegerDistinctCountIncrementer mainInstance = new JRIntegerDistinctCountIncrementer();
187:
188: /**
189: *
190: */
191: private JRIntegerDistinctCountIncrementer() {
192: }
193:
194: /**
195: *
196: */
197: public static JRIntegerDistinctCountIncrementer getInstance() {
198: return mainInstance;
199: }
200:
201: /**
202: *
203: */
204: public Object increment(JRCalculable variable,
205: Object expressionValue, AbstractValueProvider valueProvider) {
206: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
207: .getValue(variable
208: .getHelperVariable(JRCalculable.HELPER_COUNT));
209:
210: if (variable.isInitialized()) {
211: holder.init();
212: }
213:
214: return new Integer((int) holder.getCount());
215: }
216:
217: public Object combine(JRCalculable calculable,
218: JRCalculable calculableValue,
219: AbstractValueProvider valueProvider) {
220: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
221: .getValue(calculable
222: .getHelperVariable(JRCalculable.HELPER_COUNT));
223:
224: return new Integer((int) holder.getCount());
225: }
226:
227: public Object initialValue() {
228: return JRIntegerIncrementerFactory.ZERO;
229: }
230: }
231:
232: /**
233: *
234: */
235: class JRIntegerSumIncrementer extends JRAbstractExtendedIncrementer {
236: /**
237: *
238: */
239: private static JRIntegerSumIncrementer mainInstance = new JRIntegerSumIncrementer();
240:
241: /**
242: *
243: */
244: private JRIntegerSumIncrementer() {
245: }
246:
247: /**
248: *
249: */
250: public static JRIntegerSumIncrementer getInstance() {
251: return mainInstance;
252: }
253:
254: /**
255: *
256: */
257: public Object increment(JRCalculable variable,
258: Object expressionValue, AbstractValueProvider valueProvider) {
259: Number value = (Number) variable.getIncrementedValue();
260: Number newValue = (Number) expressionValue;
261:
262: if (newValue == null) {
263: if (variable.isInitialized()) {
264: return null;
265: }
266:
267: return value;
268: }
269:
270: if (value == null || variable.isInitialized()) {
271: value = JRIntegerIncrementerFactory.ZERO;
272: }
273:
274: return new Integer(value.intValue() + newValue.intValue());
275: }
276:
277: public Object initialValue() {
278: return JRIntegerIncrementerFactory.ZERO;
279: }
280: }
281:
282: /**
283: *
284: */
285: class JRIntegerAverageIncrementer extends JRAbstractExtendedIncrementer {
286: /**
287: *
288: */
289: private static JRIntegerAverageIncrementer mainInstance = new JRIntegerAverageIncrementer();
290:
291: /**
292: *
293: */
294: private JRIntegerAverageIncrementer() {
295: }
296:
297: /**
298: *
299: */
300: public static JRIntegerAverageIncrementer getInstance() {
301: return mainInstance;
302: }
303:
304: /**
305: *
306: */
307: public Object increment(JRCalculable variable,
308: Object expressionValue, AbstractValueProvider valueProvider) {
309: if (expressionValue == null) {
310: if (variable.isInitialized()) {
311: return null;
312: }
313: return variable.getValue();
314: }
315: Number countValue = (Number) valueProvider.getValue(variable
316: .getHelperVariable(JRCalculable.HELPER_COUNT));
317: Number sumValue = (Number) valueProvider.getValue(variable
318: .getHelperVariable(JRCalculable.HELPER_SUM));
319: return new Integer(sumValue.intValue() / countValue.intValue());
320: }
321:
322: public Object initialValue() {
323: return JRIntegerIncrementerFactory.ZERO;
324: }
325: }
326:
327: /**
328: *
329: */
330: class JRIntegerStandardDeviationIncrementer extends
331: JRAbstractExtendedIncrementer {
332: /**
333: *
334: */
335: private static JRIntegerStandardDeviationIncrementer mainInstance = new JRIntegerStandardDeviationIncrementer();
336:
337: /**
338: *
339: */
340: private JRIntegerStandardDeviationIncrementer() {
341: }
342:
343: /**
344: *
345: */
346: public static JRIntegerStandardDeviationIncrementer 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 Integer((int) Math.sqrt(varianceValue.doubleValue()));
364: }
365:
366: public Object initialValue() {
367: return JRIntegerIncrementerFactory.ZERO;
368: }
369: }
370:
371: /**
372: *
373: */
374: class JRIntegerVarianceIncrementer extends
375: JRAbstractExtendedIncrementer {
376: /**
377: *
378: */
379: private static JRIntegerVarianceIncrementer mainInstance = new JRIntegerVarianceIncrementer();
380:
381: /**
382: *
383: */
384: private JRIntegerVarianceIncrementer() {
385: }
386:
387: /**
388: *
389: */
390: public static JRIntegerVarianceIncrementer getInstance() {
391: return mainInstance;
392: }
393:
394: /**
395: *
396: */
397: public Object increment(JRCalculable variable,
398: Object expressionValue, AbstractValueProvider valueProvider) {
399: Number value = (Number) variable.getIncrementedValue();
400: Number newValue = (Number) expressionValue;
401:
402: if (newValue == null) {
403: if (variable.isInitialized()) {
404: return null;
405: }
406: return value;
407: } else if (value == null || variable.isInitialized()) {
408: return JRIntegerIncrementerFactory.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 Integer(
416: (countValue.intValue() - 1)
417: * value.intValue()
418: / countValue.intValue()
419: + (sumValue.intValue()
420: / countValue.intValue() - newValue
421: .intValue())
422: * (sumValue.intValue()
423: / countValue.intValue() - newValue
424: .intValue())
425: / (countValue.intValue() - 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 Integer(((Number) calculableValue
442: .getIncrementedValue()).intValue());
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 Integer((int) (c1 / c * v1 + c2 / c * v2 + c2 / c1
468: * s1 / c * s1 / c + c1 / c2 * s2 / c * s2 / c - 2 * s1
469: / c * s2 / c));
470: }
471:
472: public Object initialValue() {
473: return JRIntegerIncrementerFactory.ZERO;
474: }
475: }
|