summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2022-03-15 12:42:07 +0100
committerJon Marius Venstad <venstad@gmail.com>2022-03-15 12:42:07 +0100
commit3e321f1c24ce80153b1730f82c840c31f20ad9a2 (patch)
treecb25782656cd10ca4ad111c8a21259d794e9b310 /vespajlib
parent8e72c805a07276f81bc201e45e994060f90dc41d (diff)
Handle incocmpatible versions for dev/perf deployments
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/Iterables.java25
-rw-r--r--vespajlib/src/test/java/com/yahoo/collections/IterablesTest.java49
2 files changed, 74 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/Iterables.java b/vespajlib/src/main/java/com/yahoo/collections/Iterables.java
new file mode 100644
index 00000000000..904db7a3db7
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/collections/Iterables.java
@@ -0,0 +1,25 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.collections;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+/**
+ * @author jonmv
+ */
+public class Iterables {
+
+ private Iterables() { }
+
+ /** Returns a reverse-order iterable view of the given elements. */
+ public static <T> Iterable<T> reversed(List<T> elements) {
+ return () -> new Iterator<T>() {
+ final ListIterator<T> wrapped = elements.listIterator(elements.size());
+ @Override public boolean hasNext() { return wrapped.hasPrevious(); }
+ @Override public T next() { return wrapped.previous(); }
+ @Override public void remove() { wrapped.remove(); }
+ };
+ }
+
+}
diff --git a/vespajlib/src/test/java/com/yahoo/collections/IterablesTest.java b/vespajlib/src/test/java/com/yahoo/collections/IterablesTest.java
new file mode 100644
index 00000000000..40be8831f0b
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/collections/IterablesTest.java
@@ -0,0 +1,49 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.collections;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * @author jonmv
+ */
+class IterablesTest {
+
+ @Test
+ void testEmpty() {
+ List<Integer> elements = new ArrayList<>();
+ Iterator<Integer> iterator = Iterables.reversed(elements).iterator();
+ assertFalse(iterator.hasNext());
+ assertThrows(NoSuchElementException.class, iterator::next);
+ assertThrows(IllegalStateException.class, iterator::remove);
+ }
+
+ @Test
+ void testIterator() {
+ List<Integer> elements = new ArrayList<>(List.of(1, 2, 3));
+ Iterator<Integer> iterator = Iterables.reversed(elements).iterator();
+ assertTrue(iterator.hasNext());
+ assertEquals(3, iterator.next());
+ iterator.remove();
+ assertThrows(IllegalStateException.class, iterator::remove);
+ assertTrue(iterator.hasNext());
+ assertEquals(2, iterator.next());
+ assertTrue(iterator.hasNext());
+ assertEquals(1, iterator.next());
+ iterator.remove();
+ assertThrows(IllegalStateException.class, iterator::remove);
+ assertThrows(NoSuchElementException.class, iterator::next);
+ assertEquals(List.of(2), elements);
+ }
+
+}