calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [8/8] incubator-calcite git commit: In linq4j, implement firstOrDefault, single, and singleOrDefault methods (Daniel Cooper)
Date Mon, 23 Mar 2015 08:11:52 GMT
In linq4j, implement firstOrDefault, single, and singleOrDefault methods (Daniel Cooper)

Close apache/incubator-calcite#60


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/60789a57
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/60789a57
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/60789a57

Branch: refs/heads/master
Commit: 60789a572dcbac028197d3c2cc6b05a70ba0418d
Parents: 3807827
Author: Daniel Cooper <cooper.daniel45@gmail.com>
Authored: Tue Mar 17 13:30:21 2015 +0000
Committer: Julian Hyde <jhyde@apache.org>
Committed: Fri Mar 20 12:46:47 2015 -0700

----------------------------------------------------------------------
 .../calcite/linq4j/EnumerableDefaults.java      |  89 +++++++++-
 .../apache/calcite/linq4j/test/Linq4jTest.java  | 177 ++++++++++++++++++-
 2 files changed, 255 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/60789a57/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java
----------------------------------------------------------------------
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java b/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java
index 8608b1c..e470032 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java
@@ -530,7 +530,7 @@ public abstract class EnumerableDefaults {
         return o;
       }
     }
-    return null;
+    throw new NoSuchElementException();
   }
 
   /**
@@ -538,8 +538,16 @@ public abstract class EnumerableDefaults {
    * default value if the sequence contains no elements.
    */
   public static <TSource> TSource firstOrDefault(
-      Enumerable<TSource> enumerable) {
-    throw Extensions.todo();
+        Enumerable<TSource> enumerable) {
+    final Enumerator<TSource> os = enumerable.enumerator();
+    try {
+      if (os.moveNext()) {
+        return os.current();
+      }
+      return null;
+    } finally {
+      os.close();
+    }
   }
 
   /**
@@ -549,7 +557,12 @@ public abstract class EnumerableDefaults {
    */
   public static <TSource> TSource firstOrDefault(Enumerable<TSource> enumerable,
       Predicate1<TSource> predicate) {
-    throw Extensions.todo();
+    for (TSource o : enumerable) {
+      if (predicate.apply(o)) {
+        return o;
+      }
+    }
+    return null;
   }
 
   /**
@@ -1721,7 +1734,23 @@ public abstract class EnumerableDefaults {
    * sequence.
    */
   public static <TSource> TSource single(Enumerable<TSource> source) {
-    throw Extensions.todo();
+    final Enumerator<TSource> os = source.enumerator();
+    TSource toRet = null;
+    try {
+      if (os.moveNext()) {
+        toRet = os.current();
+
+        if (os.moveNext()) {
+          throw new IllegalStateException();
+        }
+      }
+      if (toRet != null) {
+        return toRet;
+      }
+      throw new IllegalStateException();
+    } finally {
+      os.close();
+    }
   }
 
   /**
@@ -1730,8 +1759,26 @@ public abstract class EnumerableDefaults {
    * more than one such element exists.
    */
   public static <TSource> TSource single(Enumerable<TSource> source,
-      Predicate1<TSource> predicate) {
-    throw Extensions.todo();
+                                         Predicate1<TSource> predicate) {
+    final Enumerator<TSource> os = source.enumerator();
+    TSource toRet = null;
+    try {
+      while (os.moveNext()) {
+        if (predicate.apply(os.current())) {
+          if (toRet == null) {
+            toRet = os.current();
+          } else {
+            throw new IllegalStateException();
+          }
+        }
+      }
+      if (toRet != null) {
+        return toRet;
+      }
+      throw new IllegalStateException();
+    } finally {
+      os.close();
+    }
   }
 
   /**
@@ -1741,7 +1788,21 @@ public abstract class EnumerableDefaults {
    * sequence.
    */
   public static <TSource> TSource singleOrDefault(Enumerable<TSource> source)
{
-    throw Extensions.todo();
+    final Enumerator<TSource> os = source.enumerator();
+    TSource toRet = null;
+    try {
+      if (os.moveNext()) {
+        toRet = os.current();
+      }
+
+      if (os.moveNext()) {
+        return null;
+      }
+
+      return toRet;
+    } finally {
+      os.close();
+    }
   }
 
   /**
@@ -1752,7 +1813,17 @@ public abstract class EnumerableDefaults {
    */
   public static <TSource> TSource singleOrDefault(Enumerable<TSource> source,
       Predicate1<TSource> predicate) {
-    throw Extensions.todo();
+    TSource toRet = null;
+    for (TSource s : source) {
+      if (predicate.apply(s)) {
+        if (toRet != null) {
+          return null;
+        } else {
+          toRet = s;
+        }
+      }
+    }
+    return toRet;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/60789a57/linq4j/src/test/java/org/apache/calcite/linq4j/test/Linq4jTest.java
----------------------------------------------------------------------
diff --git a/linq4j/src/test/java/org/apache/calcite/linq4j/test/Linq4jTest.java b/linq4j/src/test/java/org/apache/calcite/linq4j/test/Linq4jTest.java
index 4e133c1..7a4f65a 100644
--- a/linq4j/src/test/java/org/apache/calcite/linq4j/test/Linq4jTest.java
+++ b/linq4j/src/test/java/org/apache/calcite/linq4j/test/Linq4jTest.java
@@ -508,9 +508,182 @@ public class Linq4jTest {
     assertEquals(people[1], Linq4j.asEnumerable(people).first(startWithS));
     assertEquals(numbers[3], Linq4j.asEnumerable(numbers).first(numberGT15));
 
-    // FIXME: What we need return if no one element is satisfied?
-    assertNull(Linq4j.asEnumerable(peopleWithoutCharS).first(startWithS));
+    try {
+      String s = Linq4j.asEnumerable(peopleWithoutCharS).first(startWithS);
+      fail("expected exception, but got" + s);
+    } catch (NoSuchElementException e) {
+      // ok
+    }
+  }
+
+  @Test public void testFirstOrDefault() {
+
+    String[] people = {"Brill", "Smith", "Simpsom"};
+    String[] empty = {};
+    Integer[] numbers = {5, 10, 15, 20, 25};
+
+    assertEquals(people[0], Linq4j.asEnumerable(people).firstOrDefault());
+    assertEquals(numbers[0], Linq4j.asEnumerable(numbers).firstOrDefault());
+
+    assertNull(Linq4j.asEnumerable(empty).firstOrDefault());
+  }
+
+  @Test public void testFirstOrDefaultPredicate1() {
+    Predicate1<String> startWithS = new Predicate1<String>() {
+      public boolean apply(String s) {
+        return s != null && Character.toString(s.charAt(0)).equals("S");
+      }
+    };
+
+    Predicate1<Integer> numberGT15 = new Predicate1<Integer>() {
+      public boolean apply(Integer i) {
+        return i > 15;
+      }
+    };
+
+    String[] people = {"Brill", "Smith", "Simpsom"};
+    String[] peopleWithoutCharS = {"Brill", "Andrew", "Alice"};
+    Integer[] numbers = {5, 10, 15, 20, 25};
+
+    assertEquals(people[1], Linq4j.asEnumerable(people)
+          .firstOrDefault(startWithS));
+    assertEquals(numbers[3], Linq4j.asEnumerable(numbers)
+          .firstOrDefault(numberGT15));
+
+    assertNull(Linq4j.asEnumerable(peopleWithoutCharS)
+          .firstOrDefault(startWithS));
+  }
+
+  @Test public void testSingle() {
+
+    String[] person = {"Smith"};
+    String[] people = {"Brill", "Smith", "Simpson"};
+    Integer[] number = {20};
+    Integer[] numbers = {5, 10, 15, 20};
+
+    assertEquals(person[0], Linq4j.asEnumerable(person).single());
+    assertEquals(number[0], Linq4j.asEnumerable(number).single());
+
+    try {
+      String s = Linq4j.asEnumerable(people).single();
+      fail("expected exception, but got" + s);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+
+    try {
+      int i = Linq4j.asEnumerable(numbers).single();
+      fail("expected exception, but got" + i);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+  }
+
+  @Test public void testSingleOrDefault() {
+
+    String[] person = {"Smith"};
+    String[] people = {"Brill", "Smith", "Simpson"};
+    Integer[] number = {20};
+    Integer[] numbers = {5, 10, 15, 20};
+
+    assertEquals(person[0], Linq4j.asEnumerable(person).singleOrDefault());
+    assertEquals(number[0], Linq4j.asEnumerable(number).singleOrDefault());
+
+    assertNull(Linq4j.asEnumerable(people).singleOrDefault());
+    assertNull(Linq4j.asEnumerable(numbers).singleOrDefault());
+  }
+
+  @Test public void testSinglePredicate1() {
+    Predicate1<String> startWithS = new Predicate1<String>() {
+      public boolean apply(String s) {
+        return s != null && Character.toString(s.charAt(0)).equals("S");
+      }
+    };
+
+    Predicate1<Integer> numberGT15 = new Predicate1<Integer>() {
+      public boolean apply(Integer i) {
+        return i > 15;
+      }
+    };
+
+    String[] people = {"Brill", "Smith"};
+    String[] twoPeopleWithCharS = {"Brill", "Smith", "Simpson"};
+    String[] peopleWithoutCharS = {"Brill", "Andrew", "Alice"};
+    Integer[] numbers = {5, 10, 15, 20};
+    Integer[] numbersWithoutGT15 = {5, 10, 15};
+    Integer[] numbersWithTwoGT15 = {5, 10, 15, 20, 25};
+
+    assertEquals(people[1], Linq4j.asEnumerable(people).single(startWithS));
+    assertEquals(numbers[3], Linq4j.asEnumerable(numbers).single(numberGT15));
+
+
+    try {
+      String s = Linq4j.asEnumerable(twoPeopleWithCharS).single(startWithS);
+      fail("expected exception, but got" + s);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+
+    try {
+      int i = Linq4j.asEnumerable(numbersWithTwoGT15).single(numberGT15);
+      fail("expected exception, but got" + i);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+
+    try {
+      String s = Linq4j.asEnumerable(peopleWithoutCharS).single(startWithS);
+      fail("expected exception, but got" + s);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+
+    try {
+      int i = Linq4j.asEnumerable(numbersWithoutGT15).single(numberGT15);
+      fail("expected exception, but got" + i);
+    } catch (IllegalStateException e) {
+      // ok
+    }
+  }
+
+  @Test
+  public void testSingleOrDefaultPredicate1() {
+    Predicate1<String> startWithS = new Predicate1<String>() {
+      public boolean apply(String s) {
+        return s != null && Character.toString(s.charAt(0)).equals("S");
+      }
+    };
+
+    Predicate1<Integer> numberGT15 = new Predicate1<Integer>() {
+      public boolean apply(Integer i) {
+        return i > 15;
+      }
+    };
+
+    String[] people = {"Brill", "Smith"};
+    String[] twoPeopleWithCharS = {"Brill", "Smith", "Simpson"};
+    String[] peopleWithoutCharS = {"Brill", "Andrew", "Alice"};
+    Integer[] numbers = {5, 10, 15, 20};
+    Integer[] numbersWithTwoGT15 = {5, 10, 15, 20, 25};
+    Integer[] numbersWithoutGT15 = {5, 10, 15};
+
+    assertEquals(people[1], Linq4j.asEnumerable(people)
+          .singleOrDefault(startWithS));
+
+    assertEquals(numbers[3], Linq4j.asEnumerable(numbers)
+          .singleOrDefault(numberGT15));
+
+    assertNull(Linq4j.asEnumerable(twoPeopleWithCharS)
+          .singleOrDefault(startWithS));
+
+    assertNull(Linq4j.asEnumerable(numbersWithTwoGT15)
+          .singleOrDefault(numberGT15));
+
+    assertNull(Linq4j.asEnumerable(peopleWithoutCharS)
+          .singleOrDefault(startWithS));
 
+    assertNull(Linq4j.asEnumerable(numbersWithoutGT15)
+          .singleOrDefault(numberGT15));
   }
 
   @SuppressWarnings("UnnecessaryBoxing")


Mime
View raw message