asereda-gs commented on a change in pull request #1119: [CALCITE-2937] Linq4j: 
implement LazyEnumerable
URL: https://github.com/apache/calcite/pull/1119#discussion_r267501129
 
 

 ##########
 File path: linq4j/src/main/java/org/apache/calcite/linq4j/Linq4j.java
 ##########
 @@ -524,6 +525,39 @@ public void close() {
     }
   }
 
+  /**
+   * Returns an {@link Enumerable} whose computation (via a {@link Supplier}) 
will be
+   * postponed until it is actually required
+   *
+   * @param supplier Enumerable supplier
+   * @param <E> Element type
+   *
+   * @return Lazy enumerable
+   */
+  public static <E> Enumerable<E> lazyEnumerable(Supplier<Enumerable<E>> 
supplier) {
+    return new LazyEnumerable<>(supplier);
+  }
+
+
+  /** Lazy enumerable.
+   *
+   * @param <E> element type */
+  static class LazyEnumerable<E> extends WrapperEnumerable<E> {
+    private final Supplier<Enumerable<E>> supplier;
+    private Enumerable<E> enumerable = null;
+
+    LazyEnumerable(Supplier<Enumerable<E>> supplier) {
+      this.supplier = supplier;
+    }
+
+    @Override protected Enumerable<E> getEnumerable() {
+      if (enumerable == null) {
+        enumerable = supplier.get();
 
 Review comment:
   Perhaps check that `supplier.get()` doesn't return null (fail early).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to