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

Method size

src/main/java/groovy/lang/ObjectRange.java:309–357  ·  view source on GitHub ↗

{@inheritDoc}

()

Source from the content-addressed store, hash-verified

307 * {@inheritDoc}
308 */
309 @Override
310 public int size() {
311 if (size == -1) {
312 int tempsize = 0;
313 if ((from instanceof Integer || from instanceof Long)
314 && (to instanceof Integer || to instanceof Long)) {
315 // let's fast calculate the size
316 final BigInteger fromNum = new BigInteger(from.toString());
317 final BigInteger toNum = new BigInteger(to.toString());
318 final BigInteger sizeNum = toNum.subtract(fromNum).add(new BigInteger("1"));
319 tempsize = sizeNum.intValue();
320 if (!BigInteger.valueOf(tempsize).equals(sizeNum)) {
321 tempsize = Integer.MAX_VALUE;
322 }
323 } else if (from instanceof Character && to instanceof Character) {
324 // let's fast calculate the size
325 final char fromNum = (Character) from;
326 final char toNum = (Character) to;
327 tempsize = toNum - fromNum + 1;
328 } else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) ||
329 ((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
330 // let's fast calculate the size
331 final BigDecimal fromNum = NumberMath.toBigDecimal((Number) from);
332 final BigDecimal toNum = NumberMath.toBigDecimal((Number) to);
333 final BigInteger sizeNum = toNum.subtract(fromNum).add(BigDecimal.ONE).toBigInteger();
334 tempsize = sizeNum.intValue();
335 if (!BigInteger.valueOf(tempsize).equals(sizeNum)) {
336 tempsize = Integer.MAX_VALUE;
337 }
338 } else {
339 // let's brute-force calculate the size by iterating start to end
340 final Iterator<Comparable> iter = new StepIterator(this, 1);
341 while (iter.hasNext()) {
342 tempsize++;
343 // integer overflow
344 if (tempsize < 0) {
345 break;
346 }
347 iter.next();
348 }
349 }
350 // integer overflow
351 if (tempsize < 0) {
352 tempsize = Integer.MAX_VALUE;
353 }
354 size = tempsize;
355 }
356 return size;
357 }
358
359 /**
360 * {@inheritDoc}

Callers

nothing calls this directly

Calls 10

toBigDecimalMethod · 0.95
hasNextMethod · 0.95
nextMethod · 0.95
subtractMethod · 0.80
toStringMethod · 0.65
addMethod · 0.65
intValueMethod · 0.65
equalsMethod · 0.65
valueOfMethod · 0.45
toBigIntegerMethod · 0.45

Tested by

no test coverage detected