1 28 package net.sf.jasperreports.engine.fill; 29 30 import net.sf.jasperreports.engine.JRVariable; 31 32 33 37 public class JRFloatIncrementerFactory extends JRAbstractExtendedIncrementerFactory 38 { 39 40 41 44 protected static final Float ZERO = new Float (0); 45 46 47 50 private static JRFloatIncrementerFactory mainInstance = new JRFloatIncrementerFactory(); 51 52 53 56 public JRFloatIncrementerFactory() 57 { 58 } 59 60 61 64 public static JRFloatIncrementerFactory getInstance() 65 { 66 return mainInstance; 67 } 68 69 70 73 public JRExtendedIncrementer getExtendedIncrementer(byte calculation) 74 { 75 JRExtendedIncrementer incrementer = null; 76 77 switch (calculation) 78 { 79 case JRVariable.CALCULATION_COUNT : 80 { 81 incrementer = JRFloatCountIncrementer.getInstance(); 82 break; 83 } 84 case JRVariable.CALCULATION_SUM : 85 { 86 incrementer = JRFloatSumIncrementer.getInstance(); 87 break; 88 } 89 case JRVariable.CALCULATION_AVERAGE : 90 { 91 incrementer = JRFloatAverageIncrementer.getInstance(); 92 break; 93 } 94 case JRVariable.CALCULATION_LOWEST : 95 case JRVariable.CALCULATION_HIGHEST : 96 { 97 incrementer = JRComparableIncrementerFactory.getInstance().getExtendedIncrementer(calculation); 98 break; 99 } 100 case JRVariable.CALCULATION_STANDARD_DEVIATION : 101 { 102 incrementer = JRFloatStandardDeviationIncrementer.getInstance(); 103 break; 104 } 105 case JRVariable.CALCULATION_VARIANCE : 106 { 107 incrementer = JRFloatVarianceIncrementer.getInstance(); 108 break; 109 } 110 case JRVariable.CALCULATION_DISTINCT_COUNT : 111 { 112 incrementer = JRFloatDistinctCountIncrementer.getInstance(); 113 break; 114 } 115 case JRVariable.CALCULATION_SYSTEM : 116 case JRVariable.CALCULATION_NOTHING : 117 case JRVariable.CALCULATION_FIRST : 118 default : 119 { 120 incrementer = JRDefaultIncrementerFactory.getInstance().getExtendedIncrementer(calculation); 121 break; 122 } 123 } 124 125 return incrementer; 126 } 127 128 129 } 130 131 132 135 class JRFloatCountIncrementer extends JRAbstractExtendedIncrementer 136 { 137 140 private static JRFloatCountIncrementer mainInstance = new JRFloatCountIncrementer(); 141 142 145 private JRFloatCountIncrementer() 146 { 147 } 148 149 152 public static JRFloatCountIncrementer getInstance() 153 { 154 return mainInstance; 155 } 156 157 160 public Object increment( 161 JRCalculable variable, 162 Object expressionValue, 163 AbstractValueProvider valueProvider 164 ) 165 { 166 Number value = (Number )variable.getIncrementedValue(); 167 168 if (value == null || variable.isInitialized()) 169 { 170 value = JRFloatIncrementerFactory.ZERO; 171 } 172 173 if (expressionValue == null) 174 { 175 return value; 176 } 177 178 return new Float (value.floatValue() + 1); 179 } 180 181 182 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 183 { 184 Number value = (Number )calculable.getIncrementedValue(); 185 Number combineValue = (Number ) calculableValue.getValue(); 186 187 if (value == null || calculable.isInitialized()) 188 { 189 value = JRFloatIncrementerFactory.ZERO; 190 } 191 192 if (combineValue == null) 193 { 194 return value; 195 } 196 197 return new Float (value.floatValue() + combineValue.floatValue()); 198 } 199 200 201 public Object initialValue() 202 { 203 return JRFloatIncrementerFactory.ZERO; 204 } 205 } 206 207 208 211 class JRFloatDistinctCountIncrementer extends JRAbstractExtendedIncrementer 212 { 213 216 private static JRFloatDistinctCountIncrementer mainInstance = new JRFloatDistinctCountIncrementer(); 217 218 221 private JRFloatDistinctCountIncrementer() 222 { 223 } 224 225 228 public static JRFloatDistinctCountIncrementer getInstance() 229 { 230 return mainInstance; 231 } 232 233 236 public Object increment( 237 JRCalculable variable, 238 Object expressionValue, 239 AbstractValueProvider valueProvider 240 ) 241 { 242 DistinctCountHolder holder = 243 (DistinctCountHolder)valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 244 245 if (variable.isInitialized()) 246 { 247 holder.init(); 248 } 249 250 return new Float (holder.getCount()); 251 } 252 253 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 254 { 255 DistinctCountHolder holder = 256 (DistinctCountHolder)valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT)); 257 258 return new Float (holder.getCount()); 259 } 260 261 public Object initialValue() 262 { 263 return JRFloatIncrementerFactory.ZERO; 264 } 265 } 266 267 268 271 class JRFloatSumIncrementer extends JRAbstractExtendedIncrementer 272 { 273 276 private static JRFloatSumIncrementer mainInstance = new JRFloatSumIncrementer(); 277 278 281 private JRFloatSumIncrementer() 282 { 283 } 284 285 288 public static JRFloatSumIncrementer getInstance() 289 { 290 return mainInstance; 291 } 292 293 296 public Object increment( 297 JRCalculable variable, 298 Object expressionValue, 299 AbstractValueProvider valueProvider 300 ) 301 { 302 Number value = (Number )variable.getIncrementedValue(); 303 Number newValue = (Number )expressionValue; 304 305 if (newValue == null) 306 { 307 if (variable.isInitialized()) 308 { 309 return null; 310 } 311 312 return value; 313 } 314 315 if (value == null || variable.isInitialized()) 316 { 317 value = JRFloatIncrementerFactory.ZERO; 318 } 319 320 return new Float (value.floatValue() + newValue.floatValue()); 321 } 322 323 324 public Object initialValue() 325 { 326 return JRFloatIncrementerFactory.ZERO; 327 } 328 } 329 330 331 334 class JRFloatAverageIncrementer extends JRAbstractExtendedIncrementer 335 { 336 339 private static JRFloatAverageIncrementer mainInstance = new JRFloatAverageIncrementer(); 340 341 344 private JRFloatAverageIncrementer() 345 { 346 } 347 348 351 public static JRFloatAverageIncrementer getInstance() 352 { 353 return mainInstance; 354 } 355 356 359 public Object increment( 360 JRCalculable variable, 361 Object expressionValue, 362 AbstractValueProvider valueProvider 363 ) 364 { 365 if (expressionValue == null) 366 { 367 if (variable.isInitialized()) 368 { 369 return null; 370 } 371 return variable.getValue(); 372 } 373 Number countValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 374 Number sumValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM)); 375 return new Float (sumValue.floatValue() / countValue.floatValue()); 376 } 377 378 379 public Object initialValue() 380 { 381 return JRFloatIncrementerFactory.ZERO; 382 } 383 } 384 385 386 389 class JRFloatStandardDeviationIncrementer extends JRAbstractExtendedIncrementer 390 { 391 394 private static JRFloatStandardDeviationIncrementer mainInstance = new JRFloatStandardDeviationIncrementer(); 395 396 399 private JRFloatStandardDeviationIncrementer() 400 { 401 } 402 403 406 public static JRFloatStandardDeviationIncrementer getInstance() 407 { 408 return mainInstance; 409 } 410 411 414 public Object increment( 415 JRCalculable variable, 416 Object expressionValue, 417 AbstractValueProvider valueProvider 418 ) 419 { 420 if (expressionValue == null) 421 { 422 if (variable.isInitialized()) 423 { 424 return null; 425 } 426 return variable.getValue(); 427 } 428 Number varianceValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_VARIANCE)); 429 return new Float ( Math.sqrt(varianceValue.doubleValue()) ); 430 } 431 432 433 public Object initialValue() 434 { 435 return JRFloatIncrementerFactory.ZERO; 436 } 437 } 438 439 440 443 class JRFloatVarianceIncrementer extends JRAbstractExtendedIncrementer 444 { 445 448 private static JRFloatVarianceIncrementer mainInstance = new JRFloatVarianceIncrementer(); 449 450 453 private JRFloatVarianceIncrementer() 454 { 455 } 456 457 460 public static JRFloatVarianceIncrementer getInstance() 461 { 462 return mainInstance; 463 } 464 465 468 public Object increment( 469 JRCalculable variable, 470 Object expressionValue, 471 AbstractValueProvider valueProvider 472 ) 473 { 474 Number value = (Number )variable.getIncrementedValue(); 475 Number newValue = (Number )expressionValue; 476 477 if (newValue == null) 478 { 479 if (variable.isInitialized()) 480 { 481 return null; 482 } 483 484 return value; 485 } 486 else if (value == null || variable.isInitialized()) 487 { 488 return JRFloatIncrementerFactory.ZERO; 489 } 490 else 491 { 492 Number countValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_COUNT)); 493 Number sumValue = (Number )valueProvider.getValue(variable.getHelperVariable(JRCalculable.HELPER_SUM)); 494 return 495 new Float ( 496 (countValue.floatValue() - 1) * value.floatValue() / countValue.floatValue() + 497 ( sumValue.floatValue() / countValue.floatValue() - newValue.floatValue() ) * 498 ( sumValue.floatValue() / countValue.floatValue() - newValue.floatValue() ) / 499 (countValue.floatValue() - 1) 500 ); 501 } 502 } 503 504 public Object combine(JRCalculable calculable, JRCalculable calculableValue, AbstractValueProvider valueProvider) 505 { 506 Number value = (Number )calculable.getIncrementedValue(); 507 508 if (calculableValue.getValue() == null) 509 { 510 if (calculable.isInitialized()) 511 { 512 return null; 513 } 514 515 return value; 516 } 517 else if (value == null || calculable.isInitialized()) 518 { 519 return new Float (((Number ) calculableValue.getIncrementedValue()).floatValue()); 520 } 521 522 float v1 = value.floatValue(); 523 float c1 = ((Number ) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_COUNT))).floatValue(); 524 float s1 = ((Number ) valueProvider.getValue(calculable.getHelperVariable(JRCalculable.HELPER_SUM))).floatValue(); 525 526 float v2 = ((Number ) calculableValue.getIncrementedValue()).floatValue(); 527 float c2 = ((Number ) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_COUNT))).floatValue(); 528 float s2 = ((Number ) valueProvider.getValue(calculableValue.getHelperVariable(JRCalculable.HELPER_SUM))).floatValue(); 529 530 c1 -= c2; 531 s1 -= s2; 532 533 float c = c1 + c2; 534 535 return new Float ( 536 c1 / c * v1 + 537 c2 / c * v2 + 538 c2 / c1 * s1 / c * s1 / c + 539 c1 / c2 * s2 / c * s2 / c - 540 2 * s1 / c * s2 /c 541 ); 542 } 543 544 545 public Object initialValue() 546 { 547 return JRFloatIncrementerFactory.ZERO; 548 } 549 } 550 | Popular Tags |