1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fill;
25
26 import net.sf.jasperreports.engine.type.CalculationEnum;
27
28
29 /**
30  * @author Teodor Danciu (teodord@users.sourceforge.net)
31  */

32 public final class JRIntegerIncrementerFactory extends JRAbstractExtendedIncrementerFactory
33 {
34
35
36     /**
37      *
38      */

39     protected static final Integer ZERO = 0;
40
41
42     /**
43      *
44      */

45     private static JRIntegerIncrementerFactory mainInstance = new JRIntegerIncrementerFactory();
46
47
48     /**
49      *
50      */

51     private JRIntegerIncrementerFactory()
52     {
53     }
54
55
56     /**
57      *
58      */

59     public static JRIntegerIncrementerFactory getInstance()
60     {
61         return mainInstance;
62     }
63
64
65     @Override
66     public JRExtendedIncrementer getExtendedIncrementer(CalculationEnum calculation)
67     {
68         JRExtendedIncrementer incrementer = null;
69
70         switch (calculation)
71         {
72             case COUNT :
73             {
74                 incrementer = JRIntegerCountIncrementer.getInstance();
75                 break;
76             }
77             case SUM :
78             {
79                 incrementer = JRIntegerSumIncrementer.getInstance();
80                 break;
81             }
82             case AVERAGE :
83             {
84                 incrementer = JRIntegerAverageIncrementer.getInstance();
85                 break;
86             }
87             case LOWEST :
88             case HIGHEST :
89             {
90                 incrementer = JRComparableIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
91                 break;
92             }
93             case STANDARD_DEVIATION :
94             {
95                 incrementer = JRIntegerStandardDeviationIncrementer.getInstance();
96                 break;
97             }
98             case VARIANCE :
99             {
100                 incrementer = JRIntegerVarianceIncrementer.getInstance();
101                 break;
102             }
103             case DISTINCT_COUNT :
104             {
105                 incrementer = JRIntegerDistinctCountIncrementer.getInstance();
106                 break;
107             }
108             case SYSTEM :
109             case NOTHING :
110             case FIRST :
111             default :
112             {
113                 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
114                 break;
115             }
116         }
117         
118         return incrementer;
119     }
120
121
122 }
123
124
125 /**
126  *
127  */

128 final class JRIntegerCountIncrementer extends JRAbstractExtendedIncrementer
129 {
130     /**
131      *
132      */

133     private static JRIntegerCountIncrementer mainInstance = new JRIntegerCountIncrementer();
134
135     /**
136      *
137      */

138     private JRIntegerCountIncrementer()
139     {
140     }
141
142     /**
143      *
144      */

145     public static JRIntegerCountIncrementer getInstance()
146     {
147         return mainInstance;
148     }
149
150     @Override
151     public Object increment(
152         JRCalculable variable, 
153         Object expressionValue,
154         AbstractValueProvider valueProvider
155         )
156     {
157         Number value = (Number)variable.getIncrementedValue();
158
159         if (value == null || variable.isInitialized())
160         {
161             value = JRIntegerIncrementerFactory.ZERO;
162         }
163
164         if (expressionValue == null)
165         {
166             return value;
167         }
168
169         return value.intValue() + 1;
170     }
171
172     
173     @Override
174     public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
175     {
176         Number value = (Number)calculable.getIncrementedValue();
177         Number combineValue = (Number) calculableValue.getValue();
178
179         if (value == null || calculable.isInitialized())
180         {
181             value = JRIntegerIncrementerFactory.ZERO;
182         }
183
184         if (combineValue == null)
185         {
186             return value;
187         }
188
189         return value.intValue() + combineValue.intValue();
190     }
191
192     
193     @Override
194     public Object initialValue()
195     {
196         return JRIntegerIncrementerFactory.ZERO;
197     }
198 }
199
200
201 /**
202  *
203  */

204 final class JRIntegerDistinctCountIncrementer extends JRAbstractExtendedIncrementer
205 {
206     /**
207      *
208      */

209     private static JRIntegerDistinctCountIncrementer mainInstance = new JRIntegerDistinctCountIncrementer();
210
211     /**
212      *
213      */

214     private JRIntegerDistinctCountIncrementer()
215     {
216     }
217
218     /**
219      *
220      */

221     public static JRIntegerDistinctCountIncrementer getInstance()
222     {
223         return mainInstance;
224     }
225
226     @Override
227     public Object increment(
228         JRCalculable variable, 
229         Object expressionValue,
230         AbstractValueProvider valueProvider
231         )
232     {
233         DistinctCountHolder holder = 
234             (DistinctCountHolder)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
235         
236         if (variable.isInitialized())
237         {
238             holder.init();
239         }
240
241         return (int)holder.getCount();
242     }
243
244     @Override
245     public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
246     {
247         DistinctCountHolder holder = 
248             (DistinctCountHolder)valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT));
249         
250         return (int)holder.getCount();
251     }
252     
253     @Override
254     public Object initialValue()
255     {
256         return JRIntegerIncrementerFactory.ZERO;
257     }
258 }
259
260
261 /**
262  *
263  */

264 final class JRIntegerSumIncrementer extends JRAbstractExtendedIncrementer
265 {
266     /**
267      *
268      */

269     private static JRIntegerSumIncrementer mainInstance = new JRIntegerSumIncrementer();
270
271     /**
272      *
273      */

274     private JRIntegerSumIncrementer()
275     {
276     }
277
278     /**
279      *
280      */

281     public static JRIntegerSumIncrementer getInstance()
282     {
283         return mainInstance;
284     }
285
286     @Override
287     public Object increment(
288         JRCalculable variable, 
289         Object expressionValue,
290         AbstractValueProvider valueProvider
291         )
292     {
293         Number value = (Number)variable.getIncrementedValue();
294         Number newValue = (Number)expressionValue;
295
296         if (newValue == null)
297         {
298             if (variable.isInitialized())
299             {
300                 return null;
301             }
302
303             return value;
304         }
305
306         if (value == null || variable.isInitialized())
307         {
308             value = JRIntegerIncrementerFactory.ZERO;
309         }
310
311         return value.intValue() + newValue.intValue();
312     }
313
314     
315     @Override
316     public Object initialValue()
317     {
318         return JRIntegerIncrementerFactory.ZERO;
319     }
320 }
321
322
323 /**
324  *
325  */

