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.async.SdkPublisher;
21 
22 /**
23  * Represents the result from paginated operations such as scan and query.
24  * <p>
25  * You can either subscribe to the {@link Page}s or flattened items across <b>all</b> pages via {@link #items()}.
26  *
27  * Example:
28  * <p>
29  * 1) Subscribing to {@link Page}s
30  * <pre>
31  * {@code
32  *
33  * PagePublisher<MyItem> publisher = mappedTable.scan();
34  * publisher.subscribe(page -> page.items().forEach(item -> System.out.println(item)))
35  *          .exceptionally(failure -> {
36  *              failure.printStackTrace();
37  *              return null;
38  *          });
39  * }
40  * </pre>
41  *
42  * <p>
43  * 2) Subscribing to items across all pages.
44  * <pre>
45  * {@code
46  *
47  * PagePublisher<MyItem> publisher = mappedTable.scan();
48  * publisher.items()
49  *          .subscribe(item -> System.out.println(item))
50  *          .exceptionally(failure -> {
51  *              failure.printStackTrace();
52  *              return null;
53  *          });
54  * }
55  * </pre>
56  *
57  * @param <T> The modelled type of the object in a page.
58  */
59 @SdkPublicApi
60 @ThreadSafe
61 public interface PagePublisher<T> extends SdkPublisher<Page<T>> {
62 
63     /**
64      * Creates a flattened items publisher with the underlying page publisher.
65      */
create(SdkPublisher<Page<T>> publisher)66     static <T> PagePublisher<T> create(SdkPublisher<Page<T>> publisher) {
67         return publisher::subscribe;
68     }
69 
70     /**
71      * Returns a publisher that can be used to request a stream of items across all pages.
72      *
73      * <p>
74      * This method is useful if you are interested in subscribing the items in the response pages
75      * instead of the top level pages.
76      */
items()77     default SdkPublisher<T> items() {
78         return this.flatMapIterable(Page::items);
79     }
80 }
81