MCPcopy Index your code
hub / github.com/apache/groovy / calcSize

Method calcSize

src/main/java/groovy/lang/NumberRange.java:496–543  ·  view source on GitHub ↗

Calculates and caches the size for the supplied range parameters. @param from the lower bound @param to the upper bound @param stepSize the step size

(Comparable from, Comparable to, Number stepSize)

Source from the content-addressed store, hash-verified

494 * @param stepSize the step size
495 */
496 void calcSize(Comparable from, Comparable to, Number stepSize) {
497 if (from == to && !inclusiveLeft && !inclusiveRight) {
498 size = 0;
499 return;
500 }
501 int tempsize = 0;
502 boolean shortcut = false;
503 if (isIntegral(stepSize)) {
504 if ((from instanceof Integer || from instanceof Long)
505 && (to instanceof Integer || to instanceof Long)) {
506 // let's fast calculate the size
507 final BigInteger fromTemp = new BigInteger(from.toString());
508 final BigInteger fromNum = inclusiveLeft ? fromTemp : fromTemp.add(BigInteger.ONE);
509 final BigInteger toTemp = new BigInteger(to.toString());
510 final BigInteger toNum = inclusiveRight ? toTemp : toTemp.subtract(BigInteger.ONE);
511 final BigInteger sizeNum = new BigDecimal(toNum.subtract(fromNum)).divide(BigDecimal.valueOf(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
512 tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) < 0 ? sizeNum.intValue() : Integer.MAX_VALUE;
513 shortcut = true;
514 } else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) ||
515 ((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
516 // let's fast calculate the size
517 final BigDecimal fromTemp = NumberMath.toBigDecimal((Number) from);
518 final BigDecimal fromNum = inclusiveLeft ? fromTemp : fromTemp.add(BigDecimal.ONE);
519 final BigDecimal toTemp = NumberMath.toBigDecimal((Number) to);
520 final BigDecimal toNum = inclusiveRight ? toTemp : toTemp.subtract(BigDecimal.ONE);
521 final BigInteger sizeNum = toNum.subtract(fromNum).divide(BigDecimal.valueOf(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
522 tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) < 0 ? sizeNum.intValue() : Integer.MAX_VALUE;
523 shortcut = true;
524 }
525 }
526 if (!shortcut) {
527 // let's brute-force calculate the size by iterating start to end
528 final Iterator iter = new StepIterator(this, stepSize);
529 while (iter.hasNext()) {
530 tempsize++;
531 // integer overflow
532 if (tempsize < 0) {
533 break;
534 }
535 iter.next();
536 }
537 // integer overflow
538 if (tempsize < 0) {
539 tempsize = Integer.MAX_VALUE;
540 }
541 }
542 size = tempsize;
543 }
544
545 private boolean isIntegral(Number stepSize) {
546 BigDecimal tempStepSize = NumberMath.toBigDecimal(stepSize);

Callers 1

sizeMethod · 0.95

Calls 13

isIntegralMethod · 0.95
toBigDecimalMethod · 0.95
hasNextMethod · 0.95
nextMethod · 0.95
subtractMethod · 0.80
divideMethod · 0.80
toStringMethod · 0.65
addMethod · 0.65
longValueMethod · 0.65
intValueMethod · 0.65
toBigIntegerMethod · 0.45
valueOfMethod · 0.45

Tested by

no test coverage detected