KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRBigDecimalIncrementerFactory


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.math.BigDecimal JavaDoc;
31
32 import net.sf.jasperreports.engine.JRVariable;
33
34
35 /**
36  * @author Teodor Danciu (teodord@users.sourceforge.net)
37  * @version $Id: JRBigDecimalIncrementerFactory.java 1347 2006-07-19 15:31:07 +0300 (Wed, 19 Jul 2006) teodord $
38  */

39 public class JRBigDecimalIncrementerFactory extends JRAbstractExtendedIncrementerFactory
40 {
41
42
43     /**
44      *
45      */

46     protected static final BigDecimal JavaDoc ZERO = new BigDecimal JavaDoc("0");
47     protected static final BigDecimal JavaDoc ONE = new BigDecimal JavaDoc("1");
48
49
50     /**
51      *
52      */

53     private static JRBigDecimalIncrementerFactory mainInstance = new JRBigDecimalIncrementerFactory();
54
55
56     /**
57      *
58      */

59     private JRBigDecimalIncrementerFactory()
60     {
61     }
62
63
64     /**
65      *
66      */

67     public static JRBigDecimalIncrementerFactory getInstance()
68     {
69         return mainInstance;
70     }
71
72
73     /**
74      *
75      */

76     public JRExtendedIncrementer getExtendedIncrementer(byte calculation)
77     {
78         JRExtendedIncrementer incrementer = null;
79
80         switch (calculation)
81         {
82             case JRVariable.CALCULATION_COUNT :
83             {
84                 incrementer = JRBigDecimalCountIncrementer.getInstance();
85                 break;
86             }
87             case JRVariable.CALCULATION_SUM :
88             {
89                 incrementer = JRBigDecimalSumIncrementer.getInstance();
90                 break;
91             }
92             case JRVariable.CALCULATION_AVERAGE :
93             {
94                 incrementer = JRBigDecimalAverageIncrementer.getInstance();
95                 break;
96             }
97             case JRVariable.CALCULATION_LOWEST :
98             case JRVariable.CALCULATION_HIGHEST :
99             {
100                 incrementer = JRComparableIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
101                 break;
102             }
103             case JRVariable.CALCULATION_STANDARD_DEVIATION :
104             {
105                 incrementer = JRBigDecimalStandardDeviationIncrementer.getInstance();
106                 break;
107             }
108             case JRVariable.CALCULATION_VARIANCE :
109             {
110                 incrementer = JRBigDecimalVarianceIncrementer.getInstance();
111                 break;
112             }
113             case JRVariable.CALCULATION_DISTINCT_COUNT :
114             {
115                 incrementer = JRBigDecimalDistinctCountIncrementer.getInstance();
116                 break;
117             }
118             case JRVariable.CALCULATION_SYSTEM :
119             case JRVariable.CALCULATION_NOTHING :
120             case JRVariable.CALCULATION_FIRST :
121             default :
122             {
123                 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
124                 break;
125             }
126         }
127         
128         return incrementer;
129     }
130
131
132 }
133
134
135 /**
136  *
137  */

138 class JRBigDecimalCountIncrementer extends JRAbstractExtendedIncrementer
139 {
140     /**
141      *
142      */

143     private static JRBigDecimalCountIncrementer mainInstance = new JRBigDecimalCountIncrementer();
144
145     /**
146      *
147      */

148     private JRBigDecimalCountIncrementer()
149     {
150     }
151
152     /**
153      *
154      */

155     public static JRBigDecimalCountIncrementer getInstance()
156     {
157         return mainInstance;
158     }
159
160     /**
161      *
162      */

163     public Object JavaDoc increment(
164         JRCalculable variable,
165         Object JavaDoc expressionValue,
166         AbstractValueProvider valueProvider
167         )
168     {
169         BigDecimal JavaDoc value = (BigDecimal JavaDoc)variable.getIncrementedValue();
170
171         if (value == null || variable.isInitialized())
172         {
173             value = JRBigDecimalIncrementerFactory.ZERO;
174         }
175
176         if (expressionValue == null)
177         {
178             return value;
179         }
180
181         return value.add(JRBigDecimalIncrementerFactory.ONE);
182     }
183
184     
185     public Object JavaDoc combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
186     {
187         BigDecimal JavaDoc value = (BigDecimal JavaDoc)calculable.getIncrementedValue();
188         BigDecimal JavaDoc combineValue = (BigDecimal JavaDoc) calculableValue.getValue();
189
190         if (value == null || calculable.isInitialized())
191         {
192             value = JRBigDecimalIncrementerFactory.ZERO;
193         }
194
195         if (combineValue == null)
196         {
197             return value;
198         }
199
200         return value.add(combineValue);
201     }
202
203     
204     public Object JavaDoc initialValue()
205     {
206         return JRBigDecimalIncrementerFactory.ZERO;
207     }
208 }
209
210
211 /**
212 *
213 */

