SequenceIDBuffer implements IndexedSortable { private int binarySearch(long value) { int low = 0; int high = size() - 1; while (low <= high) { int mid = (low + high) >>> 1; long midVal = get(mid); if (midVal < value) { low = mid + 1; } else if (midVal > value) { high = mid - 1; } else { return mid; } } return -(low + 1); } SequenceIDBuffer(int size); @Override int compare(int leftIndex, int rightIndex); boolean exists(long value); @Override void swap(int leftIndex, int rightIndex); long get(int index); void put(int index, long value); int size(); void close(); void sort(); static void main(String[] args);  }
@Test public void testBinarySearch() { int size = 100; SequenceIDBuffer buffer = new SequenceIDBuffer(size); Assert.assertEquals(size, buffer.size()); for (int i = 0; i < 100; i++) { buffer.put(i, i); } buffer.sort(); Assert.assertFalse(buffer.exists(-1)); Assert.assertFalse(buffer.exists(101)); for (int i = 0; i < 100; i++) { Assert.assertTrue(buffer.exists(i)); } }