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 JRComparableIncrementerFactory extends JRAbstractExtendedIncrementerFactory
33 {
34
35
36     /**
37      *
38      */

39     private static JRComparableIncrementerFactory mainInstance = new JRComparableIncrementerFactory();
40
41
42     /**
43      *
44      */

45     private JRComparableIncrementerFactory()
46     {
47     }
48
49
50     /**
51      *
52      */

53     public static JRComparableIncrementerFactory getInstance()
54     {
55         return mainInstance;
56     }
57
58
59     @Override
60     public JRExtendedIncrementer getExtendedIncrementer(CalculationEnum calculation)
61     {
62         JRExtendedIncrementer incrementer = null;
63
64         switch (calculation)
65         {
66             case LOWEST :
67             {
68                 incrementer = JRComparableLowestIncrementer.getInstance();
69                 break;
70             }
71             case HIGHEST :
72             {
73                 incrementer = JRComparableHighestIncrementer.getInstance();
74                 break;
75             }
76             case SYSTEM :
77             case NOTHING :
78             case COUNT :
79             case SUM :
80             case AVERAGE :
81             case STANDARD_DEVIATION :
82             case VARIANCE :
83             case FIRST :
84             case DISTINCT_COUNT :
85             default :
86             {
87                 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
88                 break;
89             }
90         }
91         
92         return incrementer;
93     }
94 }
95
96
97 /**
98  *
99  */

100 final class JRComparableLowestIncrementer extends JRAbstractExtendedIncrementer
101 {
102     /**
103      *
104      */

105     private static JRComparableLowestIncrementer mainInstance = new JRComparableLowestIncrementer();
106
107     /**
108      *
109      */

110     private JRComparableLowestIncrementer()
111     {
112     }
113
114     /**
115      *
116      */

117     public static JRComparableLowestIncrementer getInstance()
118     {
119         return mainInstance;
120     }
121
122     @Override
123     @SuppressWarnings({ "rawtypes""unchecked" })
124     public Object increment(
125         JRCalculable variable, 
126         Object expressionValue,
127         AbstractValueProvider valueProvider
128         )
129     {
130         Comparable value = (Comparable)variable.getIncrementedValue();
131         Comparable newValue = (Comparable)expressionValue;
132
133         if (
134             value != null && !variable.isInitialized() &&
135             (newValue == null || value.compareTo(newValue) < 0)
136             )
137         {
138             newValue = value;
139         }
140                 
141         return newValue;
142     }
143
144     
145     @Override
146     public Object initialValue()
147     {
148         return null;
149     }
150 }
151
152
153 /**
154  *
155  */

156 final class JRComparableHighestIncrementer extends JRAbstractExtendedIncrementer
157 {
158     /**
159      *
160      */

161     private static JRComparableHighestIncrementer mainInstance = new JRComparableHighestIncrementer();
162
163     /**
164      *
165      */

166     private JRComparableHighestIncrementer()
167     {
168     }
169
170     /**
171      *
172      */

173     public static JRComparableHighestIncrementer getInstance()
174     {
175         return mainInstance;
176     }
177
178     @Override
179     @SuppressWarnings({ "rawtypes""unchecked" })
180     public Object increment(
181         JRCalculable variable, 
182         Object expressionValue,
183         AbstractValueProvider valueProvider
184         )
185     {
186         Comparable value = (Comparable)variable.getIncrementedValue();
187         Comparable newValue = (Comparable)expressionValue;
188
189         if (
190             value != null && !variable.isInitialized() &&
191             (newValue == null || value.compareTo(newValue) > 0)
192             )
193         {
194             newValue = value;
195         }
196                 
197         return newValue;
198     }
199
200     
201     @Override
202     public Object initialValue()
203     {
204         return null;
205     }
206 }
207