Skip to content

Commit 59f34f5

Browse files
committed
Upgrade *extras* to JDK 1.8; add std. @Continuable forEach to Continuations class for Stream/Iterable/ContinuableRunnable(coroutine)
1 parent 37818a0 commit 59f34f5

File tree

9 files changed

+110
-90
lines changed

9 files changed

+110
-90
lines changed
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package org.apache.commons.javaflow.examples.lambdas;
22

3-
import java.util.Iterator;
4-
import java.util.function.Consumer;
5-
import java.util.stream.Stream;
6-
7-
import org.apache.commons.javaflow.api.continuable;
3+
import org.apache.commons.javaflow.extras.ContinuableConsumer;
84
import org.apache.commons.javaflow.extras.ContinuableRunnable;
95

106
public class ContinuableAdapters {
@@ -17,21 +13,7 @@ public static <T> ContinuableConsumer<T> accept(ContinuableConsumer<T> o) {
1713
return o;
1814
}
1915

20-
public @continuable static <T> void forEach(Stream<T> o, ContinuableConsumer<? super T> action) {
21-
forEach(o.iterator(), action);
22-
}
23-
24-
public @continuable static <T> void forEach(Iterable<T> o, ContinuableConsumer<? super T> action) {
25-
forEach(o.iterator(), action);
26-
}
27-
28-
public @continuable static <T> void forEach(Iterator<T> it, ContinuableConsumer<? super T> action) {
29-
while (it.hasNext()) {
30-
T v = it.next();
31-
action.accept(v);
32-
}
33-
}
34-
16+
/*
3517
interface ContinuableConsumer<T> extends Consumer<T> {
3618
@continuable void accept(T t);
3719
@@ -47,4 +29,5 @@ default ContinuableConsumer<T> andThen(Consumer<? super T> after) {
4729
return andThen((ContinuableConsumer<? super T>)after);
4830
}
4931
}
32+
*/
5033
}

net.tascalate.javaflow.examples/src/main/java/org/apache/commons/javaflow/examples/lambdas/LambdasExample.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.apache.commons.javaflow.examples.lambdas.ContinuableAdapters.accept;
44
import static org.apache.commons.javaflow.examples.lambdas.ContinuableAdapters.exec;
5-
import static org.apache.commons.javaflow.examples.lambdas.ContinuableAdapters.forEach;
65