214 class JRBigDecimalDistinctCountIncrementer extends JRAbstractExtendedIncrementer
215 {
216     /**
217      *
218      */

219     private static JRBigDecimalDistinctCountIncrementer mainInstance = new JRBigDecimalDistinctCountIncrementer();
220
221     /**
222      *
223      */

224     private JRBigDecimalDistinctCountIncrementer()
225     {
226     }
227
228     /**
229      *
230      */

231     public static JRBigDecimalDistinctCountIncrementer getInstance()
232     {
233         return mainInstance;
234     }
235
236     /**
237      *
238      */

239     public Object JavaDoc increment(
240         JRCalculable variable,
241         Object JavaDoc expressionValue,
242         AbstractValueProvider valueProvider
243         )
244     {
245         DistinctCountHolder holder =
246             (DistinctCountHolder)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
247         
248         if (variable.isInitialized())
249         {
250             holder.init();
251         }
252
253         return new BigDecimal JavaDoc(holder.getCount());
254     }
255
256     public Object JavaDoc combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
257     {
258         DistinctCountHolder holder =
259             (DistinctCountHolder)valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT));
260         
261         return new BigDecimal JavaDoc(holder.getCount());
262     }
263     
264     public Object JavaDoc initialValue()
265     {
266         return JRBigDecimalIncrementerFactory.ZERO;
267     }
268 }
269
270
271 /**
272  *
273  */

274 class JRBigDecimalSumIncrementer extends JRAbstractExtendedIncrementer
275 {
276     /**
277      *
278      */

279     private static JRBigDecimalSumIncrementer mainInstance = new JRBigDecimalSumIncrementer();
280
281     /**
282      *
283      */

284     private JRBigDecimalSumIncrementer()
285     {
286     }
287
288     /**
289      *
290      */

291     public static JRBigDecimalSumIncrementer getInstance()
292     {
293         return mainInstance;
294     }
295
296     /**
297      *
298      */

299     public Object JavaDoc increment(
300         JRCalculable variable,
301         Object JavaDoc expressionValue,
302         AbstractValueProvider valueProvider
303         )
304     {
305         BigDecimal JavaDoc value = (BigDecimal JavaDoc)variable.getIncrementedValue();
306         BigDecimal JavaDoc newValue = (BigDecimal JavaDoc)expressionValue;
307
308         if (newValue == null)
309         {
310             if (variable.isInitialized())
311             {
312                 return null;
313             }
314
315             return value;
316         }
317
318         if (value == null || variable.isInitialized())
319         {
320             value = JRBigDecimalIncrementerFactory.ZERO;
321         }
322
323         return value.add(newValue);
324     }
325
326     
327     public Object JavaDoc initialValue()
328     {
329         return JRBigDecimalIncrementerFactory.ZERO;
330     }
331 }
332
333
334 /**
335  *
336  */

337 class JRBigDecimalAverageIncrementer extends JRAbstractExtendedIncrementer
338 {
339     /**
340      *
341      */

342     private static JRBigDecimalAverageIncrementer mainInstance = new JRBigDecimalAverageIncrementer();
343
344     /**
345      *
346      */

347     private JRBigDecimalAverageIncrementer()
348     {
349     }
350
351     /**
352      *
353      */

354     public static JRBigDecimalAverageIncrementer getInstance()
355     {
356         return mainInstance;
357     }
358
359     /**
360      *
361      */

362     public Object JavaDoc increment(
363         JRCalculable variable,
364         Object JavaDoc expressionValue,
365         AbstractValueProvider valueProvider
366         )
367     {
368         if (expressionValue == null)
369         {
370             if (variable.isInitialized())
371             {
372                 return null;
373             }
374             return variable.getValue();
375         }
376         BigDecimal JavaDoc countValue = (BigDecimal JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
377         BigDecimal JavaDoc sumValue = (BigDecimal JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
378         return sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP);
379     }
380
381     
382     public Object JavaDoc initialValue()
383     {
384         return JRBigDecimalIncrementerFactory.ZERO;
385     }
386 }
387
388
389 /**
390  *
391  */

392 class JRBigDecimalStandardDeviationIncrementer extends JRAbstractExtendedIncrementer
393 {
394     /**
395      *
396      */

397     private static JRBigDecimalStandardDeviationIncrementer mainInstance = new JRBigDecimalStandardDeviationIncrementer();
398
399     /**
400      *
401      */

402     private JRBigDecimalStandardDeviationIncrementer()
403     {
404     }
405
406     /**
407      *
408      */

409     public static JRBigDecimalStandardDeviationIncrementer getInstance()
410     {
411         return mainInstance;
412     }
413
414     /**
415      *
416      */

417     public Object JavaDoc increment(
418         JRCalculable variable,
419         Object JavaDoc expressionValue,
420         AbstractValueProvider valueProvider
421         )
422     {
423         if (expressionValue == null)
424         {
425             if (variable.isInitialized())
426             {
427                 return null;
428             }
429             return variable.getValue();
430         }
431         Number JavaDoc varianceValue = (Number JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_VARIANCE));
432         return new BigDecimal JavaDoc( Math.sqrt(varianceValue.doubleValue()) );
433     }
434
435     
436     public Object JavaDoc initialValue()
437     {
438         return JRBigDecimalIncrementerFactory.ZERO;
439     }
440 }
441
442
443 /**
444  *
445  */

