Skip to content

Commit 6158001

Browse files
committed
Mandatory cancellation support, coverage tests
1 parent 3741210 commit 6158001

29 files changed

+218
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Prototype Java 9 library based on the asynchronous enumerable concept (where mov
99
### Gradle
1010

1111
```groovy
12-
compile "com.github.akarnokd:async-enumerable:0.5.0"
12+
compile "com.github.akarnokd:async-enumerable:0.6.0"
1313
```
1414

1515
### Getting started

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=0.5.0
1+
version=0.6.0
22
org.gradle.jvmargs=-XX:+IgnoreUnrecognizedVMOptions --permit-illegal-access --show-version
33

src/main/java/hu/akarnokd/asyncenum/AsyncConcatArray.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package hu.akarnokd.asyncenum;
1818

1919
import java.util.concurrent.*;
20-
import java.util.concurrent.atomic.AtomicInteger;
20+
import java.util.concurrent.atomic.*;
2121
import java.util.function.BiConsumer;
2222

2323
final class AsyncConcatArray<T> implements AsyncEnumerable<T> {
@@ -38,33 +38,36 @@ static final class ConcatArrayEnumerator<T> extends AtomicInteger
3838

3939
final AsyncEnumerable<T>[] sources;
4040

41-
AsyncEnumerator<T> currentEnumerator;
41+
final AtomicReference<AsyncEnumerator<T>> currentEnumerator;
4242

4343
CompletableFuture<Boolean> currentStage;
4444

4545
int index;
4646

4747
ConcatArrayEnumerator(AsyncEnumerable<T>[] sources) {
4848
this.sources = sources;
49+
this.currentEnumerator = new AtomicReference<>();
4950
}
5051

5152
@Override
5253
public CompletionStage<Boolean> moveNext() {
53-
if (currentEnumerator == null) {
54+
if (currentEnumerator.get() == null) {
5455
if (index == sources.length) {
5556
return FALSE;
5657
}
57-
currentEnumerator = sources[index++].enumerator();
58+
if (!AsyncEnumeratorHelper.replace(currentEnumerator, sources[index++].enumerator())) {
59+
return CANCELLED;
60+
}
5861
}
5962

6063
currentStage = new CompletableFuture<>();
61-
currentEnumerator.moveNext().whenComplete(this);
64+
currentEnumerator.getPlain().moveNext().whenComplete(this);
6265
return currentStage;
6366
}
6467

6568
@Override
6669
public T current() {
67-
return currentEnumerator.current();
70+
return currentEnumerator.getPlain().current();
6871
}
6972

7073
@Override
@@ -82,11 +85,20 @@ public void accept(Boolean aBoolean, Throwable throwable) {
8285
currentStage.complete(false);
8386
break;
8487
}
85-
currentEnumerator = sources[index++].enumerator();
86-
currentEnumerator.moveNext().whenComplete(this);
88+
AsyncEnumerator<T> en = sources[index++].enumerator();
89+
if (AsyncEnumeratorHelper.replace(currentEnumerator, en)) {
90+
en.moveNext().whenComplete(this);
91+
} else {
92+
break;
93+
}
8794
} while (decrementAndGet() != 0);
8895
}
8996
}
9097
}
98+
99+
@Override
100+
public void cancel() {
101+
AsyncEnumeratorHelper.cancel(currentEnumerator);
102+
}
91103
}
92104
}

src/main/java/hu/akarnokd/asyncenum/AsyncEmpty.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public CompletionStage<Boolean> moveNext() {
4040
public Object current() {
4141
return null;
4242
}
43+
44+
@Override
45+
public void cancel() {
46+
// No action, consumer should stop calling moveNext().
47+
}
4348
}

src/main/java/hu/akarnokd/asyncenum/AsyncEnumerator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,5 @@ public interface AsyncEnumerator<T> {
5151
* Instructs the AsyncEnumerator to cancel any outstanding async activity and
5252
* release resources associated with it.
5353
*/
54-
// FIXME make mandatory
55-
default void cancel() {
56-
57-
}
54+
void cancel();
5855
}

src/main/java/hu/akarnokd/asyncenum/AsyncEnumeratorHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public Object current() {
3333
return null;
3434
}
3535

36+
@Override
37+
public void cancel() {
38+
// No action, consumer should stop calling moveNext().
39+
}
40+
3641
@SuppressWarnings("unchecked")
3742
static <T> boolean cancel(AtomicReference<AsyncEnumerator<T>> target) {
3843
AsyncEnumerator<?> current = target.getAndSet((AsyncEnumerator<T>)CANCELLED);

src/main/java/hu/akarnokd/asyncenum/AsyncError.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public CompletionStage<Boolean> moveNext() {
4040
public T current() {
4141
return null;
4242
}
43+
44+
@Override
45+
public void cancel() {
46+
// No action, consumer should stop calling moveNext().
47+
}
4348
}

src/main/java/hu/akarnokd/asyncenum/AsyncFromArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public CompletionStage<Boolean> moveNext() {
5959
public T current() {
6060
return current;
6161
}
62+
63+
@Override
64+
public void cancel() {
65+
// No action, consumer should stop calling moveNext().
66+
}
6267
}
6368
}

src/main/java/hu/akarnokd/asyncenum/AsyncFromCallable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ public CompletionStage<Boolean> moveNext() {
6262
public T current() {
6363
return result;
6464
}
65+
66+
@Override
67+
public void cancel() {
68+
// No action, consumer should stop calling moveNext().
69+
}
6570
}
6671
}

src/main/java/hu/akarnokd/asyncenum/AsyncFromCharSequence.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public CompletionStage<Boolean> moveNext() {
5959
public Integer current() {
6060
return current;
6161
}
62+
63+
@Override
64+
public void cancel() {
65+
// No action, consumer should stop calling moveNext().
66+
}
6267
}
6368
}

0 commit comments

Comments
 (0)