OffsetRangeTracker extends RestrictionTracker<OffsetRange, Long> implements HasProgress { @Override public boolean tryClaim(Long i) { checkArgument( lastAttemptedOffset == null || i > lastAttemptedOffset, "Trying to claim offset %s while last attempted was %s", i, lastAttemptedOffset); checkArgument( i >= range.getFrom(), "Trying to claim offset %s before start of the range %s", i, range); lastAttemptedOffset = i; if (i >= range.getTo()) { return false; } lastClaimedOffset = i; return true; } 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 testTryClaim() throws Exception { OffsetRange range = new OffsetRange(100, 200); OffsetRangeTracker tracker = new OffsetRangeTracker(range); assertEquals(range, tracker.currentRestriction()); assertTrue(tracker.tryClaim(100L)); assertTrue(tracker.tryClaim(150L)); assertTrue(tracker.tryClaim(199L)); assertFalse(tracker.tryClaim(200L)); }
@Test public void testNonMonotonicClaim() throws Exception { expected.expectMessage("Trying to claim offset 103 while last attempted was 110"); OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 200)); assertTrue(tracker.tryClaim(105L)); assertTrue(tracker.tryClaim(110L)); tracker.tryClaim(103L); }
@Test public void testClaimBeforeStartOfRange() throws Exception { expected.expectMessage("Trying to claim offset 90 before start of the range [100, 200)"); OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 200)); tracker.tryClaim(90L); }