MemoryLogDataStore extends AbstractMemoryLogStore implements LogDataStore { protected int getIndexToInsert(Date date, final int downLimit, final int upLimit, int startAt) { int startPoint = startAt; int effectiveDownLimit = downLimit; Date dateInList = list.get(startPoint).getDate(); int compareTo = date.compareTo(dateInList); int effectiveUpLimit = upLimit; if (effectiveUpLimit - effectiveDownLimit < 3) { for (int i = effectiveUpLimit; i >= effectiveDownLimit; i--) { dateInList = list.get(i).getDate(); compareTo = date.compareTo(dateInList); if (compareTo == 0) { return i; } else if (compareTo > 0) { return i + 1; } } return effectiveDownLimit; } if (compareTo < 0) { effectiveUpLimit = startPoint; } else if (compareTo > 0) { effectiveDownLimit = startPoint; } else { return startPoint; } startPoint = (effectiveDownLimit + effectiveUpLimit) / 2; return getIndexToInsert(date, effectiveDownLimit, effectiveUpLimit, startPoint); } MemoryLogDataStore(); @Override int getCount(); @Override void add(LogData... logDatas); @Override void remove(int... rows); @Override LogData getLogData(int id); @Override LogData[] getLogData(); @Override int clear(); @Override Iterator<LogData> iterator(); @Override Integer getLogDataIdInRow(int row);  }
@Test public void testGetIndexToInsert() { MemoryLogDataStore memoryLogDataStore = (MemoryLogDataStore) logDataStore; for (int i = 0; i < 100; i++) { logDataStore.add(new LogDataBuilder().withId(i).withDate(new Date(i)).build()); } int indexToInsert = memoryLogDataStore.getIndexToInsert(new Date(40), 0, logDataStore.getCount(), logDataStore.getCount() / 2); assertEquals(40, indexToInsert); }