001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.crosstabs.fill;
029:
030: import java.math.BigDecimal;
031: import java.math.BigInteger;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import net.sf.jasperreports.engine.JRRuntimeException;
036: import net.sf.jasperreports.engine.fill.JRCalculable;
037:
038: /**
039: * Factory for percentage calculators.
040: *
041: * @author Lucian Chirita (lucianc@users.sourceforge.net)
042: * @version $Id: JRPercentageCalculatorFactory.java 1229 2006-04-19 10:27:35Z teodord $
043: */
044: public class JRPercentageCalculatorFactory {
045: private static final Map builtInCalculators;
046:
047: private static final Map cachedCalculators;
048:
049: static {
050: builtInCalculators = new HashMap();
051: builtInCalculators.put(Float.class.getName(),
052: new FloatPercentageCalculator());
053: builtInCalculators.put(Double.class.getName(),
054: new DoublePercentageCalculator());
055: builtInCalculators.put(Integer.class.getName(),
056: new IntegerPercentageCalculator());
057: builtInCalculators.put(Long.class.getName(),
058: new LongPercentageCalculator());
059: builtInCalculators.put(Short.class.getName(),
060: new ShortPercentageCalculator());
061: builtInCalculators.put(Byte.class.getName(),
062: new BytePercentageCalculator());
063: builtInCalculators.put(BigDecimal.class.getName(),
064: new BigDecimalPercentageCalculator());
065: builtInCalculators.put(BigInteger.class.getName(),
066: new BigIntegerPercentageCalculator());
067:
068: cachedCalculators = new HashMap();
069: }
070:
071: /**
072: * Checks whether a class has built-in percentage support.
073: *
074: * @param valueClass the class
075: * @return whether the class has built-in percentage support
076: */
077: public static boolean hasBuiltInCalculator(Class valueClass) {
078: return builtInCalculators.containsKey(valueClass.getName());
079: }
080:
081: /**
082: * Returns a percentage calculator.
083: * <p>
084: * If the percentage calculator class is not null, it will be used to instantiate a percentage calculator.
085: * Otherwise, a built-in percentage calculator will be returned based on the value class.
086: *
087: * @param percentageCalculatorClass the percentage calculator class
088: * @param valueClass the value class
089: * @return a percentage calculator for the percentage calculator class/value class
090: */
091: public static JRPercentageCalculator getPercentageCalculator(
092: Class percentageCalculatorClass, Class valueClass) {
093: JRPercentageCalculator calculator;
094:
095: if (percentageCalculatorClass == null) {
096: calculator = (JRPercentageCalculator) builtInCalculators
097: .get(valueClass.getName());
098: if (calculator == null) {
099: throw new JRRuntimeException(
100: "Measure with type "
101: + valueClass.getName()
102: + " should specify a percentage calculator class.");
103: }
104: } else {
105: calculator = (JRPercentageCalculator) cachedCalculators
106: .get(percentageCalculatorClass.getName());
107:
108: if (calculator == null) {
109: try {
110: calculator = (JRPercentageCalculator) percentageCalculatorClass
111: .newInstance();
112: cachedCalculators.put(percentageCalculatorClass
113: .getName(), calculator);
114: } catch (InstantiationException e) {
115: throw new JRRuntimeException(
116: "Error while creating percentage calculator instance of "
117: + percentageCalculatorClass + ".",
118: e);
119: } catch (IllegalAccessException e) {
120: throw new JRRuntimeException(
121: "Error while creating percentage calculator instance of "
122: + percentageCalculatorClass + ".",
123: e);
124: }
125: }
126: }
127:
128: return calculator;
129: }
130:
131: /**
132: * Percentage calculator for {@link Byte Byte} values.
133: */
134: public static class BytePercentageCalculator implements
135: JRPercentageCalculator {
136: public Object calculatePercentage(JRCalculable value,
137: JRCalculable total) {
138: Byte totalVal = (Byte) total.getValue();
139: Byte val = (Byte) value.getValue();
140: byte percentage = 0;
141: if (totalVal != null && totalVal.byteValue() != 0) {
142: percentage = (byte) (100 * val.byteValue() / totalVal
143: .byteValue());
144: }
145:
146: return new Byte(percentage);
147: }
148: }
149:
150: /**
151: * Percentage calculator for {@link Short Short} values.
152: */
153: public static class ShortPercentageCalculator implements
154: JRPercentageCalculator {
155: public Object calculatePercentage(JRCalculable value,
156: JRCalculable total) {
157: Short totalVal = (Short) total.getValue();
158: Short val = (Short) value.getValue();
159: short percentage = 0;
160: if (totalVal != null && totalVal.shortValue() != 0) {
161: percentage = (short) (100 * val.shortValue() / totalVal
162: .shortValue());
163: }
164:
165: return new Short(percentage);
166: }
167: }
168:
169: /**
170: * Percentage calculator for {@link Integer Integer} values.
171: */
172: public static class IntegerPercentageCalculator implements
173: JRPercentageCalculator {
174: public Object calculatePercentage(JRCalculable value,
175: JRCalculable total) {
176: Integer totalVal = (Integer) total.getValue();
177: Integer val = (Integer) value.getValue();
178: int percentage = 0;
179: if (totalVal != null && totalVal.intValue() != 0) {
180: percentage = 100 * val.intValue() / totalVal.intValue();
181: }
182:
183: return new Integer(percentage);
184: }
185: }
186:
187: /**
188: * Percentage calculator for {@link Long Long} values.
189: */
190: public static class LongPercentageCalculator implements
191: JRPercentageCalculator {
192: public Object calculatePercentage(JRCalculable value,
193: JRCalculable total) {
194: Long totalVal = (Long) total.getValue();
195: Long val = (Long) value.getValue();
196: long percentage = 0L;
197: if (totalVal != null && totalVal.longValue() != 0) {
198: percentage = 100L * val.longValue()
199: / totalVal.longValue();
200: }
201:
202: return new Long(percentage);
203: }
204: }
205:
206: /**
207: * Percentage calculator for {@link Float Float} values.
208: */
209: public static class FloatPercentageCalculator implements
210: JRPercentageCalculator {
211: public Object calculatePercentage(JRCalculable value,
212: JRCalculable total) {
213: Float totalVal = (Float) total.getValue();
214: Float val = (Float) value.getValue();
215: float percentage = 0f;
216: if (totalVal != null && totalVal.floatValue() != 0) {
217: percentage = 100f * val.floatValue()
218: / totalVal.floatValue();
219: }
220:
221: return new Float(percentage);
222: }
223: }
224:
225: /**
226: * Percentage calculator for {@link Double Double} values.
227: */
228: public static class DoublePercentageCalculator implements
229: JRPercentageCalculator {
230: public Object calculatePercentage(JRCalculable value,
231: JRCalculable total) {
232: Double totalVal = (Double) total.getValue();
233: Double val = (Double) value.getValue();
234: double percentage = 0d;
235: if (totalVal != null && totalVal.doubleValue() != 0) {
236: percentage = 100d * val.doubleValue()
237: / totalVal.doubleValue();
238: }
239:
240: return new Double(percentage);
241: }
242: }
243:
244: /**
245: * Percentage calculator for {@link BigDecimal BigDecimal} values.
246: */
247: public static class BigDecimalPercentageCalculator implements
248: JRPercentageCalculator {
249: public Object calculatePercentage(JRCalculable value,
250: JRCalculable total) {
251: BigDecimal totalVal = (BigDecimal) total.getValue();
252: BigDecimal val = (BigDecimal) value.getValue();
253: BigDecimal percentage;
254: if (totalVal != null && totalVal.doubleValue() != 0) {
255: percentage = val.multiply(BigDecimal.valueOf(100L))
256: .divide(totalVal, BigDecimal.ROUND_HALF_UP);
257: } else {
258: percentage = BigDecimal.valueOf(0);
259: }
260:
261: return percentage;
262: }
263: }
264:
265: /**
266: * Percentage calculator for {@link BigInteger BigInteger} values.
267: */
268: public static class BigIntegerPercentageCalculator implements
269: JRPercentageCalculator {
270: public Object calculatePercentage(JRCalculable value,
271: JRCalculable total) {
272: BigInteger totalVal = (BigInteger) total.getValue();
273: BigInteger val = (BigInteger) value.getValue();
274: BigInteger percentage;
275: if (totalVal != null && totalVal.doubleValue() != 0) {
276: percentage = val.multiply(BigInteger.valueOf(100L))
277: .divide(totalVal);
278: } else {
279: percentage = BigInteger.valueOf(0);
280: }
281:
282: return percentage;
283: }
284: }
285: }
|