KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.jasperreports.engine.JRVariable;
31
32
33 /**
34  * @author Teodor Danciu (teodord@users.sourceforge.net)
35  * @version $Id: JRShortIncrementerFactory.java 1347 2006-07-19 15:31:07 +0300 (Wed, 19 Jul 2006) teodord $
36  */

37 public class JRShortIncrementerFactory extends JRAbstractExtendedIncrementerFactory
38 {
39
40
41     /**
42      *
43      */

44     protected static final Short JavaDoc ZERO = new Short JavaDoc((short)0);
45
46
47     /**
48      *
49      */

50     private static JRShortIncrementerFactory mainInstance = new JRShortIncrementerFactory();
51
52
53     /**
54      *
55      */

56     private JRShortIncrementerFactory()
57     {
58     }
59
60
61     /**
62      *
63      */

64     public static JRShortIncrementerFactory getInstance()
65     {
66         return mainInstance;
67     }
68
69
70     /**
71      *
72      */

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

135 class JRShortCountIncrementer extends JRAbstractExtendedIncrementer
136 {
137     /**
138      *
139      */

140     private static JRShortCountIncrementer mainInstance = new JRShortCountIncrementer();
141
142     /**
143      *
144      */

145     private JRShortCountIncrementer()
146     {
147     }
148
149     /**
150      *
151      */

152     public static JRShortCountIncrementer getInstance()
153     {
154         return mainInstance;
155     }
156
157     /**
158      *
159      */

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

211 class JRShortDistinctCountIncrementer extends JRAbstractExtendedIncrementer
212 {
213     /**
214      *
215      */

216     private static JRShortDistinctCountIncrementer mainInstance = new JRShortDistinctCountIncrementer();
217
218     /**
219      *
220      */

221     private JRShortDistinctCountIncrementer()
222     {
223     }
224
225     /**
226      *
227      */

228     public static JRShortDistinctCountIncrementer getInstance()
229     {
230         return mainInstance;
231     }
232
233     /**
234      *
235      */

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

271 class JRShortSumIncrementer extends JRAbstractExtendedIncrementer
272 {
273     /**
274      *
275      */

276     private static JRShortSumIncrementer mainInstance = new JRShortSumIncrementer();
277
278     /**
279      *
280      */

281     private JRShortSumIncrementer()
282     {
283     }
284
285     /**
286      *
287      */

288     public static JRShortSumIncrementer getInstance()
289     {
290         return mainInstance;
291     }
292
293     /**
294      *
295      */

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

334 class JRShortAverageIncrementer extends JRAbstractExtendedIncrementer
335 {
336     /**
337      *
338      */

339     private static JRShortAverageIncrementer mainInstance = new JRShortAverageIncrementer();
340
341     /**
342      *
343      */

344     private JRShortAverageIncrementer()
345     {
346     }
347
348     /**
349      *
350      */

351     public static JRShortAverageIncrementer getInstance()
352     {
353         return mainInstance;
354     }
355
356     /**
357      *
358      */

359     public Object JavaDoc increment(
360         JRCalculable variable,
361         Object JavaDoc expressionValue,
362         AbstractValueProvider valueProvider
363         )
364     {
365         if (expressionValue == null)
366         {
367             if (variable.isInitialized())
368             {
369                 return null;
370             }
371             return variable.getValue();
372         }
373         Number JavaDoc countValue = (Number JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
374         Number JavaDoc sumValue = (Number JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
375         return new Short JavaDoc((short)(sumValue.shortValue() / countValue.shortValue()));
376     }
377
378     
379     public Object JavaDoc initialValue()
380     {
381         return JRShortIncrementerFactory.ZERO;
382     }
383 }
384
385
386 /**
387  *
388  */

389 class JRShortStandardDeviationIncrementer extends JRAbstractExtendedIncrementer
390 {
391     /**
392      *
393      */

394     private static JRShortStandardDeviationIncrementer mainInstance = new JRShortStandardDeviationIncrementer();
395
396     /**
397      *
398      */

399     private JRShortStandardDeviationIncrementer()
400     {
401     }
402
403     /**
404      *
405      */

406     public static JRShortStandardDeviationIncrementer getInstance()
407     {
408         return mainInstance;
409     }
410
411     /**
412      *
413      */

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

443 class JRShortVarianceIncrementer extends JRAbstractExtendedIncrementer
444 {
445     /**
446      *
447      */

448     private static JRShortVarianceIncrementer mainInstance = new JRShortVarianceIncrementer();
449
450     /**
451      *
452      */

453     private JRShortVarianceIncrementer()
454     {
455     }
456
457     /**
458      *
459      */

460     public static JRShortVarianceIncrementer getInstance()
461     {
462         return mainInstance;
463     }
464
465     /**
466      *
467      */

468     public Object JavaDoc increment(
469         JRCalculable variable,
470         Object JavaDoc expressionValue,
471         AbstractValueProvider valueProvider
472         )
473     {
474         Number JavaDoc value = (Number JavaDoc)variable.getIncrementedValue();
475         Number JavaDoc newValue = (Number JavaDoc)expressionValue;
476         
477         if (newValue == null)
478         {
479             if (variable.isInitialized())
480             {
481                 return null;
482             }
483             return value;
484         }
485         else if (value == null || variable.isInitialized())
486         {
487             return JRShortIncrementerFactory.ZERO;
488         }
489         else
490         {
491             Number JavaDoc countValue = (Number JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
492             Number JavaDoc sumValue = (Number JavaDoc)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
493             return
494                 new Short JavaDoc((short)(
495                     (countValue.shortValue() - 1) * value.shortValue() / countValue.shortValue() +
496                     ( sumValue.shortValue() / countValue.shortValue() - newValue.shortValue() ) *
497                     ( sumValue.shortValue() / countValue.shortValue() - newValue.shortValue() ) /
498                     (countValue.shortValue() - 1)
499                     ));
500         }
501     }
502
503     public Object JavaDoc combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
504     {
505         Number JavaDoc value = (Number JavaDoc)calculable.getIncrementedValue();
506         
507         if (calculableValue.getValue() == null)
508         {
509             if (calculable.isInitialized())
510             {
511                 return null;
512             }
513
514             return value;
515         }
516         else if (value == null || calculable.isInitialized())
517         {
518             return new Short JavaDoc(((Number JavaDoc) calculableValue.getIncrementedValue()).shortValue());
519         }
520
521         float v1 = value.floatValue();
522         float c1 = ((Number JavaDoc) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT))).floatValue();
523         float s1 = ((Number JavaDoc) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_SUM))).floatValue();
524
525         float v2 = ((Number JavaDoc) calculableValue.getIncrementedValue()).floatValue();
526         float c2 = ((Number JavaDoc) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_COUNT))).floatValue();
527         float s2 = ((Number JavaDoc) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_SUM))).floatValue();
528
529         c1 -= c2;
530         s1 -= s2;
531         
532         float c = c1 + c2;
533
534         return new Short JavaDoc(
535                 (short) (
536                 c1 / c * v1 +
537                 c2 / c * v2 +
538                 c2 / c1 * s1 / c * s1 / c +
539                 c1 / c2 * s2 / c * s2 / c -
540                 2 * s1 / c * s2 /c
541                 ));
542     }
543
544     
545     public Object JavaDoc initialValue()
546     {
547         return JRShortIncrementerFactory.ZERO;
548     }
549 }
550
Popular Tags