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