1 28 package net.sf.jasperreports.crosstabs.fill; 29 30 import java.math.BigDecimal ; 31 import java.math.BigInteger ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import net.sf.jasperreports.engine.JRRuntimeException; 36 import net.sf.jasperreports.engine.fill.JRCalculable; 37 38 44 public class JRPercentageCalculatorFactory 45 { 46 private static final Map builtInCalculators; 47 48 private static final Map cachedCalculators; 49 50 static 51 { 52 builtInCalculators = new HashMap (); 53 builtInCalculators.put(Float .class.getName(), new FloatPercentageCalculator()); 54 builtInCalculators.put(Double .class.getName(), new DoublePercentageCalculator()); 55 builtInCalculators.put(Integer .class.getName(), new IntegerPercentageCalculator()); 56 builtInCalculators.put(Long .class.getName(), new LongPercentageCalculator()); 57 builtInCalculators.put(Short .class.getName(), new ShortPercentageCalculator()); 58 builtInCalculators.put(Byte .class.getName(), new BytePercentageCalculator()); 59 builtInCalculators.put(BigDecimal .class.getName(), new BigDecimalPercentageCalculator()); 60 builtInCalculators.put(BigInteger .class.getName(), new BigIntegerPercentageCalculator()); 61 62 cachedCalculators = new HashMap (); 63 } 64 65 66 72 public static boolean hasBuiltInCalculator(Class valueClass) 73 { 74 return builtInCalculators.containsKey(valueClass.getName()); 75 } 76 77 78 88 public static JRPercentageCalculator getPercentageCalculator(Class percentageCalculatorClass, Class 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 e) 112 { 113 throw new JRRuntimeException("Error while creating percentage calculator instance of " + percentageCalculatorClass + ".", e); 114 } 115 catch (IllegalAccessException 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 129 public static class BytePercentageCalculator implements JRPercentageCalculator 130 { 131 public Object calculatePercentage(JRCalculable value, JRCalculable total) 132 { 133 Byte totalVal = (Byte ) total.getValue(); 134 Byte val = (Byte ) 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 (percentage); 142 } 143 } 144 145 146 149 public static class ShortPercentageCalculator implements JRPercentageCalculator 150 { 151 public Object calculatePercentage(JRCalculable value, JRCalculable total) 152 { 153 Short totalVal = (Short ) total.getValue(); 154 Short val = (Short ) 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 (percentage); 162 } 163 } 164 165 166 169 public static class IntegerPercentageCalculator implements JRPercentageCalculator 170 { 171 public Object calculatePercentage(JRCalculable value, JRCalculable total) 172 { 173 Integer totalVal = (Integer ) total.getValue(); 174 Integer val = (Integer ) 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 (percentage); 182 } 183 } 184 185 186 189 public static class LongPercentageCalculator implements JRPercentageCalculator 190 { 191 public Object calculatePercentage(JRCalculable value, JRCalculable total) 192 { 193 Long totalVal = (Long ) total.getValue(); 194 Long val = (Long ) 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 (percentage); 202 } 203 } 204 205 206 209 public static class FloatPercentageCalculator implements JRPercentageCalculator 210 { 211 public Object calculatePercentage(JRCalculable value, JRCalculable total) 212 { 213 Float totalVal = (Float ) total.getValue(); 214 Float val = (Float ) 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 (percentage); 222 } 223 } 224 225 228 public static class DoublePercentageCalculator implements JRPercentageCalculator 229 { 230 public Object calculatePercentage(JRCalculable value, JRCalculable total) 231 { 232 Double totalVal = (Double ) total.getValue(); 233 Double val = (Double ) 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 (percentage); 241 } 242 } 243 244 245 248 public static class BigDecimalPercentageCalculator implements JRPercentageCalculator 249 { 250 public Object calculatePercentage(JRCalculable value, JRCalculable total) 251 { 252 BigDecimal totalVal = (BigDecimal ) total.getValue(); 253 BigDecimal val = (BigDecimal ) value.getValue(); 254 BigDecimal 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 272 public static class BigIntegerPercentageCalculator implements JRPercentageCalculator 273 { 274 public Object calculatePercentage(JRCalculable value, JRCalculable total) 275 { 276 BigInteger totalVal = (BigInteger ) total.getValue(); 277 BigInteger val = (BigInteger ) value.getValue(); 278 BigInteger 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 |