1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.math.BigDecimal ; 31 32 import net.sf.jasperreports.engine.JRVariable; 33 34 35 39 public class JRBigDecimalIncrementerFactory extends JRAbstractExtendedIncrementerFactory 40 { 41 42 43 46 protected static final BigDecimal ZERO = new BigDecimal ("0"); 47 protected static final BigDecimal ONE = new BigDecimal ("1"); 48 49 50 53 private static JRBigDecimalIncrementerFactory mainInstance = new JRBigDecimalIncrementerFactory(); 54 55 56 59 private JRBigDecimalIncrementerFactory() 60 { 61 } 62 63 64 67 public static JRBigDecimalIncrementerFactory getInstance() 68 { 69 return mainInstance; 70 } 71 72 73 76 public JRExtendedIncrementer getExtendedIncrementer(byte calculation) 77 { 78 JRExtendedIncrementer incrementer = null; 79 80 switch (calculation) 81 { 82 case JRVariable.CALCULATION_COUNT : 83 { 84 incrementer = JRBigDecimalCountIncrementer.getInstance(); 85 break; 86 } 87 case JRVariable.CALCULATION_SUM : 88 { 89 incrementer = JRBigDecimalSumIncrementer.getInstance(); 90 break; 91 } 92 case JRVariable.CALCULATION_AVERAGE : 93 { 94 incrementer = JRBigDecimalAverageIncrementer.getInstance(); 95 break; 96 } 97 case JRVariable.CALCULATION_LOWEST : 98 case JRVariable.CALCULATION_HIGHEST : 99 { 100 incrementer = JRComparableIncrementerFactory.getInstance().getExtendedIncrementer(calculation); 101 break; 102 } 103 case JRVariable.CALCULATION_STANDARD_DEVIATION : 104 { 105 incrementer = JRBigDecimalStandardDeviationIncrementer.getInstance(); 106 break; 107 } 108 case JRVariable.CALCULATION_VARIANCE : 109 { 110 incrementer = JRBigDecimalVarianceIncrementer.getInstance(); 111 break; 112 } 113 case JRVariable.CALCULATION_DISTINCT_COUNT : 114 { 115 incrementer = JRBigDecimalDistinctCountIncrementer.getInstance(); 116 break; 117 } 118 case JRVariable.CALCULATION_SYSTEM : 119 case JRVariable.CALCULATION_NOTHING : 120 case JRVariable.CALCULATION_FIRST : 121 default : 122 { 123 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation); 124 break; 125 } 126 } 127 128 return incrementer; 129 } 130 131 132 } 133 134 135 138 class JRBigDecimalCountIncrementer extends JRAbstractExtendedIncrementer 139 { 140 143 private static JRBigDecimalCountIncrementer mainInstance = new JRBigDecimalCountIncrementer(); 144 145 148 private JRBigDecimalCountIncrementer() 149 { 150 } 151 152 155 public static JRBigDecimalCountIncrementer getInstance() 156 { 157 return mainInstance; 158 } 159 160 163 public Object increment( 164 JRCalculable variable, 165 Object expressionValue, 166 AbstractValueProvider valueProvider 167 ) 168 { 169 BigDecimal value = (BigDecimal )variable.getIncrementedValue(); 170 171 if (value == null || variable.isInitialized()) 172 { 173 value = JRBigDecimalIncrementerFactory.ZERO; 174 } 175 176 if (expressionValue == null) 177 { 178 return value; 179 } 180 181 return value.add(JRBigDecimalIncrementerFactory.ONE); 182 } 183 184 185 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 186 { 187 BigDecimal value = (BigDecimal )calculable.getIncrementedValue(); 188 BigDecimal combineValue = (BigDecimal ) calculableValue.getValue(); 189 190 if (value == null || calculable.isInitialized()) 191 { 192 value = JRBigDecimalIncrementerFactory.ZERO; 193 } 194 195 if (combineValue == null) 196 { 197 return value; 198 } 199 200 return value.add(combineValue); 201 } 202 203 204 public Object initialValue() 205 { 206 return JRBigDecimalIncrementerFactory.ZERO; 207 } 208 } 209 210 211 214 class JRBigDecimalDistinctCountIncrementer extends JRAbstractExtendedIncrementer 215 { 216 219 private static JRBigDecimalDistinctCountIncrementer mainInstance = new JRBigDecimalDistinctCountIncrementer(); 220 221 224 private JRBigDecimalDistinctCountIncrementer() 225 { 226 } 227 228 231 public static JRBigDecimalDistinctCountIncrementer getInstance() 232 { 233 return mainInstance; 234 } 235 236 239 public Object increment( 240 JRCalculable variable, 241 Object expressionValue, 242 AbstractValueProvider valueProvider 243 ) 244 { 245 DistinctCountHolder holder = 246 (DistinctCountHolder)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 247 248 if (variable.isInitialized()) 249 { 250 holder.init(); 251 } 252 253 return new BigDecimal (holder.getCount()); 254 } 255 256 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 257 { 258 DistinctCountHolder holder = 259 (DistinctCountHolder)valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT)); 260 261 return new BigDecimal (holder.getCount()); 262 } 263 264 public Object initialValue() 265 { 266 return JRBigDecimalIncrementerFactory.ZERO; 267 } 268 } 269 270 271 274 class JRBigDecimalSumIncrementer extends JRAbstractExtendedIncrementer 275 { 276 279 private static JRBigDecimalSumIncrementer mainInstance = new JRBigDecimalSumIncrementer(); 280 281 284 private JRBigDecimalSumIncrementer() 285 { 286 } 287 288 291 public static JRBigDecimalSumIncrementer getInstance() 292 { 293 return mainInstance; 294 } 295 296 299 public Object increment( 300 JRCalculable variable, 301 Object expressionValue, 302 AbstractValueProvider valueProvider 303 ) 304 { 305 BigDecimal value = (BigDecimal )variable.getIncrementedValue(); 306 BigDecimal newValue = (BigDecimal )expressionValue; 307 308 if (newValue == null) 309 { 310 if (variable.isInitialized()) 311 { 312 return null; 313 } 314 315 return value; 316 } 317 318 if (value == null || variable.isInitialized()) 319 { 320 value = JRBigDecimalIncrementerFactory.ZERO; 321 } 322 323 return value.add(newValue); 324 } 325 326 327 public Object initialValue() 328 { 329 return JRBigDecimalIncrementerFactory.ZERO; 330 } 331 } 332 333 334 337 class JRBigDecimalAverageIncrementer extends JRAbstractExtendedIncrementer 338 { 339 342 private static JRBigDecimalAverageIncrementer mainInstance = new JRBigDecimalAverageIncrementer(); 343 344 347 private JRBigDecimalAverageIncrementer() 348 { 349 } 350 351 354 public static JRBigDecimalAverageIncrementer getInstance() 355 { 356 return mainInstance; 357 } 358 359 362 public Object increment( 363 JRCalculable variable, 364 Object expressionValue, 365 AbstractValueProvider valueProvider 366 ) 367 { 368 if (expressionValue == null) 369 { 370 if (variable.isInitialized()) 371 { 372 return null; 373 } 374 return variable.getValue(); 375 } 376 BigDecimal countValue = (BigDecimal )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 377 BigDecimal sumValue = (BigDecimal )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM)); 378 return sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP); 379 } 380 381 382 public Object initialValue() 383 { 384 return JRBigDecimalIncrementerFactory.ZERO; 385 } 386 } 387 388 389 392 class JRBigDecimalStandardDeviationIncrementer extends JRAbstractExtendedIncrementer 393 { 394 397 private static JRBigDecimalStandardDeviationIncrementer mainInstance = new JRBigDecimalStandardDeviationIncrementer(); 398 399 402 private JRBigDecimalStandardDeviationIncrementer() 403 { 404 } 405 406 409 public static JRBigDecimalStandardDeviationIncrementer getInstance() 410 { 411 return mainInstance; 412 } 413 414 417 public Object increment( 418 JRCalculable variable, 419 Object expressionValue, 420 AbstractValueProvider valueProvider 421 ) 422 { 423 if (expressionValue == null) 424 { 425 if (variable.isInitialized()) 426 { 427 return null; 428 } 429 return variable.getValue(); 430 } 431 Number varianceValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_VARIANCE)); 432 return new BigDecimal ( Math.sqrt(varianceValue.doubleValue()) ); 433 } 434 435 436 public Object initialValue() 437 { 438 return JRBigDecimalIncrementerFactory.ZERO; 439 } 440 } 441 442 443 446 class JRBigDecimalVarianceIncrementer extends JRAbstractExtendedIncrementer 447 { 448 451 private static JRBigDecimalVarianceIncrementer mainInstance = new JRBigDecimalVarianceIncrementer(); 452 453 456 private JRBigDecimalVarianceIncrementer() 457 { 458 } 459 460 463 public static JRBigDecimalVarianceIncrementer getInstance() 464 { 465 return mainInstance; 466 } 467 468 471 public Object increment( 472 JRCalculable variable, 473 Object expressionValue, 474 AbstractValueProvider valueProvider 475 ) 476 { 477 BigDecimal value = (BigDecimal )variable.getIncrementedValue(); 478 BigDecimal newValue = (BigDecimal )expressionValue; 479 480 if (newValue == null) 481 { 482 if (variable.isInitialized()) 483 { 484 return null; 485 } 486 return value; 487 } 488 else if (value == null || variable.isInitialized()) 489 { 490 return JRBigDecimalIncrementerFactory.ZERO; 491 } 492 else 493 { 494 BigDecimal countValue = (BigDecimal )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 495 BigDecimal sumValue = (BigDecimal )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM)); 496 return 497 countValue.subtract(JRBigDecimalIncrementerFactory.ONE).multiply(value).divide(countValue, BigDecimal.ROUND_HALF_UP).add( 498 sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP).subtract(newValue).multiply( 499 sumValue.divide(countValue, BigDecimal.ROUND_HALF_UP).subtract(newValue) 500 ).divide(countValue.subtract(JRBigDecimalIncrementerFactory.ONE), BigDecimal.ROUND_HALF_UP) 501 ); 502 } 503 } 504 505 506 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 507 { 508 BigDecimal value = (BigDecimal )calculable.getIncrementedValue(); 509 510 if (calculableValue.getValue() == null) 511 { 512 if (calculable.isInitialized()) 513 { 514 return null; 515 } 516 517 return value; 518 } 519 else if (value == null || calculable.isInitialized()) 520 { 521 return (BigDecimal ) calculableValue.getIncrementedValue(); 522 } 523 524 BigDecimal v1 = value; 525 BigDecimal c1 = (BigDecimal ) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT)); 526 BigDecimal s1 = (BigDecimal ) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_SUM)); 527 528 BigDecimal v2 = (BigDecimal ) calculableValue.getIncrementedValue(); 529 BigDecimal c2 = (BigDecimal ) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_COUNT)); 530 BigDecimal s2 = (BigDecimal ) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_SUM)); 531 532 c1 = c1.subtract(c2); 533 s1 = s1.subtract(s2); 534 535 BigDecimal c = c1.add(c2); 536 537 BigDecimal x1 = s1.divide(c, BigDecimal.ROUND_HALF_UP); 538 BigDecimal x2 = s2.divide(c, BigDecimal.ROUND_HALF_UP); 539 BigDecimal x3 = x1.multiply(x2); 540 541 return c1.divide(c, BigDecimal.ROUND_HALF_UP).multiply(v1) 542 .add(c2.divide(c, BigDecimal.ROUND_HALF_UP).multiply(v2)) 543 .add(c2.divide(c1, BigDecimal.ROUND_HALF_UP).multiply(x1).multiply(x1)) 544 .add(c1.divide(c2, BigDecimal.ROUND_HALF_UP).multiply(x2).multiply(x2)) 545 .subtract(x3).subtract(x3); 546 } 547 548 549 public Object initialValue() 550 { 551 return JRBigDecimalIncrementerFactory.ZERO; 552 } 553 } 554 | Popular Tags |