OffsetRangeTracker extends RestrictionTracker<OffsetRange, Long> implements HasProgress { @Override public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) { BigDecimal cur = (lastAttemptedOffset == null) ? BigDecimal.valueOf(range.getFrom()).subtract(BigDecimal.ONE, MathContext.DECIMAL128) : BigDecimal.valueOf(lastAttemptedOffset); BigDecimal splitPos = cur.add( BigDecimal.valueOf(range.getTo()) .subtract(cur, MathContext.DECIMAL128) .multiply(BigDecimal.valueOf(fractionOfRemainder), MathContext.DECIMAL128) .max(BigDecimal.ONE), MathContext.DECIMAL128); long split = splitPos.longValue(); if (split >= range.getTo()) { return null; } OffsetRange res = new OffsetRange(split, range.getTo()); this.range = new OffsetRange(range.getFrom(), split); return SplitResult.of(range, res); } OffsetRangeTracker(OffsetRange range); @Override OffsetRange currentRestriction(); @Override SplitResult<OffsetRange> trySplit(double fractionOfRemainder); @Override boolean tryClaim(Long i); @Override void checkDone(); @Override IsBounded isBounded(); @Override String toString(); @Override Progress getProgress();  }
@Test public void testTrySplit() throws Exception { OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 200)); tracker.tryClaim(100L); SplitResult splitRes = tracker.trySplit(0.509); assertEquals(new OffsetRange(100, 150), splitRes.getPrimary()); assertEquals(new OffsetRange(150, 200), splitRes.getResidual()); splitRes = tracker.trySplit(1); assertNull(splitRes); }
@Test public void testTrySplitAtEmptyRange() throws Exception { OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 100)); assertNull(tracker.trySplit(0)); assertNull(tracker.trySplit(0.1)); assertNull(tracker.trySplit(1)); }