446 class JRBigDecimalVarianceIncrementer extends JRAbstractExtendedIncrementer
447 {
448     /**
449      *
450      */

451     private static JRBigDecimalVarianceIncrementer mainInstance = new JRBigDecimalVarianceIncrementer();
452
453     /**
454      *
455      */

456     private JRBigDecimalVarianceIncrementer()
457     {
458     }
459
460     /**
461      *
462      */

463     public static JRBigDecimalVarianceIncrementer getInstance()
464     {
465         return mainInstance;
466     }
467
468     /**
469      *
470      */

471     public Object JavaDoc increment(
472         JRCalculable variable,
473         Object JavaDoc expressionValue,
474         AbstractValueProvider valueProvider
475         )
476     {
477         BigDecimal JavaDoc value = (BigDecimal JavaDoc)variable.getIncrementedValue();
478         BigDecimal JavaDoc newValue = (BigDecimal JavaDoc)expressionValue;
479         
480         if (newValue == null)
481         {
482             if (variable.isInitialized())
483             {
484                 return null;
485             }
486             return value;
487         }
488         else if (value == null || variable.isInitialized())
489         {
490             return JRBigDecimalIncrementerFactory.ZERO;
491         }
492         else
493         {
494             BigDecimal JavaDoc countValue = (BigDecimal JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
495             BigDecimal JavaDoc sumValue = (BigDecimal JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
496             return
497                 countValue.subtract(JRBigDecimalIncrementerFactory.ONE).multiply(value).divide(countValue, BigDecimal.ROUND_HALF_UP).add(
498                     sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP).subtract(newValue).multiply(
499                         sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP).subtract(newValue)
500                         ).divide(countValue.subtract(JRBigDecimalIncrementerFactory.ONE), BigDecimal.ROUND_HALF_UP)
501                     );
502         }
503     }
504
505     
506     public Object JavaDoc combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
507     {
508         BigDecimal JavaDoc value = (BigDecimal JavaDoc)calculable.getIncrementedValue();
509         
510         if (calculableValue.getValue() == null)
511         {
512             if (calculable.isInitialized())
513             {
514                 return null;
515             }
516
517             return value;
518         }
519         else if (value == null || calculable.isInitialized())
520         {
521             return (BigDecimal JavaDoc) calculableValue.getIncrementedValue();
522         }
523
524         BigDecimal JavaDoc v1 = value;
525         BigDecimal JavaDoc c1 = (BigDecimal JavaDoc) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT));
526         BigDecimal JavaDoc s1 = (BigDecimal JavaDoc) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_SUM));
527
528         BigDecimal JavaDoc v2 = (BigDecimal JavaDoc) calculableValue.getIncrementedValue();
529         BigDecimal JavaDoc c2 = (BigDecimal JavaDoc) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_COUNT));
530         BigDecimal JavaDoc s2 = (BigDecimal JavaDoc) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_SUM));
531
532         c1 = c1.subtract(c2);
533         s1 = s1.subtract(s2);
534         
535         BigDecimal JavaDoc c = c1.add(c2);
536         
537         BigDecimal JavaDoc x1 = s1.divide(c, BigDecimal.ROUND_HALF_UP);
538         BigDecimal JavaDoc x2 = s2.divide(c, BigDecimal.ROUND_HALF_UP);
539         BigDecimal JavaDoc x3 = x1.multiply(x2);
540         
541         return c1.divide(c, BigDecimal.ROUND_HALF_UP).multiply(v1)
542             .add(c2.divide(c, BigDecimal.ROUND_HALF_UP).multiply(v2))
543             .add(c2.divide(c1, BigDecimal.ROUND_HALF_UP).multiply(x1).multiply(x1))
544             .add(c1.divide(c2, BigDecimal.ROUND_HALF_UP).multiply(x2).multiply(x2))
545             .subtract(x3).subtract(x3);
546     }
547
548     
549     public Object JavaDoc initialValue()
550     {
551         return JRBigDecimalIncrementerFactory.ZERO;
552     }
553 }
554
Popular Tags