KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > crosstabs > fill > calculation > MeasureDefinition


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.crosstabs.fill.calculation;
29
30 import net.sf.jasperreports.engine.JRException;
31 import net.sf.jasperreports.engine.JRVariable;
32 import net.sf.jasperreports.engine.fill.AbstractValueProvider;
33 import net.sf.jasperreports.engine.fill.JRCalculable;
34 import net.sf.jasperreports.engine.fill.JRDistinctCountExtendedIncrementerFactory;
35 import net.sf.jasperreports.engine.fill.JRExtendedIncrementer;
36 import net.sf.jasperreports.engine.fill.JRExtendedIncrementerFactory;
37
38 /**
39  * Crosstab measure definition.
40  *
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: MeasureDefinition.java 1311 2006-06-23 12:19:26 +0300 (Fri, 23 Jun 2006) teodord $
43  */

44 public class MeasureDefinition
45 {
46     protected final byte calculation;
47     protected final JRExtendedIncrementerFactory incrementerFactory;
48     protected final Class JavaDoc valueClass;
49     protected final boolean isSystemDefined;
50     
51     
52     /**
53      * Create a measure definition.
54      *
55      * @param valueClass the value class
56      * @param calculation the calculation type
57      * @param incrementerFactory the incrementer factory
58      */

59     public MeasureDefinition(
60             Class JavaDoc valueClass,
61             byte calculation,
62             JRExtendedIncrementerFactory incrementerFactory)
63     {
64         this(valueClass, calculation, incrementerFactory, false);
65     }
66     
67     protected MeasureDefinition(
68             Class JavaDoc valueClass,
69             byte calculation,
70             JRExtendedIncrementerFactory incrementerFactory,
71             boolean isSystemDefined)
72     {
73         this.valueClass = valueClass;
74         this.calculation = calculation;
75         this.incrementerFactory = incrementerFactory;
76         this.isSystemDefined = isSystemDefined;
77     }
78     
79     
80     /**
81      * Creates a helper measure for a specific calculation.
82      *
83      * @param measure the measure
84      * @param helperCalculation the calculation
85      * @return the helper measure having the specified calculation
86      */

87     public static MeasureDefinition createHelperMeasure(MeasureDefinition measure, byte helperCalculation)
88     {
89         return new MeasureDefinition(measure.valueClass, helperCalculation, measure.incrementerFactory, true);
90     }
91
92     
93     /**
94      * Creates a helper measure for a distinct count calculation.
95      *
96      * @param measure the measure
97      * @return the helper measure having the specified calculation
98      */

99     public static MeasureDefinition createDistinctCountHelperMeasure(MeasureDefinition measure)
100     {
101         return new MeasureDefinition(measure.valueClass, JRVariable.CALCULATION_NOTHING, JRDistinctCountExtendedIncrementerFactory.getInstance(), true);
102     }
103
104     
105     /**
106      * Returns the calculation type.
107      *
108      * @return the calculation type
109      */

110     public byte getCalculation()
111     {
112         return calculation;
113     }
114
115     
116     /**
117      * Returns the incrementer factory.
118      *
119      * @return the incrementer factory
120      */

121     public JRExtendedIncrementerFactory getIncrementerFactory()
122     {
123         return incrementerFactory;
124     }
125     
126     
127     /**
128      * Returns the incrementer used for this measure.
129      *
130      * @return the incrementer used for this measure
131      */

132     public JRExtendedIncrementer getIncrementer()
133     {
134         return incrementerFactory.getExtendedIncrementer(calculation);
135     }
136     
137     protected boolean isSystemDefined()
138     {
139         return isSystemDefined;
140     }
141     
142     
143     /**
144      * Returns the measure value class.
145      *
146      * @return the measure value class
147      */

148     public Class JavaDoc getValueClass()
149     {
150         return valueClass;
151     }
152     
153     
154     /**
155      * Measure value provider.
156      */

157     protected static final AbstractValueProvider VALUE_PROVIDER = new AbstractValueProvider()
158     {
159         public Object JavaDoc getValue(JRCalculable calculable)
160         {
161             return calculable.getValue();
162         }
163     };
164
165     
166     /**
167      * An accumulated value of a crosstab measure.
168      *
169      * @author Lucian Chirita (lucianc@users.sourceforge.net)
170      */

171     public class MeasureValue implements JRCalculable
172     {
173         private Object JavaDoc value;
174         private MeasureValue[] helpers;
175         private boolean initialized;
176         private JRExtendedIncrementer incrementer;
177         
178         
179         /**
180          * Initializes the value.
181          */

182         public MeasureValue()
183         {
184             this.value = null;
185             this.helpers = new MeasureValue[HELPER_SIZE];
186             incrementer = getIncrementer();
187             
188             init();
189         }
190         
191         protected void init()
192         {
193             this.value = incrementer.initialValue();
194             setInitialized(true);
195         }
196         
197         
198         /**
199          * Accumulates a value.
200          *
201          * @param addValue the value
202          * @throws JRException
203          */

204         public void addValue(Object JavaDoc addValue) throws JRException
205         {
206             this.value = incrementer.increment(this, addValue, VALUE_PROVIDER);
207             setInitialized(false);
208         }
209         
210         
211         /**
212          * Accumulates another measure value.
213          * <p>
214          * This is used for total calculations, when two accumulated values are combined into a total.
215          *
216          * @param measureValue the measure value
217          * @throws JRException
218          */

219         public void addValue(MeasureValue measureValue) throws JRException
220         {
221             if (!measureValue.isInitialized())
222             {
223                 this.value = incrementer.combine(this, measureValue, VALUE_PROVIDER);
224                 setInitialized(false);
225             }
226         }
227         
228         public Object JavaDoc getValue()
229         {
230             return value;
231         }
232         
233         public String JavaDoc toString()
234         {
235             return String.valueOf(getValue());
236         }
237
238         
239         /**
240          * Sets a helper variable.
241          *
242          * @param helperVariable the helper variable
243          * @param type the helper type
244          * @return the previous helper variable for the type
245          */

246         public MeasureValue setHelper(MeasureValue helperVariable, byte type)
247         {
248             MeasureValue old = helpers[type];
249             helpers[type] = helperVariable;
250             return old;
251         }
252
253         public boolean isInitialized()
254         {
255             return initialized;
256         }
257
258         public Object JavaDoc getIncrementedValue()
259         {
260             return value;
261         }
262
263         public JRCalculable getHelperVariable(byte helperType)
264         {
265             return helpers[helperType];
266         }
267
268         public void setInitialized(boolean isInitialized)
269         {
270             this.initialized = isInitialized;
271         }
272     }
273 }
274
Popular Tags