1818import static org .apache .commons .javaflow .extras .ContinuationSupport .toRunnable ;
1919
2020import java .util .Iterator ;
21- import java .util .function .Supplier ;
21+ import java .util .Spliterators ;
22+ import java .util .function .Consumer ;
2223import java .util .stream .Stream ;
24+ import java .util .stream .StreamSupport ;
2325
2426import org .apache .commons .javaflow .api .Continuation ;
2527import org .apache .commons .javaflow .api .continuable ;
@@ -64,29 +66,56 @@ public static Continuation start(ContinuableRunnable o) {
6466 return Continuation .startWith (toRunnable (o ));
6567 }
6668
69+ public static <T > CoIterator <T > iterate (Continuation continuation ) {
70+ return new CoIterator <>(continuation );
71+ }
72+
73+ public static <T > CoIterator <T > iterate (ContinuableRunnable generator ) {
74+ return iterate (create (generator ));
75+ }
76+
77+ public static <T > Stream <T > stream (Continuation continuation ) {
78+ CoIterator <T > iterator = iterate (continuation );
79+ return StreamSupport
80+ .stream (Spliterators .spliteratorUnknownSize (iterator , 0 ), false )
81+ .onClose (iterator ::close );
82+ }
83+
84+ public static <T > Stream <T > stream (ContinuableRunnable generator ) {
85+ return stream (create (generator ));
86+ }
87+
6788 /**
6889 * Executes the suspended continuation from the point specified till the end
69- * of the corresponding code block and performs a potentially suspendable action
90+ * of the corresponding code block and performs a non- suspendable action
7091 * on each value yielded.
7192 *
93+ * @param <T> a type of values
7294 * @param continuation a continuation to resume a code block that yields multiple results
95+ * @param valueType a type of the values yielded from code block
7396 * @param action a continuable action to perform on the values yielded
74- */
75- public @ continuable static void execute (Continuation continuation , ContinuableConsumer <? super Object > action ) {
76- execute (continuation , Object .class , action );
97+ */
98+ public static <T > void execute (Continuation continuation , Consumer <? super T > action ) {
99+ try (CoIterator <T > iter = new CoIterator <>(continuation )) {
100+ while (iter .hasNext ()) {
101+ action .accept (iter .next ());
102+ }
103+ }
77104 }
78105
79106 /**
80- * Fully executes the continuable code block and performs a potentially suspendable
107+ * Fully executes the continuable code block and performs a non- suspendable
81108 * action on each value yielded.
82109 *
110+ * @param <T> a type of values
83111 * @param generator a continuable code block that yields multiple results
112+ * @param valueType a type of the values yielded from code block
84113 * @param action a continuable action to perform on the values yielded
85114 */
86- public @ continuable static void execute (ContinuableRunnable generator , ContinuableConsumer <? super Object > action ) {
87- execute (generator , Object . class , action );
115+ public static < T > void execute (ContinuableRunnable generator , Consumer <? super T > action ) {
116+ execute (create ( generator ) , action );
88117 }
89-
118+
90119
91120 /**
92121 * Executes the suspended continuation from the point specified till the end
@@ -98,7 +127,7 @@ public static Continuation start(ContinuableRunnable o) {
98127 * @param valueType a type of the values yielded from code block
99128 * @param action a continuable action to perform on the values yielded
100129 */
101- public @ continuable static <T > void execute (Continuation continuation , Class < T > valueType , ContinuableConsumer <? super T > action ) {
130+ public @ continuable static <T > void executeContinuable (Continuation continuation , ContinuableConsumer <? super T > action ) {
102131 forEach (()-> new CoIterator <>(continuation ), action );
103132 }
104133
@@ -111,17 +140,10 @@ public static Continuation start(ContinuableRunnable o) {
111140 * @param valueType a type of the values yielded from code block
112141 * @param action a continuable action to perform on the values yielded
113142 */
114- public @ continuable static <T > void execute (ContinuableRunnable generator , Class < T > valueType , ContinuableConsumer <? super T > action ) {
115- forEach (()-> new CoIterator <> (generator ), action );
143+ public @ continuable static <T > void executeContinuable (ContinuableRunnable generator , ContinuableConsumer <? super T > action ) {
144+ executeContinuable ( create (generator ), action );
116145 }
117146
118- private @ continuable static <T > void execute (Supplier <CoIterator <T >> iteratorProvider , Class <T > valueType , ContinuableConsumer <? super T > action ) {
119- try (CoIterator <T > iterator = iteratorProvider .get ()) {
120- forEach (iterator , action );
121- }
122- }
123-
124-
125147 /**
126148 * Performs an continuable action for each element of the {@link Stream} supplied.
127149 *
0 commit comments