Util.filter replaces Guava Iterables.filter
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/f5cef160 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/f5cef160 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/f5cef160 Branch: refs/heads/master Commit: f5cef16066a8f13f906eed495f62b6c12cc1648d Parents: 4402f91 Author: Julian Hyde <[email protected]> Authored: Thu Jul 12 12:17:58 2018 -0700 Committer: Julian Hyde <[email protected]> Committed: Fri Jul 13 14:21:43 2018 -0700 ---------------------------------------------------------------------- .../calcite/plan/AbstractRelOptPlanner.java | 4 +- .../calcite/rel/logical/LogicalWindow.java | 4 +- .../java/org/apache/calcite/rex/RexUtil.java | 2 +- .../sql/type/SqlTypeExplicitPrecedenceList.java | 6 +-- .../main/java/org/apache/calcite/util/Util.java | 50 ++++++++++++++++++++ .../java/org/apache/calcite/util/UtilTest.java | 46 ++++++++++++++++++ 6 files changed, 103 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java b/core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java index 06306d5..9eab16e 100644 --- a/core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java +++ b/core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java @@ -22,9 +22,9 @@ import org.apache.calcite.rel.metadata.RelMetadataProvider; import org.apache.calcite.rel.metadata.RelMetadataQuery; import org.apache.calcite.rex.RexExecutor; import org.apache.calcite.util.CancelFlag; +import org.apache.calcite.util.Util; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.HashMap; @@ -415,7 +415,7 @@ public abstract class AbstractRelOptPlanner implements RelOptPlanner { /** Returns sub-classes of relational expression. */ public Iterable<Class<? extends RelNode>> subClasses( final Class<? extends RelNode> clazz) { - return Iterables.filter(classes, clazz::isAssignableFrom); + return Util.filter(classes, clazz::isAssignableFrom); } } http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java b/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java index f03cc2f..db5b856 100644 --- a/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java +++ b/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java @@ -37,8 +37,8 @@ import org.apache.calcite.tools.RelBuilder; import org.apache.calcite.util.ImmutableBitSet; import org.apache.calcite.util.Litmus; import org.apache.calcite.util.Pair; +import org.apache.calcite.util.Util; -import com.google.common.collect.Iterables; import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; @@ -345,7 +345,7 @@ public final class LogicalWindow extends Window { // Look up or create a window. RelCollation orderKeys = getCollation( Lists.newArrayList( - Iterables.filter(aggWindow.orderKeys, + Util.filter(aggWindow.orderKeys, rexFieldCollation -> // If ORDER BY references constant (i.e. RexInputRef), // then we can ignore such ORDER BY key. http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/main/java/org/apache/calcite/rex/RexUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rex/RexUtil.java b/core/src/main/java/org/apache/calcite/rex/RexUtil.java index 29e63f1..5c69573 100644 --- a/core/src/main/java/org/apache/calcite/rex/RexUtil.java +++ b/core/src/main/java/org/apache/calcite/rex/RexUtil.java @@ -1855,7 +1855,7 @@ public class RexUtil { case EQUALS: final RexCall call = (RexCall) e; if (call.getOperands().get(1) instanceof RexLiteral) { - notTerms = Iterables.filter(notTerms, + notTerms = Util.filter(notTerms, e2 -> { switch (e2.getKind()) { case EQUALS: http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/main/java/org/apache/calcite/sql/type/SqlTypeExplicitPrecedenceList.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeExplicitPrecedenceList.java b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeExplicitPrecedenceList.java index 539f058..d8eabdd 100644 --- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeExplicitPrecedenceList.java +++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeExplicitPrecedenceList.java @@ -22,14 +22,13 @@ import org.apache.calcite.util.Glossary; import org.apache.calcite.util.ImmutableNullableList; import org.apache.calcite.util.Util; -import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; /** * SqlTypeExplicitPrecedenceList implements the @@ -60,8 +59,7 @@ public class SqlTypeExplicitPrecedenceList SqlTypeName.DOUBLE); private static final List<SqlTypeName> COMPACT_NUMERIC_TYPES = - ImmutableList.copyOf( - Iterables.filter(NUMERIC_TYPES, Predicates.notNull())); + ImmutableList.copyOf(Util.filter(NUMERIC_TYPES, Objects::nonNull)); /** * Map from SqlTypeName to corresponding precedence list. http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/main/java/org/apache/calcite/util/Util.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/util/Util.java b/core/src/main/java/org/apache/calcite/util/Util.java index b90bbbd..1139643 100644 --- a/core/src/main/java/org/apache/calcite/util/Util.java +++ b/core/src/main/java/org/apache/calcite/util/Util.java @@ -95,6 +95,7 @@ import java.util.Set; import java.util.StringTokenizer; import java.util.TimeZone; import java.util.function.Function; +import java.util.function.Predicate; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -2369,6 +2370,18 @@ public class Util { } } + /** Filters an iterable. */ + public static <E> Iterable<E> filter(Iterable<E> iterable, + Predicate<E> predicate) { + return () -> filter(iterable.iterator(), predicate); + } + + /** Filters an iterator. */ + public static <E> Iterator<E> filter(Iterator<E> iterator, + Predicate<E> predicate) { + return new FilteringIterator<>(iterator, predicate); + } + //~ Inner Classes ---------------------------------------------------------- /** @@ -2447,6 +2460,43 @@ public class Util { super(list, function); } } + + /** Iterator that applies a predicate to each element. + * + * @param <T> Element type */ + private static class FilteringIterator<T> implements Iterator<T> { + private static final Object DUMMY = new Object(); + final Iterator<? extends T> iterator; + private final Predicate<T> predicate; + T current; + + FilteringIterator(Iterator<? extends T> iterator, + Predicate<T> predicate) { + this.iterator = iterator; + this.predicate = predicate; + current = moveNext(); + } + + public boolean hasNext() { + return current != DUMMY; + } + + public T next() { + final T t = this.current; + current = moveNext(); + return t; + } + + protected T moveNext() { + while (iterator.hasNext()) { + T t = iterator.next(); + if (predicate.test(t)) { + return t; + } + } + return (T) DUMMY; + } + } } // End Util.java http://git-wip-us.apache.org/repos/asf/calcite/blob/f5cef160/core/src/test/java/org/apache/calcite/util/UtilTest.java ---------------------------------------------------------------------- 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 e9159a9..fa649c8 100644 --- a/core/src/test/java/org/apache/calcite/util/UtilTest.java +++ b/core/src/test/java/org/apache/calcite/util/UtilTest.java @@ -43,8 +43,10 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; +import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.StringDescription; +import org.hamcrest.TypeSafeMatcher; import org.junit.BeforeClass; import org.junit.Test; @@ -73,6 +75,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.NavigableSet; +import java.util.Objects; import java.util.Properties; import java.util.Random; import java.util.RandomAccess; @@ -2175,6 +2178,49 @@ public class UtilTest { not(instanceOf(RandomAccess.class))); } + /** Tests {@link Util#filter(Iterable, java.util.function.Predicate)}. */ + @Test public void testFilter() { + final List<String> beatles = + Arrays.asList("John", "Paul", "George", "Ringo"); + final List<String> empty = Collections.emptyList(); + final List<String> nullBeatles = + Arrays.asList("John", "Paul", null, "Ringo"); + assertThat(Util.filter(beatles, s -> s.length() == 4), + isIterable(Arrays.asList("John", "Paul"))); + assertThat(Util.filter(empty, s -> s.length() == 4), isIterable(empty)); + assertThat(Util.filter(empty, s -> false), isIterable(empty)); + assertThat(Util.filter(empty, s -> true), isIterable(empty)); + assertThat(Util.filter(beatles, s -> false), isIterable(empty)); + assertThat(Util.filter(beatles, s -> true), isIterable(beatles)); + assertThat(Util.filter(nullBeatles, s -> false), isIterable(empty)); + assertThat(Util.filter(nullBeatles, s -> true), isIterable(nullBeatles)); + assertThat(Util.filter(nullBeatles, Objects::isNull), + isIterable(Collections.singletonList(null))); + assertThat(Util.filter(nullBeatles, Objects::nonNull), + isIterable(Arrays.asList("John", "Paul", "Ringo"))); + } + + private static <E> Matcher<Iterable<E>> isIterable(final Iterable<E> iterable) { + final List<E> list = toList(iterable); + return new TypeSafeMatcher<Iterable<E>>() { + protected boolean matchesSafely(Iterable<E> iterable) { + return list.equals(toList(iterable)); + } + + public void describeTo(Description description) { + description.appendText("is iterable ").appendValue(list); + } + }; + } + + private static <E> List<E> toList(Iterable<E> iterable) { + final List<E> list = new ArrayList<>(); + for (E e : iterable) { + list.add(e); + } + return list; + } + static String mismatchDescription(Matcher m, Object item) { final StringDescription d = new StringDescription(); m.describeMismatch(item, d);
