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: JRLongIncrementerFactory.java 1347 2006-07-19 12:31:07Z teodord $
035: */
036: public class JRLongIncrementerFactory extends
037: JRAbstractExtendedIncrementerFactory {
038:
039: /**
040: *
041: */
042: protected static final Long ZERO = new Long(0);
043:
044: /**
045: *
046: */
047: private static JRLongIncrementerFactory mainInstance = new JRLongIncrementerFactory();
048:
049: /**
050: *
051: */
052: private JRLongIncrementerFactory() {
053: }
054:
055: /**
056: *
057: */
058: public static JRLongIncrementerFactory 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 = JRLongCountIncrementer.getInstance();
071: break;
072: }
073: case JRVariable.CALCULATION_SUM: {
074: incrementer = JRLongSumIncrementer.getInstance();
075: break;
076: }
077: case JRVariable.CALCULATION_AVERAGE: {
078: incrementer = JRLongAverageIncrementer.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 = JRLongStandardDeviationIncrementer
089: .getInstance();
090: break;
091: }
092: case JRVariable.CALCULATION_VARIANCE: {
093: incrementer = JRLongVarianceIncrementer.getInstance();
094: break;
095: }
096: case JRVariable.CALCULATION_DISTINCT_COUNT: {
097: incrementer = JRLongDistinctCountIncrementer.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 JRLongCountIncrementer extends JRAbstractExtendedIncrementer {
119: /**
120: *
121: */
122: private static JRLongCountIncrementer mainInstance = new JRLongCountIncrementer();
123:
124: /**
125: *
126: */
127: private JRLongCountIncrementer() {
128: }
129:
130: /**
131: *
132: */
133: public static JRLongCountIncrementer 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 = JRLongIncrementerFactory.ZERO;
146: }
147:
148: if (expressionValue == null) {
149: return value;
150: }
151:
152: return new Long(value.longValue() + 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 = JRLongIncrementerFactory.ZERO;
163: }
164:
165: if (combineValue == null) {
166: return value;
167: }
168:
169: return new Long(value.longValue() + combineValue.longValue());
170: }
171:
172: public Object initialValue() {
173: return JRLongIncrementerFactory.ZERO;
174: }
175: }
176:
177: /**
178: *
179: */
180: class JRLongDistinctCountIncrementer extends
181: JRAbstractExtendedIncrementer {
182: /**
183: *
184: */
185: private static JRLongDistinctCountIncrementer mainInstance = new JRLongDistinctCountIncrementer();
186:
187: /**
188: *
189: */
190: private JRLongDistinctCountIncrementer() {
191: }
192:
193: /**
194: *
195: */
196: public static JRLongDistinctCountIncrementer 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 Long(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 Long(holder.getCount());
224: }
225:
226: public Object initialValue() {
227: return JRLongIncrementerFactory.ZERO;
228: }
229: }
230:
231: /**
232: *
233: */
234: class JRLongSumIncrementer extends JRAbstractExtendedIncrementer {
235: /**
236: *
237: */
238: private static JRLongSumIncrementer mainInstance = new JRLongSumIncrementer();
239:
240: /**
241: *
242: */
243: private JRLongSumIncrementer() {
244: }
245:
246: /**
247: *
248: */
249: public static JRLongSumIncrementer 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 = JRLongIncrementerFactory.ZERO;
271: }
272:
273: return new Long(value.longValue() + newValue.longValue());
274: }
275:
276: public Object initialValue() {
277: return JRLongIncrementerFactory.ZERO;
278: }
279: }
280:
281: /**
282: *
283: */
284: class JRLongAverageIncrementer extends JRAbstractExtendedIncrementer {
285: /**
286: *
287: */
288: private static JRLongAverageIncrementer mainInstance = new JRLongAverageIncrementer();
289:
290: /**
291: *
292: */
293: private JRLongAverageIncrementer() {
294: }
295:
296: /**
297: *
298: */
299: public static JRLongAverageIncrementer 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 Long(sumValue.longValue() / countValue.longValue());
319: }
320:
321: public Object initialValue() {
322: return JRLongIncrementerFactory.ZERO;
323: }
324: }
325:
326: /**
327: *
328: */
329: class JRLongStandardDeviationIncrementer extends
330: JRAbstractExtendedIncrementer {
331: /**
332: *
333: */
334: private static JRLongStandardDeviationIncrementer mainInstance = new JRLongStandardDeviationIncrementer();
335:
336: /**
337: *
338: */
339: private JRLongStandardDeviationIncrementer() {
340: }
341:
342: /**
343: *
344: */
345: public static JRLongStandardDeviationIncrementer getInstance() {
346: return mainInstance;
347: }
348:
349: /**
350: *
351: */
352: public Object increment(JRCalculable variable,
353: Object expressionValue, AbstractValueProvider valueProvider) {
354: if (expressionValue == null) {
355: if (variable.isInitialized()) {
356: return null;
357: }
358: return variable.getValue();
359: }
360: Number varianceValue = (Number) valueProvider.getValue(variable
361: .getHelperVariable(JRCalculable.HELPER_VARIANCE));
362: return new Long((long) Math.sqrt(varianceValue.doubleValue()));
363: }
364:
365: public Object initialValue() {
366: return JRLongIncrementerFactory.ZERO;
367: }
368: }
369:
370: /**
371: *
372: */
373: class JRLongVarianceIncrementer extends JRAbstractExtendedIncrementer {
374: /**
375: *
376: */
377: private static JRLongVarianceIncrementer mainInstance = new JRLongVarianceIncrementer();
378:
379: /**
380: *
381: */
382: private JRLongVarianceIncrementer() {
383: }
384:
385: /**
386: *
387: */
388: public static JRLongVarianceIncrementer getInstance() {
389: return mainInstance;
390: }
391:
392: /**
393: *
394: */
395: public Object increment(JRCalculable variable,
396: Object expressionValue, AbstractValueProvider valueProvider) {
397: Number value = (Number) variable.getIncrementedValue();
398: Number newValue = (Number) expressionValue;
399:
400: if (newValue == null) {
401: if (variable.isInitialized()) {
402: return null;
403: }
404: return value;
405: } else if (value == null || variable.isInitialized()) {
406: return JRLongIncrementerFactory.ZERO;
407: } else {
408: Number countValue = (Number) valueProvider
409: .getValue(variable
410: .getHelperVariable(JRCalculable.HELPER_COUNT));
411: Number sumValue = (Number) valueProvider.getValue(variable
412: .getHelperVariable(JRCalculable.HELPER_SUM));
413: return new Long(
414: (countValue.longValue() - 1)
415: * value.longValue()
416: / countValue.longValue()
417: + (sumValue.longValue()
418: / countValue.longValue() - newValue
419: .longValue())
420: * (sumValue.longValue()
421: / countValue.longValue() - newValue
422: .longValue())
423: / (countValue.longValue() - 1));
424: }
425: }
426:
427: public Object combine(JRCalculable calculable,
428: JRCalculable calculableValue,
429: AbstractValueProvider valueProvider) {
430: Number value = (Number) calculable.getIncrementedValue();
431:
432: if (calculableValue.getValue() == null) {
433: if (calculable.isInitialized()) {
434: return null;
435: }
436:
437: return value;
438: } else if (value == null || calculable.isInitialized()) {
439: return new Long(((Number) calculableValue
440: .getIncrementedValue()).longValue());
441: }
442:
443: double v1 = value.doubleValue();
444: double c1 = ((Number) valueProvider.getValue(calculable
445: .getHelperVariable(JRCalculable.HELPER_COUNT)))
446: .doubleValue();
447: double s1 = ((Number) valueProvider.getValue(calculable
448: .getHelperVariable(JRCalculable.HELPER_SUM)))
449: .doubleValue();
450:
451: double v2 = ((Number) calculableValue.getIncrementedValue())
452: .doubleValue();
453: double c2 = ((Number) valueProvider.getValue(calculableValue
454: .getHelperVariable(JRCalculable.HELPER_COUNT)))
455: .doubleValue();
456: double s2 = ((Number) valueProvider.getValue(calculableValue
457: .getHelperVariable(JRCalculable.HELPER_SUM)))
458: .doubleValue();
459:
460: c1 -= c2;
461: s1 -= s2;
462:
463: double c = c1 + c2;
464:
465: return new Long((long) (c1 / c * v1 + c2 / c * v2 + c2 / c1
466: * s1 / c * s1 / c + c1 / c2 * s2 / c * s2 / c - 2 * s1
467: / c * s2 / c));
468: }
469:
470: public Object initialValue() {
471: return JRLongIncrementerFactory.ZERO;
472: }
473: }
|