1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.enhanced.dynamodb.model;
17 
18 import software.amazon.awssdk.annotations.SdkPublicApi;
19 import software.amazon.awssdk.annotations.ThreadSafe;
20 import software.amazon.awssdk.core.pagination.sync.PaginatedItemsIterable;
21 import software.amazon.awssdk.core.pagination.sync.SdkIterable;
22 
23 /**
24  * Page iterable represents the result from paginated operations such as scan and query.
25  *
26  * <p>
27  * The result can be accessed either through iterable {@link Page}s or flattened items across <b>all</b>  pages via
28  * {@link #items()}
29  *
30  * <p>
31  * Example:
32  * <p>
33  * 1) Iterating through pages
34  *
35  * <pre>
36  * {@code
37  * PageIterable<MyItem> results = table.scan();
38  * results.stream().forEach(p -> p.items().forEach(item -> System.out.println(item)))
39  * }
40  * </pre>
41  *
42  * 2) Iterating through items
43  *
44  * <pre>
45  * {@code
46  * PageIterable<MyItem> results = table.scan();
47  * results.items().stream().forEach(item -> System.out.println(item));
48  * }
49  * </pre>
50  * @param <T> The modelled type of the object in a page.
51  */
52 @SdkPublicApi
53 @ThreadSafe
54 public interface PageIterable<T> extends SdkIterable<Page<T>> {
55 
create(SdkIterable<Page<T>> pageIterable)56     static <T> PageIterable<T> create(SdkIterable<Page<T>> pageIterable) {
57         return pageIterable::iterator;
58     }
59 
60     /**
61      * Returns an iterable to iterate through the paginated {@link Page#items()} across <b>all</b> response pages.
62      *
63      * <p>
64      * This method is useful if you are interested in iterating over the items in the response pages
65      * instead of the top level pages.
66      */
items()67     default SdkIterable<T> items() {
68         return PaginatedItemsIterable.<Page<T>, T>builder()
69             .pagesIterable(this)
70             .itemIteratorFunction(page -> page.items().iterator())
71             .build();
72     }
73 }
74