KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > crosstabs > fill > JRPercentageCalculatorFactory


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;
29
30 import java.math.BigDecimal JavaDoc;
31 import java.math.BigInteger JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sf.jasperreports.engine.JRRuntimeException;
36 import net.sf.jasperreports.engine.fill.JRCalculable;
37
38 /**
39  * Factory for percentage calculators.
40  *
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: JRPercentageCalculatorFactory.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
43  */

44 public class JRPercentageCalculatorFactory
45 {
46     private static final Map JavaDoc builtInCalculators;
47
48     private static final Map JavaDoc cachedCalculators;
49
50     static
51     {
52         builtInCalculators = new HashMap JavaDoc();
53         builtInCalculators.put(Float JavaDoc.class.getName(), new FloatPercentageCalculator());
54         builtInCalculators.put(Double JavaDoc.class.getName(), new DoublePercentageCalculator());
55         builtInCalculators.put(Integer JavaDoc.class.getName(), new IntegerPercentageCalculator());
56         builtInCalculators.put(Long JavaDoc.class.getName(), new LongPercentageCalculator());
57         builtInCalculators.put(Short JavaDoc.class.getName(), new ShortPercentageCalculator());
58         builtInCalculators.put(Byte JavaDoc.class.getName(), new BytePercentageCalculator());
59         builtInCalculators.put(BigDecimal JavaDoc.class.getName(), new BigDecimalPercentageCalculator());
60         builtInCalculators.put(BigInteger JavaDoc.class.getName(), new BigIntegerPercentageCalculator());
61
62         cachedCalculators = new HashMap JavaDoc();
63     }
64
65     
66     /**
67      * Checks whether a class has built-in percentage support.
68      *
69      * @param valueClass the class
70      * @return whether the class has built-in percentage support
71      */

72     public static boolean hasBuiltInCalculator(Class JavaDoc valueClass)
73     {
74         return builtInCalculators.containsKey(valueClass.getName());
75     }
76
77     
78     /**
79      * Returns a percentage calculator.
80      * <p>
81      * If the percentage calculator class is not null, it will be used to instantiate a percentage calculator.
82      * Otherwise, a built-in percentage calculator will be returned based on the value class.
83      *
84      * @param percentageCalculatorClass the percentage calculator class
85      * @param valueClass the value class
86      * @return a percentage calculator for the percentage calculator class/value class
87      */

88     public static JRPercentageCalculator getPercentageCalculator(Class JavaDoc percentageCalculatorClass, Class JavaDoc valueClass)
89     {
90         JRPercentageCalculator calculator;
91
92         if (percentageCalculatorClass == null)
93         {
94             calculator = (JRPercentageCalculator) builtInCalculators.get(valueClass.getName());
95             if (calculator == null)
96             {
97                 throw new JRRuntimeException("Measure with type " + valueClass.getName() + " should specify a percentage calculator class.");
98             }
99         }
100         else
101         {
102             calculator = (JRPercentageCalculator) cachedCalculators.get(percentageCalculatorClass.getName());
103             
104             if (calculator == null)
105             {
106                 try
107                 {
108                     calculator = (JRPercentageCalculator) percentageCalculatorClass.newInstance();
109                     cachedCalculators.put(percentageCalculatorClass.getName(), calculator);
110                 }
111                 catch (InstantiationException JavaDoc e)
112                 {
113                     throw new JRRuntimeException("Error while creating percentage calculator instance of " + percentageCalculatorClass + ".", e);
114                 }
115                 catch (IllegalAccessException JavaDoc e)
116                 {
117                     throw new JRRuntimeException("Error while creating percentage calculator instance of " + percentageCalculatorClass + ".", e);
118                 }
119             }
120         }
121
122         return calculator;
123     }
124
125     
126     /**
127      * Percentage calculator for {@link Byte Byte} values.
128      */

129     public static class BytePercentageCalculator implements JRPercentageCalculator
130     {
131         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
132         {
133             Byte JavaDoc totalVal = (Byte JavaDoc) total.getValue();
134             Byte JavaDoc val = (Byte JavaDoc) value.getValue();
135             byte percentage = 0;
136             if (totalVal != null && totalVal.byteValue() != 0)
137             {
138                 percentage = (byte) (100 * val.byteValue() / totalVal.byteValue());
139             }
140
141             return new Byte JavaDoc(percentage);
142         }
143     }
144
145     
146     /**
147      * Percentage calculator for {@link Short Short} values.
148      */

149     public static class ShortPercentageCalculator implements JRPercentageCalculator
150     {
151         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
152         {
153             Short JavaDoc totalVal = (Short JavaDoc) total.getValue();
154             Short JavaDoc val = (Short JavaDoc) value.getValue();
155             short percentage = 0;
156             if (totalVal != null && totalVal.shortValue() != 0)
157             {
158                 percentage = (short) (100 * val.shortValue() / totalVal.shortValue());
159             }
160
161             return new Short JavaDoc(percentage);
162         }
163     }
164
165     
166     /**
167      * Percentage calculator for {@link Integer Integer} values.
168      */

169     public static class IntegerPercentageCalculator implements JRPercentageCalculator
170     {
171         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
172         {
173             Integer JavaDoc totalVal = (Integer JavaDoc) total.getValue();
174             Integer JavaDoc val = (Integer JavaDoc) value.getValue();
175             int percentage = 0;
176             if (totalVal != null && totalVal.intValue() != 0)
177             {
178                 percentage = 100 * val.intValue() / totalVal.intValue();
179             }
180
181             return new Integer JavaDoc(percentage);
182         }
183     }
184
185     
186     /**
187      * Percentage calculator for {@link Long Long} values.
188      */

189     public static class LongPercentageCalculator implements JRPercentageCalculator
190     {
191         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
192         {
193             Long JavaDoc totalVal = (Long JavaDoc) total.getValue();
194             Long JavaDoc val = (Long JavaDoc) value.getValue();
195             long percentage = 0L;
196             if (totalVal != null && totalVal.longValue() != 0)
197             {
198                 percentage = 100L * val.longValue() / totalVal.longValue();
199             }
200
201             return new Long JavaDoc(percentage);
202         }
203     }
204
205     
206     /**
207      * Percentage calculator for {@link Float Float} values.
208      */

209     public static class FloatPercentageCalculator implements JRPercentageCalculator
210     {
211         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
212         {
213             Float JavaDoc totalVal = (Float JavaDoc) total.getValue();
214             Float JavaDoc val = (Float JavaDoc) value.getValue();
215             float percentage = 0f;
216             if (totalVal != null && totalVal.floatValue() != 0)
217             {
218                 percentage = 100f * val.floatValue() / totalVal.floatValue();
219             }
220
221             return new Float JavaDoc(percentage);
222         }
223     }
224
225     /**
226      * Percentage calculator for {@link Double Double} values.
227      */

228     public static class DoublePercentageCalculator implements JRPercentageCalculator
229     {
230         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
231         {
232             Double JavaDoc totalVal = (Double JavaDoc) total.getValue();
233             Double JavaDoc val = (Double JavaDoc) value.getValue();
234             double percentage = 0d;
235             if (totalVal != null && totalVal.doubleValue() != 0)
236             {
237                 percentage = 100d * val.doubleValue() / totalVal.doubleValue();
238             }
239
240             return new Double JavaDoc(percentage);
241         }
242     }
243
244     
245     /**
246      * Percentage calculator for {@link BigDecimal BigDecimal} values.
247      */

248     public static class BigDecimalPercentageCalculator implements JRPercentageCalculator
249     {
250         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
251         {
252             BigDecimal JavaDoc totalVal = (BigDecimal JavaDoc) total.getValue();
253             BigDecimal JavaDoc val = (BigDecimal JavaDoc) value.getValue();
254             BigDecimal JavaDoc percentage;
255             if (totalVal != null && totalVal.doubleValue() != 0)
256             {
257                 percentage = val.multiply(BigDecimal.valueOf(100L)).divide(totalVal, BigDecimal.ROUND_HALF_UP);
258             }
259             else
260             {
261                 percentage = BigDecimal.valueOf(0);
262             }
263
264             return percentage;
265         }
266     }
267
268     
269     /**
270      * Percentage calculator for {@link BigInteger BigInteger} values.
271      */

272     public static class BigIntegerPercentageCalculator implements JRPercentageCalculator
273     {
274         public Object JavaDoc calculatePercentage(JRCalculable value, JRCalculable total)
275         {
276             BigInteger JavaDoc totalVal = (BigInteger JavaDoc) total.getValue();
277             BigInteger JavaDoc val = (BigInteger JavaDoc) value.getValue();
278             BigInteger JavaDoc percentage;
279             if (totalVal != null && totalVal.doubleValue() != 0)
280             {
281                 percentage = val.multiply(BigInteger.valueOf(100L)).divide(totalVal);
282             }
283             else
284             {
285                 percentage = BigInteger.valueOf(0);
286             }
287
288             return percentage;
289         }
290     }
291 }
292
Popular Tags