SmartIterator { public Map<String, A> applyLimitAndOffset(Map<String, A> map) { iterator = map.entrySet().iterator(); Map<String, A> filteredMap = new LinkedHashMap<String, A>(); int i = 0; while (isRecordRequired(filteredMap.size())) { Entry<String, A> item = (Entry<String, A>) iterator.next(); if (i++ >= offset) { filteredMap.put(item.getKey(), item.getValue()); } } return filteredMap; } SmartIterator(int limit, int offset); Map<String, A> applyLimitAndOffset(Map<String, A> map); List<A> applyLimitAndOffset(Collection<A> unfilteredCollection);  }
@Test public void testNoLimitNoOffset() { smartIterator = new SmartIterator(0, 0); Map<String, Object> unfilteredList = createMap(5); Map<String, Object> filteredList = smartIterator.applyLimitAndOffset(unfilteredList); assertEquals(5, filteredList.size()); }
@Test public void testLimit() { smartIterator = new SmartIterator(2, 0); Map<String, Object> unfilteredList = createMap(5); Map<String, Object> filteredList = smartIterator.applyLimitAndOffset(unfilteredList); assertMapContainsOnly(filteredList, 0, 1); }
@Test public void testHigherLimitThanSources() { smartIterator = new SmartIterator(10, 0); Map<String, Object> unfilteredList = createMap(5); Map<String, Object> filteredList = smartIterator.applyLimitAndOffset(unfilteredList); assertMapContainsOnly(filteredList, 0, 1, 2, 3, 4); }
@Test public void testOffset() { smartIterator = new SmartIterator(0, 2); Map<String, Object> unfilteredList = createMap(5); Map<String, Object> filteredList = smartIterator.applyLimitAndOffset(unfilteredList); assertMapContainsOnly(filteredList, 2, 3, 4); }
@Test public void testOffsetAndLimit() { smartIterator = new SmartIterator(2, 2); Map<String, Object> unfilteredList = createMap(5); Map<String, Object> filteredList = smartIterator.applyLimitAndOffset(unfilteredList); assertMapContainsOnly(filteredList, 2, 3); }