I'm building a *Seq* that contains *limit()* and *window()* terms and I'm seeing some surprising behavior:
- If the *limit()* comes after the *window() *it appears that the iterator that the *Seq* is based on gets completely drained even though the stream terminates as expected after the number of items specified in the *limit()* are processed. - If the *limit()* comes before the *window()* the iterator is not drained. (The stream also terminates as expected.) I've attached a simple program that exhibits the behavior. In my real scenario, the iterator that the *Seq *is based on is an iterator on top of a DB cursor and the *limit()* comes after the *window()*. I really need the behavior to not be that the code tries to drain this iterator--i.e., read a ton of rows from the DB. Is this a bug or just the way things work? Thanks. Nat -- You received this message because you are subscribed to the Google Groups "jOOQ User Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/9114e574-7d05-4109-95f8-c9700b38fc32%40googlegroups.com.
import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.jooq.lambda.Seq; public class SeqDemo { private static final int SIZE = 1000; public static void main(String[] args) { List<Integer> list = Seq.range(0, SIZE).toList(); { MyIterator it = new MyIterator(list); Seq.seq(it) .limit(10) // limit before window .window(-1, 1) .toList(); System.out.format("%d %s\n", it.nextCount_.get(), it.hasNext()); // Prints "100 true" } { MyIterator it = new MyIterator(list); Seq.seq(it) .window(-1, 1) .limit(10) // limit after window .toList(); System.out.format("%d %s\n", it.nextCount_.get(), it.hasNext()); // Prints "1000 false" } } static class MyIterator implements Iterator<Integer> { List<Integer> list_; AtomicInteger nextCount_ = new AtomicInteger(); int idx = 0; private MyIterator(List<Integer> list) { list_ = list; } @Override public boolean hasNext() { return idx < SIZE; } @Override public Integer next() { nextCount_.incrementAndGet(); return list_.get(idx++); } } }