@@ -49,7 +49,52 @@ private Continuations() {}
4949 * @return the continuation, suspended before code starts
5050 */
5151 public static Continuation create (SuspendableRunnable o ) {
52- return Continuation .startSuspendedWith (toRunnable (o ));
52+ return create (o , false );
53+ }
54+
55+ /**
56+ * Creates a suspended continuation, {@link SuspendableRunnable} is not started
57+ * @param o a continuable code block
58+ * @param optimized
59+ * If true then continuation constructed is performance-optimized but
60+ * may be resumed only once. Otherwise "restartable" continuation is created that may
61+ * be resumed multiple times.
62+ * @return the continuation, suspended before code starts
63+ */
64+ public static Continuation create (SuspendableRunnable o , boolean optimized ) {
65+ return Continuation .startSuspendedWith (toRunnable (o ), optimized );
66+ }
67+
68+
69+ /**
70+ * Starts {@link SuspendableRunnable} code block and returns a continuation,
71+ * corresponding to the first {@link Continuation#suspend()} call inside this code block
72+ * (incl. nested continuable method calls), if any exists. Returns null if the code
73+ * is not suspended.
74+ *
75+ * @param o a continuable code block
76+ * @return the first continuation suspended
77+ */
78+ public static Continuation start (SuspendableRunnable o ) {
79+ return start (o , false );
80+ }
81+
82+
83+ /**
84+ * Starts {@link SuspendableRunnable} code block and returns a continuation,
85+ * corresponding to the first {@link Continuation#suspend()} call inside this code block
86+ * (incl. nested continuable method calls), if any exists. Returns null if the code
87+ * is not suspended.
88+ *
89+ * @param o a continuable code block
90+ * @param optimized
91+ * If true then continuation constructed is performance-optimized but
92+ * may be resumed only once. Otherwise "restartable" continuation is created that may
93+ * be resumed multiple times.
94+ * @return the first continuation suspended
95+ */
96+ public static Continuation start (SuspendableRunnable o , boolean optimized ) {
97+ return start (o , null , optimized );
5398 }
5499
55100 /**
@@ -63,22 +108,28 @@ public static Continuation create(SuspendableRunnable o) {
63108 * @return the first continuation suspended
64109 */
65110 public static Continuation start (SuspendableRunnable o , Object ctx ) {
66- return Continuation . startWith ( toRunnable ( o ) , ctx );
111+ return start ( o , ctx , false );
67112 }
68-
113+
69114 /**
70115 * Starts {@link SuspendableRunnable} code block and returns a continuation,
71116 * corresponding to the first {@link Continuation#suspend()} call inside this code block
72117 * (incl. nested continuable method calls), if any exists. Returns null if the code
73118 * is not suspended.
74119 *
75120 * @param o a continuable code block
121+ * @param ctx an initial argument for the continuable code
122+ * @param optimized
123+ * If true then continuation constructed is performance-optimized but
124+ * may be resumed only once. Otherwise "restartable" continuation is created that may
125+ * be resumed multiple times.
76126 * @return the first continuation suspended
77127 */
78- public static Continuation start (SuspendableRunnable o ) {
79- return Continuation .startWith (toRunnable (o ));
128+ public static Continuation start (SuspendableRunnable o , Object ctx , boolean optimized ) {
129+ return Continuation .startWith (toRunnable (o ), ctx , optimized );
80130 }
81131
132+
82133 /**
83134 * <p>Convert a <code>coroutine</code> to the {@link Iterator} of emitted values
84135 *
@@ -101,7 +152,7 @@ public static <T> CloseableIterator<T> iteratorOf(Continuation coroutine) {
101152 * @return the iterator over emitted values
102153 */
103154 public static <T > CloseableIterator <T > iteratorOf (Continuation coroutine , boolean useCurrentValue ) {
104- return new ContinuationIterator <>(coroutine , useCurrentValue );
155+ return new ContinuationIterator <>(coroutine . optimized () , useCurrentValue );
105156 }
106157
107158
@@ -113,7 +164,7 @@ public static <T> CloseableIterator<T> iteratorOf(Continuation coroutine, boolea
113164 * @return the iterator over emitted values
114165 */
115166 public static <T > CloseableIterator <T > iteratorOf (SuspendableRunnable coroutine ) {
116- return iteratorOf (create (coroutine ), false );
167+ return iteratorOf (create (coroutine , true ), false );
117168 }
118169
119170 /**
@@ -152,7 +203,7 @@ public static <T> Stream<T> streamOf(Continuation coroutine, boolean useCurrentV
152203 * @return the iterator over emitted values
153204 */
154205 public static <T > Stream <T > streamOf (SuspendableRunnable coroutine ) {
155- return streamOf (create (coroutine ), false );
206+ return streamOf (create (coroutine , true ), false );
156207 }
157208
158209 /**
@@ -195,7 +246,7 @@ public static <T> void forEach(Continuation coroutine, boolean useCurrentValue,
195246 * @param action a non-continuable action to perform on the values emitted
196247 */
197248 public static <T > void forEach (SuspendableRunnable coroutine , Consumer <? super T > action ) {
198- forEach (create (coroutine ), action );
249+ forEach (create (coroutine , true ), action );
199250 }
200251
201252
@@ -239,7 +290,7 @@ public static <T> void forEachReply(Continuation coroutine, Function<? super T,
239290 * resume coroutine.
240291 */
241292 public static <T > void forEachReply (Continuation coroutine , boolean useCurrentValue , Function <? super T , ?> action ) {
242- Continuation cc = coroutine ;
293+ Continuation cc = coroutine . optimized () ;
243294 try {
244295 Object param = null ;
245296 if (null != cc && useCurrentValue ) {
@@ -275,7 +326,7 @@ public static <T> void forEachReply(Continuation coroutine, boolean useCurrentVa
275326 * resume continuation.
276327 */
277328 public static <T > void forEachReply (SuspendableRunnable coroutine , Function <? super T , ?> action ) {
278- forEachReply (create (coroutine ), action );
329+ forEachReply (create (coroutine , true ), action );
279330 }
280331
281332 /**
@@ -316,7 +367,7 @@ public static <T> void forEachReply(SuspendableRunnable coroutine, Function<? su
316367 * @param action a continuable action to perform on the values emitted
317368 */
318369 public static @ continuable <T > void forEach$ (SuspendableRunnable coroutine , SuspendableConsumer <? super T > action ) {
319- forEach$ (create (coroutine ), false , action );
370+ forEach$ (create (coroutine , true ), false , action );
320371 }
321372
322373 /**
@@ -359,7 +410,7 @@ public static <T> void forEachReply(SuspendableRunnable coroutine, Function<? su
359410 * resume coroutine.
360411 */
361412 public static @ continuable <T > void forEachReply$ (Continuation coroutine , boolean useCurrentValue , SuspendableFunction <? super T , ?> action ) {
362- Continuation cc = coroutine ;
413+ Continuation cc = coroutine . optimized () ;
363414 try {
364415 Object param = null ;
365416 if (null != cc && useCurrentValue ) {
@@ -395,7 +446,7 @@ public static <T> void forEachReply(SuspendableRunnable coroutine, Function<? su
395446 * resume coroutine.
396447 */
397448 public static @ continuable <T > void forEachReply$ (SuspendableRunnable coroutine , SuspendableFunction <? super T , ?> action ) {
398- forEachReply$ (create (coroutine ), action );
449+ forEachReply$ (create (coroutine , true ), action );
399450 }
400451
401452 /**
0 commit comments