326 final class JRIntegerAverageIncrementer extends JRAbstractExtendedIncrementer
327 {
328     /**
329      *
330      */

331     private static JRIntegerAverageIncrementer mainInstance = new JRIntegerAverageIncrementer();
332
333     /**
334      *
335      */

336     private JRIntegerAverageIncrementer()
337     {
338     }
339
340     /**
341      *
342      */

343     public static JRIntegerAverageIncrementer getInstance()
344     {
345         return mainInstance;
346     }
347
348     @Override
349     public Object increment(
350         JRCalculable variable, 
351         Object expressionValue,
352         AbstractValueProvider valueProvider
353         )
354     {
355         if (expressionValue == null)
356         {
357             if (variable.isInitialized())
358             {
359                 return null;
360             }
361             return variable.getValue();
362         }
363         Number countValue = (Number)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
364         Number sumValue = (Number)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
365         return sumValue.intValue() / countValue.intValue();
366     }
367
368     
369     @Override
370     public Object initialValue()
371     {
372         return JRIntegerIncrementerFactory.ZERO;
373     }
374 }
375
376
377 /**
378  *
379  */

380 final class JRIntegerStandardDeviationIncrementer extends JRAbstractExtendedIncrementer
381 {
382     /**
383      *
384      */

385     private static JRIntegerStandardDeviationIncrementer mainInstance = new JRIntegerStandardDeviationIncrementer();
386
387     /**
388      *
389      */

390     private JRIntegerStandardDeviationIncrementer()
391     {
392     }
393
394     /**
395      *
396      */

397     public static JRIntegerStandardDeviationIncrementer getInstance()
398     {
399         return mainInstance;
400     }
401
402     @Override
403     public Object increment(
404         JRCalculable variable, 
405         Object expressionValue,
406         AbstractValueProvider valueProvider
407         )
408     {
409         if (expressionValue == null)
410         {
411             if (variable.isInitialized())
412             {
413                 return null;
414             }
415             return variable.getValue(); 
416         }
417         Number varianceValue = (Number)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_VARIANCE));
418         return (int)Math.sqrt(varianceValue.doubleValue());
419     }
420
421     
422     @Override
423     public Object initialValue()
424     {
425         return JRIntegerIncrementerFactory.ZERO;
426     }
427 }
428
429
430 /**
431  *
432  */

433 final class JRIntegerVarianceIncrementer extends JRAbstractExtendedIncrementer
434 {
435     /**
436      *
437      */

438     private static JRIntegerVarianceIncrementer mainInstance = new JRIntegerVarianceIncrementer();
439
440     /**
441      *
442      */

443     private JRIntegerVarianceIncrementer()
444     {
445     }
446
447     /**
448      *
449      */

450     public static JRIntegerVarianceIncrementer getInstance()
451     {
452         return mainInstance;
453     }
454
455     @Override
456     public Object increment(
457         JRCalculable variable, 
458         Object expressionValue,
459         AbstractValueProvider valueProvider
460         )
461     {
462         Number value = (Number)variable.getIncrementedValue();
463         Number newValue = (Number)expressionValue;
464         
465         if (newValue == null)
466         {
467             if (variable.isInitialized())
468             {
469                 return null;
470             }
471             return value;
472         }
473         else if (value == null || variable.isInitialized())
474         {
475             return JRIntegerIncrementerFactory.ZERO;
476         }
477         else
478         {
479             Number countValue = (Number)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT));
480             Number sumValue = (Number)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM));
481             return
482                     (countValue.intValue() - 1) * value.intValue() / countValue.intValue() +
483                     ( sumValue.intValue() / countValue.intValue() - newValue.intValue() ) *
484                     ( sumValue.intValue() / countValue.intValue() - newValue.intValue() ) /
485                     (countValue.intValue() - 1);
486         }
487     }
488
489     @Override
490     public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider)
491     {
492         Number value = (Number)calculable.getIncrementedValue();
493         
494         if (calculableValue.getValue() == null)
495         {
496             if (calculable.isInitialized())
497             {
498                 return null;
499             }
500
501             return value;
502         }
503         else if (value == null || calculable.isInitialized())
504         {
505             return ((Number) calculableValue.getIncrementedValue()).intValue();
506         }
507
508         double v1 = value.doubleValue();
509         double c1 = ((Number) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT))).doubleValue();
510         double s1 = ((Number) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_SUM))).doubleValue();
511
512         double v2 = ((Number) calculableValue.getIncrementedValue()).doubleValue();
513         double c2 = ((Number) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_COUNT))).doubleValue();
514         double s2 = ((Number) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_SUM))).doubleValue();
515
516         c1 -= c2;
517         s1 -= s2;
518         
519         double c = c1 + c2;
520
521         return
522             (int) (
523                 c1 / c * v1 +
524                 c2 / c * v2 +
525                 c2 / c1 * s1 / c * s1 / c +
526                 c1 / c2 * s2 / c * s2 / c -
527                 2 * s1 / c * s2 /c
528                 );
529     }
530
531     
532     @Override
533     public Object initialValue()
534     {
535         return JRIntegerIncrementerFactory.ZERO;
536     }
537 }
538