@@ -66,23 +66,32 @@ public static Continuation start(ContinuableRunnable o) {
6666 return Continuation .startWith (toRunnable (o ));
6767 }
6868
69- public static <T > CoIterator <T > iterate (Continuation continuation ) {
70- return new CoIterator <> (continuation );
69+ public static <T > ClosableIterator <T > iterate (Continuation continuation ) {
70+ return iterate (continuation , false );
7171 }
7272
73- public static <T > CoIterator <T > iterate (ContinuableRunnable generator ) {
74- return iterate (create (generator ));
73+ public static <T > ClosableIterator <T > iterate (Continuation continuation , boolean useCurrentValue ) {
74+ return new CoIterator <>(continuation , useCurrentValue );
75+ }
76+
77+
78+ public static <T > ClosableIterator <T > iterate (ContinuableRunnable generator ) {
79+ return iterate (create (generator ), false );
7580 }
7681
7782 public static <T > Stream <T > stream (Continuation continuation ) {
78- CoIterator <T > iterator = iterate (continuation );
83+ return stream (continuation , false );
84+ }
85+
86+ public static <T > Stream <T > stream (Continuation continuation , boolean useCurrentValue ) {
87+ ClosableIterator <T > iterator = iterate (continuation , useCurrentValue );
7988 return StreamSupport
8089 .stream (Spliterators .spliteratorUnknownSize (iterator , 0 ), false )
8190 .onClose (iterator ::close );
8291 }
8392
8493 public static <T > Stream <T > stream (ContinuableRunnable generator ) {
85- return stream (create (generator ));
94+ return stream (create (generator ), false );
8695 }
8796
8897 /**
@@ -92,11 +101,24 @@ public static <T> Stream<T> stream(ContinuableRunnable generator) {
92101 *
93102 * @param <T> a type of values
94103 * @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
96- * @param action a continuable action to perform on the values yielded
104+ * @param action a non-continuable action to perform on the values yielded
97105 */
98106 public static <T > void execute (Continuation continuation , Consumer <? super T > action ) {
99- try (CoIterator <T > iter = new CoIterator <>(continuation )) {
107+ execute (continuation , false , action );
108+ }
109+
110+ /**
111+ * Executes the suspended continuation from the point specified till the end
112+ * of the corresponding code block and performs a non-suspendable action
113+ * on each value yielded.
114+ *
115+ * @param <T> a type of values
116+ * @param continuation a continuation to resume a code block that yields multiple results
117+ * @param useCurrentValue should the value of the supplied continuation be used as a first value to process
118+ * @param action a non-continuable action to perform on the values yielded
119+ */
120+ public static <T > void execute (Continuation continuation , boolean useCurrentValue , Consumer <? super T > action ) {
121+ try (ClosableIterator <T > iter = iterate (continuation , useCurrentValue )) {
100122 while (iter .hasNext ()) {
101123 action .accept (iter .next ());
102124 }
@@ -109,8 +131,7 @@ public static <T> void execute(Continuation continuation, Consumer<? super T> ac
109131 *
110132 * @param <T> a type of values
111133 * @param generator a continuable code block that yields multiple results
112- * @param valueType a type of the values yielded from code block
113- * @param action a continuable action to perform on the values yielded
134+ * @param action a non-continuable action to perform on the values yielded
114135 */
115136 public static <T > void execute (ContinuableRunnable generator , Consumer <? super T > action ) {
116137 execute (create (generator ), action );
@@ -124,24 +145,38 @@ public static <T> void execute(ContinuableRunnable generator, Consumer<? super T
124145 *
125146 * @param <T> a type of values
126147 * @param continuation a continuation to resume a code block that yields multiple results
127- * @param valueType a type of the values yielded from code block
128148 * @param action a continuable action to perform on the values yielded
129149 */
130150 public @ continuable static <T > void executeContinuable (Continuation continuation , ContinuableConsumer <? super T > action ) {
131- forEach (()-> new CoIterator <>( continuation ) , action );
151+ executeContinuable ( continuation , false , action );
132152 }
133153
154+ /**
155+ * Executes the suspended continuation from the point specified till the end
156+ * of the corresponding code block and performs a potentially suspendable action
157+ * on each value yielded.
158+ *
159+ * @param <T> a type of values
160+ * @param useCurrentValue should the value of the supplied continuation be used as a first value to process
161+ * @param action a continuable action to perform on the values yielded
162+ */
163+ public @ continuable static <T > void executeContinuable (Continuation continuation , boolean useCurrentValue , ContinuableConsumer <? super T > action ) {
164+ try (ClosableIterator <T > iter = iterate (continuation , useCurrentValue )) {
165+ forEach (iter , action );
166+ }
167+ }
168+
169+
134170 /**
135171 * Fully executes the continuable code block and performs a potentially suspendable
136172 * action on each value yielded.
137173 *
138174 * @param <T> a type of values
139175 * @param generator a continuable code block that yields multiple results
140- * @param valueType a type of the values yielded from code block
141176 * @param action a continuable action to perform on the values yielded
142177 */
143178 public @ continuable static <T > void executeContinuable (ContinuableRunnable generator , ContinuableConsumer <? super T > action ) {
144- executeContinuable (create (generator ), action );
179+ executeContinuable (create (generator ), false , action );
145180 }
146181
147182 /**
@@ -169,7 +204,10 @@ public static <T> void execute(ContinuableRunnable generator, Consumer<? super T
169204 * @param action a continuable action to perform on the elements
170205 */
171206 public @ continuable static <T > void forEach (Iterable <T > iterable , ContinuableConsumer <? super T > action ) {
172- forEach (iterable .iterator (), action );
207+ Iterator <T > iter = iterable .iterator ();
208+ try (ClosableIterator <T > closable = asClosable (iter )) {
209+ forEach (iter , action );
210+ }
173211 }
174212
175213 /**
@@ -188,5 +226,9 @@ public static <T> void execute(ContinuableRunnable generator, Consumer<? super T
188226 }
189227 }
190228
229+ @ SuppressWarnings ("unchecked" )
230+ private static <E > ClosableIterator <E > asClosable (Object o ) {
231+ return o instanceof ClosableIterator ? (ClosableIterator <E >)o : null ;
232+ }
191233
192234}
0 commit comments