You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2016/01/21 23:38:46 UTC
[09/50] [abbrv] calcite git commit: Add
ImmutableNullableList.copyOf(Iterable)
Add ImmutableNullableList.copyOf(Iterable)
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/b1fdd128
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/b1fdd128
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/b1fdd128
Branch: refs/heads/branch-release
Commit: b1fdd1280aafc883d7e088d5527621a789f6170d
Parents: ba1eee1
Author: Julian Hyde <jh...@apache.org>
Authored: Tue Jul 28 17:13:27 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Sun Jan 10 00:51:23 2016 -0800
----------------------------------------------------------------------
.../calcite/util/ImmutableNullableList.java | 28 ++++++++++++++++++++
.../java/org/apache/calcite/util/UtilTest.java | 27 ++++++++++++++++++-
2 files changed, 54 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/b1fdd128/core/src/main/java/org/apache/calcite/util/ImmutableNullableList.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/util/ImmutableNullableList.java b/core/src/main/java/org/apache/calcite/util/ImmutableNullableList.java
index 8ba9a6e..202ee5e 100644
--- a/core/src/main/java/org/apache/calcite/util/ImmutableNullableList.java
+++ b/core/src/main/java/org/apache/calcite/util/ImmutableNullableList.java
@@ -22,6 +22,7 @@ import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import java.util.AbstractList;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
@@ -74,6 +75,33 @@ public class ImmutableNullableList<E> extends AbstractList<E> {
* Returns an immutable list containing the given elements, in order.
*
* <p>Behavior as
+ * {@link com.google.common.collect.ImmutableList#copyOf(Iterable)}
+ * except that this list allows nulls.
+ */
+ public static <E> List<E> copyOf(Iterable<? extends E> elements) {
+ if (elements instanceof ImmutableNullableList
+ || elements instanceof ImmutableList
+ || elements == SINGLETON_NULL) {
+ //noinspection unchecked
+ return (List<E>) elements;
+ }
+ if (elements instanceof Collection) {
+ //noinspection unchecked
+ return copyOf((Collection) elements);
+ }
+ // If there are no nulls, ImmutableList is better.
+ final List<E> list = new ArrayList<>();
+ Iterables.addAll(list, elements);
+ if (list.contains(null)) {
+ return ImmutableNullableList.copyOf(list);
+ }
+ return ImmutableList.copyOf(elements);
+ }
+
+ /**
+ * Returns an immutable list containing the given elements, in order.
+ *
+ * <p>Behavior as
* {@link com.google.common.collect.ImmutableList#copyOf(Object[])}
* except that this list allows nulls.</p>
*/
http://git-wip-us.apache.org/repos/asf/calcite/blob/b1fdd128/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 9a42fcc..78202e2 100644
--- a/core/src/test/java/org/apache/calcite/util/UtilTest.java
+++ b/core/src/test/java/org/apache/calcite/util/UtilTest.java
@@ -72,6 +72,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -1282,8 +1283,32 @@ public class UtilTest {
isA((Class) ImmutableList.class));
// list with no nulls uses ImmutableList
- assertThat(ImmutableNullableList.copyOf(Arrays.asList("a", "b", "c")),
+ final List<String> abcList = Arrays.asList("a", "b", "c");
+ assertThat(ImmutableNullableList.copyOf(abcList),
isA((Class) ImmutableList.class));
+
+ // list with no nulls uses ImmutableList
+ final Iterable<String> abc =
+ new Iterable<String>() {
+ public Iterator<String> iterator() {
+ return abcList.iterator();
+ }
+ };
+ assertThat(ImmutableNullableList.copyOf(abc),
+ isA((Class) ImmutableList.class));
+ assertThat(ImmutableNullableList.copyOf(abc), equalTo(abcList));
+
+ // list with no nulls uses ImmutableList
+ final List<String> ab0cList = Arrays.asList("a", "b", null, "c");
+ final Iterable<String> ab0c =
+ new Iterable<String>() {
+ public Iterator<String> iterator() {
+ return ab0cList.iterator();
+ }
+ };
+ assertThat(ImmutableNullableList.copyOf(ab0c),
+ not(isA((Class) ImmutableList.class)));
+ assertThat(ImmutableNullableList.copyOf(ab0c), equalTo(ab0cList));
}
/** Test for {@link org.apache.calcite.util.UnmodifiableArrayList}. */