Skip to content

Commit a200ceb

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11548 : added util method to replace Guava's Iterables.limit() (#2138)
Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent c43a4a2 commit a200ceb

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,22 @@ public static boolean elementsEqual(final Iterable<?> itr1, final Iterable<?> it
357357
}
358358
return IteratorUtils.elementsEqual(itr1.iterator(), itr2.iterator());
359359
}
360+
361+
/**
362+
* Creates an iterable limited to the specified number of elements.
363+
* <p>
364+
* The returned iterable's iterator will stop returning elements after the specified limit
365+
* has been reached or when the source iterable's iterator is exhausted, whichever comes first.
366+
* <p>
367+
* The returned iterable's iterator supports {@code remove()} if the original iterator supports it.
368+
*
369+
* @param <T> the type of elements in the iterable
370+
* @param iterable the iterable to limit, may be null
371+
* @param limitSize the maximum number of elements to return
372+
* @return a limited iterable
373+
* @throws IllegalArgumentException if limitSize is negative
374+
*/
375+
public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitSize) {
376+
return org.apache.commons.collections4.IterableUtils.boundedIterable(iterable, limitSize);
377+
}
360378
}

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,4 +755,71 @@ public void testMixedCollectionTypes() {
755755

756756
Assert.assertTrue(IterableUtils.elementsEqual(arrayList, linkedList));
757757
}
758+
759+
@Test
760+
public void testLimitWithFewerElements() {
761+
List<Integer> list = List.of(1, 2, 3, 4, 5);
762+
Iterable<Integer> limited = IterableUtils.limit(list, 3);
763+
764+
List<Integer> result = new ArrayList<>();
765+
limited.forEach(result::add);
766+
767+
Assert.assertEquals(List.of(1, 2, 3), result);
768+
}
769+
770+
@Test
771+
public void testLimitWithMoreElements() {
772+
List<Integer> list = List.of(1, 2, 3);
773+
Iterable<Integer> limited = IterableUtils.limit(list, 5);
774+
775+
List<Integer> result = new ArrayList<>();
776+
limited.forEach(result::add);
777+
778+
Assert.assertEquals(List.of(1, 2, 3), result);
779+
}
780+
781+
@Test
782+
public void testLimitWithZero() {
783+
List<Integer> list = List.of(1, 2, 3, 4, 5);
784+
Iterable<Integer> limited = IterableUtils.limit(list, 0);
785+
786+
List<Integer> result = new ArrayList<>();
787+
limited.forEach(result::add);
788+
789+
Assert.assertTrue(result.isEmpty());
790+
}
791+
792+
@Test
793+
public void testLimitWithEmptyIterable() {
794+
List<Integer> list = Collections.emptyList();
795+
Iterable<Integer> limited = IterableUtils.limit(list, 3);
796+
797+
Assert.assertFalse(limited.iterator().hasNext());
798+
}
799+
800+
@Test(expected = NullPointerException.class)
801+
public void testLimitWithNullIterable() {
802+
Iterable<Integer> limited = IterableUtils.limit(null, 3);
803+
804+
Assert.assertFalse(limited.iterator().hasNext());
805+
}
806+
807+
@Test(expected = IllegalArgumentException.class)
808+
public void testLimitWithNegativeSize() {
809+
List<Integer> list = List.of(1, 2, 3);
810+
IterableUtils.limit(list, -1);
811+
}
812+
813+
@Test
814+
public void testLimitWithRemove() {
815+
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
816+
Iterable<Integer> limited = IterableUtils.limit(list, 2);
817+
818+
Iterator<Integer> iterator = limited.iterator();
819+
iterator.next(); // 1
820+
iterator.remove();
821+
iterator.next(); // 2
822+
823+
Assert.assertEquals(Arrays.asList(2, 3, 4, 5), list);
824+
}
758825
}

0 commit comments

Comments
 (0)