This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
commit a55e533be5a0adf4537e98d2e7209fe7bd946ddb Author: Julian Hyde <[email protected]> AuthorDate: Tue Oct 8 11:27:00 2019 -0700 Add method Pair.forEach(Iterable, Iterable, BiConsumer) --- .../main/java/org/apache/calcite/util/Pair.java | 27 +++++++++++ .../java/org/apache/calcite/util/UtilTest.java | 53 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/util/Pair.java b/core/src/main/java/org/apache/calcite/util/Pair.java index 699f379..9cd9c2e 100644 --- a/core/src/main/java/org/apache/calcite/util/Pair.java +++ b/core/src/main/java/org/apache/calcite/util/Pair.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.BiConsumer; import javax.annotation.Nonnull; /** @@ -267,6 +268,32 @@ public class Pair<T1, T2> return new MutableZipList<>(ks, vs); } + /** Applies an action to every element of a pair of iterables. + * + * <p>Calls to the action stop whenever the first of the input iterators + * ends. But typically the source iterators will be the same length. + * + * @see Map#forEach(java.util.function.BiConsumer) + * @see org.apache.calcite.linq4j.Ord#forEach(Iterable, java.util.function.ObjIntConsumer) + * + * @param ks Left iterable + * @param vs Right iterable + * @param consumer The action to be performed for each element + * + * @param <K> Left type + * @param <V> Right type + */ + public static <K, V> void forEach( + final Iterable<? extends K> ks, + final Iterable<? extends V> vs, + BiConsumer<K, V> consumer) { + final Iterator<? extends K> leftIterator = ks.iterator(); + final Iterator<? extends V> rightIterator = vs.iterator(); + while (leftIterator.hasNext() && rightIterator.hasNext()) { + consumer.accept(leftIterator.next(), rightIterator.next()); + } + } + /** * Returns an iterable over the left slice of an iterable. * diff --git a/core/src/test/java/org/apache/calcite/util/UtilTest.java b/core/src/test/java/org/apache/calcite/util/UtilTest.java index b853f67..6fc152e 100644 --- a/core/src/test/java/org/apache/calcite/util/UtilTest.java +++ b/core/src/test/java/org/apache/calcite/util/UtilTest.java @@ -86,6 +86,8 @@ import java.util.SortedMap; import java.util.SortedSet; import java.util.TimeZone; import java.util.TreeSet; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.ObjIntConsumer; @@ -921,6 +923,57 @@ public class UtilTest { } /** + * Unit test for {@link Pair#forEach(Iterable, Iterable, BiConsumer)}. + */ + @Test public void testPairForEach() { + List<String> strings = Arrays.asList("paul", "george", "john", "ringo"); + List<Integer> integers = Arrays.asList(1942, 1943, 1940); + + // shorter list on the right + final AtomicInteger size = new AtomicInteger(); + Pair.forEach(strings, integers, (s, i) -> size.incrementAndGet()); + assertThat(size.get(), is(3)); + + // shorter list on the left + size.set(0); + Pair.forEach(integers, strings, (i, s) -> size.incrementAndGet()); + assertThat(size.get(), is(3)); + + // same on left and right + size.set(0); + Pair.forEach(strings, strings, (s1, s2) -> size.incrementAndGet()); + assertThat(size.get(), is(4)); + + // empty on left + size.set(0); + Pair.forEach(strings, ImmutableList.of(), (s, i) -> size.incrementAndGet()); + assertThat(size.get(), is(0)); + + // empty on right + size.set(0); + Pair.forEach(strings, ImmutableList.of(), (s, i) -> size.incrementAndGet()); + assertThat(size.get(), is(0)); + + // empty on right + size.set(0); + Pair.forEach(ImmutableList.<String>of(), integers, + (s, i) -> size.incrementAndGet()); + assertThat(size.get(), is(0)); + + // both empty + size.set(0); + Pair.forEach(ImmutableList.<String>of(), ImmutableList.<Integer>of(), + (s, i) -> size.incrementAndGet()); + assertThat(size.get(), is(0)); + + // build a string + final StringBuilder b = new StringBuilder(); + Pair.forEach(strings, integers, + (s, i) -> b.append(s).append(":").append(i).append(";")); + assertThat(b.toString(), is("paul:1942;george:1943;john:1940;")); + } + + /** * Unit test for {@link Pair#adjacents(Iterable)}. */ @Test public void testPairAdjacents() {
