KickJava   Java API By Example, From Geeks To Geeks.

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


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: JRComparableIncrementerFactory.java 1311 2006-06-23 12:19:26 +0300 (Fri, 23 Jun 2006) teodord $
36  */

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

44     private static JRComparableIncrementerFactory mainInstance = new JRComparableIncrementerFactory();
45
46
47     /**
48      *
49      */

50     private JRComparableIncrementerFactory()
51     {
52     }
53
54
55     /**
56      *
57      */

58     public static JRComparableIncrementerFactory getInstance()
59     {
60         return mainInstance;
61     }
62
63
64     /**
65      *
66      */

67     public JRExtendedIncrementer getExtendedIncrementer(byte calculation)
68     {
69         JRExtendedIncrementer incrementer = null;
70
71         switch (calculation)
72         {
73             case JRVariable.CALCULATION_LOWEST :
74             {
75                 incrementer = JRComparableLowestIncrementer.getInstance();
76                 break;
77             }
78             case JRVariable.CALCULATION_HIGHEST :
79             {
80                 incrementer = JRComparableHighestIncrementer.getInstance();
81                 break;
82             }
83             case JRVariable.CALCULATION_SYSTEM :
84             case JRVariable.CALCULATION_NOTHING :
85             case JRVariable.CALCULATION_COUNT :
86             case JRVariable.CALCULATION_SUM :
87             case JRVariable.CALCULATION_AVERAGE :
88             case JRVariable.CALCULATION_STANDARD_DEVIATION :
89             case JRVariable.CALCULATION_VARIANCE :
90             case JRVariable.CALCULATION_FIRST :
91             case JRVariable.CALCULATION_DISTINCT_COUNT :
92             default :
93             {
94                 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation);
95                 break;
96             }
97         }
98         
99         return incrementer;
100     }
101 }
102
103
104 /**
105  *
106  */

107 class JRComparableLowestIncrementer extends JRAbstractExtendedIncrementer
108 {
109     /**
110      *
111      */

112     private static JRComparableLowestIncrementer mainInstance = new JRComparableLowestIncrementer();
113
114     /**
115      *
116      */

117     private JRComparableLowestIncrementer()
118     {
119     }
120
121     /**
122      *
123      */

124     public static JRComparableLowestIncrementer getInstance()
125     {
126         return mainInstance;
127     }
128
129     /**
130      *
131      */

132     public Object JavaDoc increment(
133         JRCalculable variable,
134         Object JavaDoc expressionValue,
135         AbstractValueProvider valueProvider
136         )
137     {
138         Comparable JavaDoc value = (Comparable JavaDoc)variable.getIncrementedValue();
139         Comparable JavaDoc newValue = (Comparable JavaDoc)expressionValue;
140
141         if (
142             value != null && !variable.isInitialized() &&
143             (newValue == null || value.compareTo(newValue) < 0)
144             )
145         {
146             newValue = value;
147         }
148                 
149         return newValue;
150     }
151
152     
153     public Object JavaDoc initialValue()
154     {
155         return null;
156     }
157 }
158
159
160 /**
161  *
162  */

163 class JRComparableHighestIncrementer extends JRAbstractExtendedIncrementer
164 {
165     /**
166      *
167      */

168     private static JRComparableHighestIncrementer mainInstance = new JRComparableHighestIncrementer();
169
170     /**
171      *
172      */

173     private JRComparableHighestIncrementer()
174     {
175     }
176
177     /**
178      *
179      */

180     public static JRComparableHighestIncrementer getInstance()
181     {
182         return mainInstance;
183     }
184
185     /**
186      *
187      */

188     public Object JavaDoc increment(
189         JRCalculable variable,
190         Object JavaDoc expressionValue,
191         AbstractValueProvider valueProvider
192         )
193     {
194         Comparable JavaDoc value = (Comparable JavaDoc)variable.getIncrementedValue();
195         Comparable JavaDoc newValue = (Comparable JavaDoc)expressionValue;
196
197         if (
198             value != null && !variable.isInitialized() &&
199             (newValue == null || value.compareTo(newValue) > 0)
200             )
201         {
202             newValue = value;
203         }
204                 
205         return newValue;
206     }
207
208     
209     public Object JavaDoc initialValue()
210     {
211         return null;
212     }
213 }
214
Popular Tags