1printFirstTen() gọi allOrders(): service tạo new OrderIterator(api) rồi bọc nó thành Stream bằng Spliterators.spliteratorUnknownSize. Chưa có cuộc gọi mạng nào; chỉ khi Stream kéo phần tử thì việc tải mới bắt đầu.printFirstTen() calls allOrders(): the service creates new OrderIterator(api) and wraps it in a Stream via Spliterators.spliteratorUnknownSize. No network call yet; loading starts only when the Stream pulls an element.
2limit(10).forEach kéo phần tử đầu, Stream gọi hasNext() qua Iterator. Bộ đệm page đang rỗng và lastPage=false, nên OrderIterator gọi api.fetch("", 100) qua OrderApiClient.limit(10).forEach pulls the first element and the Stream calls hasNext() through Iterator. The page buffer is empty and lastPage=false, so OrderIterator calls api.fetch("", 100) through OrderApiClient.
3OrderApiClient trả OrderPage gồm 100 đơn và nextCursor=c_100. OrderIterator gán page = p.items().iterator(), cursor = c_100, lastPage = false.OrderApiClient returns an OrderPage with 100 orders and nextCursor=c_100. OrderIterator sets page = p.items().iterator(), cursor = c_100, lastPage = false.
4Mỗi next() trả một đơn từ bộ đệm; forEach chạy System.out.println(o.id()). Sau 10 đơn, limit(10) ngừng kéo nên trang 2 không bao giờ được tải.Each next() returns a buffered order; forEach runs System.out.println(o.id()). After ten orders limit(10) stops pulling, so page 2 is never fetched.
5countBigCodOrders() tạo iterator mới qua allOrders() và chạy filter(COD).filter(total > 5_000_000).count(), nên kéo hết mọi đơn. Khi 100 đơn trang 1 hết, hasNext() gọi fetch(c_100, 100).countBigCodOrders() creates a fresh iterator via allOrders() and runs filter(COD).filter(total > 5_000_000).count(), pulling every order. When page 1's 100 orders run out, hasNext() calls fetch(c_100, 100).
6Trang cuối có 20 đơn và nextCursor=null nên lastPage=true. Sau đơn thứ 120, bộ đệm rỗng và hasNext() trả false mà không gọi API thêm; count() trả kết quả. Gọi next() lúc này sẽ ném NoSuchElementException.The final page has 20 orders and nextCursor=null, so lastPage=true. After order 120 the buffer is empty and hasNext() returns false without another API call; count() returns. Calling next() now throws NoSuchElementException.
7Bản Go: PrintFirstTen chạy for o, err := range All(ctx, c). All là iter.Seq2: closure gọi c.Fetch(ctx, "", 100) rồi yield(o, nil) từng đơn của Page.Items.In Go: PrintFirstTen runs for o, err := range All(ctx, c). All is an iter.Seq2: its closure calls c.Fetch(ctx, "", 100) and then yield(o, nil) for each order in Page.Items.
8fmt.Println(o.ID) in 10 mã; khi i == 10 vòng lặp break, yield trả false và All return ngay, không gọi Fetch trang tiếp theo.fmt.Println(o.ID) prints ten IDs; at i == 10 the loop breaks, yield returns false and All returns immediately, never fetching the next page.
9CountBigCOD duyệt cùng All đến khi NextCursor == "", đếm đơn COD trên 5.000.000; nếu Fetch lỗi, All yield(Order{}, err) và hàm trả n, err. Filter[T] là adapter có thể dùng lại cho iter.Seq, giống Stream.filter.CountBigCOD ranges over the same All until NextCursor == "", counting COD orders above 5,000,000; if Fetch fails, All yields (Order{}, err) and the function returns n, err. Filter[T] is a reusable adapter for iter.Seq, like Stream.filter.