1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.data.pipeline.core.util;
19
20 import org.apache.commons.lang3.Range;
21
22 import java.math.BigInteger;
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
25
26
27
28
29
30
31
32 public final class IntervalToRangeIterator implements Iterator<Range<Long>> {
33
34 private final BigInteger maximum;
35
36 private final BigInteger interval;
37
38 private BigInteger current;
39
40 public IntervalToRangeIterator(final long minimum, final long maximum, final long interval) {
41 if (minimum > maximum) {
42 throw new IllegalArgumentException("minimum greater than maximum");
43 }
44 if (interval < 0L) {
45 throw new IllegalArgumentException("interval is less than zero");
46 }
47 this.maximum = BigInteger.valueOf(maximum);
48 this.interval = BigInteger.valueOf(interval);
49 this.current = BigInteger.valueOf(minimum);
50 }
51
52 @Override
53 public boolean hasNext() {
54 return current.compareTo(maximum) <= 0;
55 }
56
57 @Override
58 public Range<Long> next() {
59 if (!hasNext()) {
60 throw new NoSuchElementException("");
61 }
62 BigInteger upperLimit = min(maximum, current.add(interval));
63 Range<Long> result = Range.between(current.longValue(), upperLimit.longValue());
64 current = upperLimit.add(BigInteger.ONE);
65 return result;
66 }
67
68 private BigInteger min(final BigInteger one, final BigInteger another) {
69 return one.compareTo(another) < 0 ? one : another;
70 }
71 }