Timestamps { public static int findNearestSuccessorForTimestamp( Set<Timestamp> timestamps, Timestamp sought) { int start = 0; int end = timestamps.size() - 1; while (start <= end) { int mid = (start + end) / 2; Timestamp stored = Iterables.get(timestamps, mid); if(sought.getMicros() == stored.getMicros()) { return mid; } else if(sought.getMicros() > stored.getMicros()) { start = mid + 1; } else { end = mid - 1; } } return start; }  static int findNearestSuccessorForTimestamp(
            Set<Timestamp> timestamps, Timestamp sought); static int findNearestSuccessorForTimestamp(Set<Long> timestamps,
            long sought);  }
@Test public void testFindNearestSuccessorForTimestampWithStartTimestampLessThanFirstTimestampInChronology() { Set<Timestamp> timestamps = new LinkedHashSet<Timestamp>(); timestamps.add(Timestamp.fromMicros(1000L)); for (int i = 0; i < Random.getScaleCount(); i++) { long increment = 0; while (increment == 0) { increment = Math.abs(Random.getScaleCount()); } timestamps.add(Timestamp.fromMicros( Iterables.getLast(timestamps).getMicros() + increment)); } Timestamp startTimestamp = Timestamp.epoch(); Assert.assertEquals(0, Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); }
@Test public void testFindNearestSuccessorForTimestampWithStartTimestampGreaterThanLastTimestampInChronology() { Set<Timestamp> timestamps = new LinkedHashSet<Timestamp>(); timestamps.add(Timestamp.fromMicros(1000L)); for (int i = 0; i < Random.getScaleCount(); i++) { long increment = 0; while (increment == 0) { increment = Math.abs(Random.getScaleCount()); } timestamps.add(Timestamp.fromMicros( Iterables.getLast(timestamps).getMicros() + increment)); } Timestamp startTimestamp = Timestamp .fromMicros(Iterables.getLast(timestamps).getMicros() + 1000L); Assert.assertEquals(timestamps.size(), Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); }
@Test public void testFindNearestSuccessorForTimestampWithStartTimestampEqualToATimestampInChronology() { Set<Timestamp> timestamps = new LinkedHashSet<Timestamp>(); timestamps.add(Timestamp.fromMicros(1000L)); for (int i = 0; i < Random.getScaleCount(); i++) { long increment = 0; while (increment == 0) { increment = Math.abs(Random.getScaleCount()); } timestamps.add(Timestamp.fromMicros( Iterables.getLast(timestamps).getMicros() + increment)); } Timestamp startTimestamp = Timestamp .fromMicros(Iterables.getFirst(timestamps, null).getMicros()); Assert.assertEquals(0, Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); startTimestamp = Timestamp.fromMicros( Iterables.get(timestamps, timestamps.size() / 2).getMicros()); Assert.assertEquals(timestamps.size() / 2, Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); startTimestamp = Timestamp .fromMicros(Iterables.getLast(timestamps).getMicros()); Assert.assertEquals(timestamps.size() - 1, Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); }
@Test public void testFindNearestSuccessorForTimestampWithStartTimestampGreaterThanFirstTimestampAndLessThanLastTimestampInChronology() { Set<Timestamp> timestamps = new LinkedHashSet<Timestamp>(); timestamps.add(Timestamp.fromMicros(1000L)); for (int i = 0; i < Random.getScaleCount(); i++) { long increment = 0; while (increment == 0) { increment = Math.abs(Random.getScaleCount()) + 100L; } timestamps.add(Timestamp.fromMicros( Iterables.getLast(timestamps).getMicros() + increment)); } Timestamp abitrary = Iterables.get(timestamps, timestamps.size() / 3); Timestamp abitrarySuccessor = Iterables.get(timestamps, timestamps.size() / 3 + 1); Timestamp startTimestamp = Timestamp.fromMicros( (abitrary.getMicros() + abitrarySuccessor.getMicros()) / 2); Assert.assertEquals(timestamps.size() / 3 + 1, Timestamps .findNearestSuccessorForTimestamp(timestamps, startTimestamp)); }