76
import java.util.Arrays;
87
import java.util.List;
@@ -16,10 +15,9 @@
1615
public class LambdasExample {
1716
public static void main(final String[] argv) throws Exception {
1817
LambdasExample example = new LambdasExample();
19-
20-
for (Continuation cc = Continuations.start(example::executeAll); null != cc; cc = cc.resume()) {
21-
System.out.println("Interrupted " + cc.value());
22-
}
18+
Continuations.<String>forEach(example::executeAll, s -> {
19+
System.out.println("Interrupted " + s);
20+
});
2321

2422
System.out.println("===");
2523

@@ -53,7 +51,7 @@ public static void main(final String[] argv) throws Exception {
5351
// See org.apache.commons.javaflow.examples.lambdas.ContinuableAdapters for
5452
// definition of "accept" & "forEach"
5553
List<String> listOfStrings = Arrays.asList("A", null, "B", null, "C");
56-
forEach(
54+
Continuations.forEach(
5755
listOfStrings.stream().filter(s -> s != null).map(s -> s + s),
5856
accept(this::yieldString1).andThen(this::yieldString2)
5957
);

net.tascalate.javaflow.extras/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
2424
</classpathentry>
25-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
2626
<attributes>
2727
<attribute name="maven.pomderived" value="true"/>
2828
</attributes>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3-
org.eclipse.jdt.core.compiler.compliance=1.5
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
44
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5-
org.eclipse.jdt.core.compiler.source=1.5
5+
org.eclipse.jdt.core.compiler.source=1.8

net.tascalate.javaflow.extras/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
</dependencies>
3434
<build>
3535
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<configuration>
40+
<source>1.8</source>
41+
<target>1.8</target>
42+
</configuration>
43+
</plugin>
3644
<plugin>
3745
<groupId>net.tascalate.javaflow</groupId>
3846
<artifactId>net.tascalate.javaflow.tools.maven</artifactId>

net.tascalate.javaflow.extras/src/main/java/org/apache/commons/javaflow/extras/CoRoutine.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.apache.commons.javaflow.extras;
2+
3+
import java.util.Objects;
4+
5+
import org.apache.commons.javaflow.api.continuable;
6+
7+
/**
8+
* Represents an continuable operation that accepts a single input argument and returns no
9+
* result. Unlike most other functional interfaces, {@code ContinuableConsumer} is expected
10+
* to operate via side-effects.
11+
*
12+
* <p>This is a functional interface whose functional method is {@link #accept(Object)}.
13+
*
14+
* @param <T> the type of the input to the operation
15+
*
16+
*/
17+
@FunctionalInterface
18+
public interface ContinuableConsumer<T> {
19+
20+
/**
21+
* Performs this operation on the given argument.
22+
*
23+
* @param t the input argument
24+
*/
25+
@continuable void accept(T t);
26+
27+
/**
28+
* Returns a composed {@code ContinuableConsumer} that performs, in sequence, this
29+
* operation followed by the {@code after} operation. If performing either
30+
* operation throws an exception, it is relayed to the caller of the
31+
* composed operation. If performing this operation throws an exception,
32+
* the {@code after} operation will not be performed.
33+
*
34+
* @param after the operation to perform after this operation
35+
* @return a composed {@code ContinuableConsumer} that performs in sequence this
36+
* operation followed by the {@code after} operation
37+
* @throws NullPointerException if {@code after} is null
38+
*/
39+
default ContinuableConsumer<T> andThen(ContinuableConsumer<? super T> after) {
40+
Objects.requireNonNull(after);
41+
return new ContinuableConsumer<T>() {
42+
public @continuable void accept(T t) {
43+
ContinuableConsumer.this.accept(t);
44+
after.accept(t);
45+
}
46+
};
47+
}
48+
49+
}

net.tascalate.javaflow.extras/src/main/java/org/apache/commons/javaflow/extras/ContinuableRunnable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import org.apache.commons.javaflow.api.continuable;
44

5+
/**
6+
* Continuable version of Runnable
7+
*/
8+
@FunctionalInterface
59
public interface ContinuableRunnable extends Runnable {
6-
//Re-declare to mark as continuable
10+
/**
11+
* Run method re-declared as continuable
12+
*/
713
@continuable void run();
814
}
Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
package org.apache.commons.javaflow.extras;
22

3+
import java.util.Iterator;
4+
import java.util.stream.Stream;
5+
36
import org.apache.commons.javaflow.api.Continuation;
7+
import org.apache.commons.javaflow.api.continuable;
48

59
final public class Continuations {
6-
private Continuations() {}
7-
8-
public static Continuation create(ContinuableRunnable o) {
9-
return Continuation.startSuspendedWith(o);
10-
}
11-
12-
public static Continuation start(ContinuableRunnable o, Object ctx) {
13-
return Continuation.startWith(o, ctx);
14-
}
15-
16-
public static Continuation start(ContinuableRunnable o) {
17-
return Continuation.startWith(o);
18-
}
10+
private Continuations() {}
11+
12+
public static Continuation create(ContinuableRunnable o) {
13+
return Continuation.startSuspendedWith(o);
14+
}
15+
16+
public static Continuation start(ContinuableRunnable o, Object ctx) {
17+
return Continuation.startWith(o, ctx);
18+
}
19+
20+
public static Continuation start(ContinuableRunnable o) {
21+
return Continuation.startWith(o);
22+
}
23+
24+
public @continuable static <T> void forEach(ContinuableRunnable coroutine, ContinuableConsumer<? super T> action) {
25+
forEach(new CoIterator<>(coroutine), action);
26+
}
27+
28+
public @continuable static <T> void forEach(Stream<T> iterable, ContinuableConsumer<? super T> action) {
29+
forEach(iterable.iterator(), action);
30+
}
31+
32+
public @continuable static <T> void forEach(Iterable<T> iterable, ContinuableConsumer<? super T> action) {
33+
forEach(iterable.iterator(), action);
34+
}
35+
36+
public @continuable static <T> void forEach(Iterator<T> iterator, ContinuableConsumer<? super T> action) {
37+
while (iterator.hasNext()) {
38+
action.accept( iterator.next() );
39+
}
40+
}
1941
}

0 commit comments

Comments
 (0)