ContextExtractor { protected int closest(int key, List<Integer> list) { int size = list.size(); int delta = size / 2; int i = size / 2; while (delta > 1) { logger.debug("i = {} \t delta = {} ", i, delta); int elem = list.get(i); delta = (delta % 2 == 0) ? (delta) / 2 : (delta + 1) / 2; if (elem == key) return i; if (elem > key) { i = Math.max(i - delta, 0); } else { i = Math.min(delta + i, size - 1); } } int elem = list.get(i); if (elem > key) return i - 1; return i; } protected  ContextExtractor();  ContextExtractor(Field field); protected  ContextExtractor(String text); String getContext(String label); int getWindowSize(); void setWindowSize(int windowSize);  }
@Test public void testBinSearch() { ContextExtractor extractor = new ContextExtractor( "diego ceccarelli prova test pippo pluto belen"); List<Integer> test = Arrays.asList(new Integer[] { 1, 5, 10, 50, 100, 110 }); assertEquals(0, extractor.closest(2, test)); assertEquals(0, extractor.closest(3, test)); assertEquals(0, extractor.closest(4, test)); assertEquals(1, extractor.closest(5, test)); assertEquals(4, extractor.closest(105, test)); assertEquals(5, extractor.closest(120, test)); assertEquals(3, extractor.closest(99, test)); test = Arrays.asList(new Integer[] { 0, 1 }); assertEquals(1, extractor.closest(10000, test)); assertEquals(0, extractor.closest(0, test)); test = Arrays.asList(new Integer[] { 0, 1 }); assertEquals(1, extractor.closest(10000, test)); assertEquals(0, extractor.closest(0, test)); test = Arrays.asList(new Integer[] { 1, 5, 10, 50, 100, 110, 1000 }); assertEquals(0, extractor.closest(2, test)); assertEquals(0, extractor.closest(3, test)); assertEquals(0, extractor.closest(4, test)); assertEquals(1, extractor.closest(5, test)); assertEquals(4, extractor.closest(105, test)); assertEquals(5, extractor.closest(120, test)); assertEquals(6, extractor.closest(1000, test)); assertEquals(6, extractor.closest(10000, test)); }