@Test public void testTokenSearch() throws Exception { List<Integer> list = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90, 100); DynoBinarySearch<Integer> search = new DynoBinarySearch<Integer>(list); for (int i = 0; i <= 133; i++) { Integer token = search.getTokenOwner(i); Integer expected = getExpectedToken(i); Assert.assertEquals(expected, token); } }
public T getTokenOwner(T token) { if (listEmpty.get()) { return null; } if (rangeList.size() == 1) { return rangeList.get(0).getTokenOwner(); } DynoTokenRange<T> firstRange = rangeList.get(0); DynoTokenRange<T> lastRange = rangeList.get(rangeList.size() - 1); if (firstRange.compareTo(token) == 0) { return firstRange.getTokenOwner(); } if (lastRange.compareTo(token) < 0) { return firstRange.getTokenOwner(); } int index = Collections.binarySearch(rangeList, token); if (index < 0) { throw new RuntimeException("Token not found!: " + token); } return rangeList.get(index).getTokenOwner(); }
DynoBinarySearch { public T getTokenOwner(T token) { if (listEmpty.get()) { return null; } if (rangeList.size() == 1) { return rangeList.get(0).getTokenOwner(); } DynoTokenRange<T> firstRange = rangeList.get(0); DynoTokenRange<T> lastRange = rangeList.get(rangeList.size() - 1); if (firstRange.compareTo(token) == 0) { return firstRange.getTokenOwner(); } if (lastRange.compareTo(token) < 0) { return firstRange.getTokenOwner(); } int index = Collections.binarySearch(rangeList, token); if (index < 0) { throw new RuntimeException("Token not found!: " + token); } return rangeList.get(index).getTokenOwner(); } }
DynoBinarySearch { public T getTokenOwner(T token) { if (listEmpty.get()) { return null; } if (rangeList.size() == 1) { return rangeList.get(0).getTokenOwner(); } DynoTokenRange<T> firstRange = rangeList.get(0); DynoTokenRange<T> lastRange = rangeList.get(rangeList.size() - 1); if (firstRange.compareTo(token) == 0) { return firstRange.getTokenOwner(); } if (lastRange.compareTo(token) < 0) { return firstRange.getTokenOwner(); } int index = Collections.binarySearch(rangeList, token); if (index < 0) { throw new RuntimeException("Token not found!: " + token); } return rangeList.get(index).getTokenOwner(); } DynoBinarySearch(List<T> list); }
DynoBinarySearch { public T getTokenOwner(T token) { if (listEmpty.get()) { return null; } if (rangeList.size() == 1) { return rangeList.get(0).getTokenOwner(); } DynoTokenRange<T> firstRange = rangeList.get(0); DynoTokenRange<T> lastRange = rangeList.get(rangeList.size() - 1); if (firstRange.compareTo(token) == 0) { return firstRange.getTokenOwner(); } if (lastRange.compareTo(token) < 0) { return firstRange.getTokenOwner(); } int index = Collections.binarySearch(rangeList, token); if (index < 0) { throw new RuntimeException("Token not found!: " + token); } return rangeList.get(index).getTokenOwner(); } DynoBinarySearch(List<T> list); T getTokenOwner(T token); String toString(); }
DynoBinarySearch { public T getTokenOwner(T token) { if (listEmpty.get()) { return null; } if (rangeList.size() == 1) { return rangeList.get(0).getTokenOwner(); } DynoTokenRange<T> firstRange = rangeList.get(0); DynoTokenRange<T> lastRange = rangeList.get(rangeList.size() - 1); if (firstRange.compareTo(token) == 0) { return firstRange.getTokenOwner(); } if (lastRange.compareTo(token) < 0) { return firstRange.getTokenOwner(); } int index = Collections.binarySearch(rangeList, token); if (index < 0) { throw new RuntimeException("Token not found!: " + token); } return rangeList.get(index).getTokenOwner(); } DynoBinarySearch(List<T> list); T getTokenOwner(T token); String toString();  }