-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More operators, prepare release 0.3.0
- Loading branch information
Showing
21 changed files
with
1,367 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
version=0.2.0 | ||
version=0.3.0 | ||
org.gradle.jvmargs=-XX:+IgnoreUnrecognizedVMOptions --permit-illegal-access --show-version | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2017 David Karnok | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package hu.akarnokd.asyncenum; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
final class AsyncDoOnCancel<T> implements AsyncEnumerable<T> { | ||
|
||
final AsyncEnumerable<T> source; | ||
|
||
final Runnable onCancel; | ||
|
||
AsyncDoOnCancel(AsyncEnumerable<T> source, Runnable onCancel) { | ||
this.source = source; | ||
this.onCancel = onCancel; | ||
} | ||
|
||
@Override | ||
public AsyncEnumerator<T> enumerator() { | ||
return new DoOnCancelEnumerator<>(source.enumerator(), onCancel); | ||
} | ||
|
||
static final class DoOnCancelEnumerator<T> implements AsyncEnumerator<T> { | ||
|
||
final AsyncEnumerator<T> source; | ||
|
||
final Runnable onCancel; | ||
|
||
DoOnCancelEnumerator(AsyncEnumerator<T> source, Runnable onCancel) { | ||
this.source = source; | ||
this.onCancel = onCancel; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Boolean> moveNext() { | ||
return source.moveNext(); | ||
} | ||
|
||
@Override | ||
public T current() { | ||
return source.current(); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
onCancel.run(); | ||
source.cancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/hu/akarnokd/asyncenum/AsyncFromCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2017 David Karnok | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package hu.akarnokd.asyncenum; | ||
|
||
import java.util.concurrent.*; | ||
|
||
final class AsyncFromCallable<T> implements AsyncEnumerable<T> { | ||
|
||
final Callable<? extends T> callable; | ||
|
||
AsyncFromCallable(Callable<? extends T> callable) { | ||
this.callable = callable; | ||
} | ||
|
||
@Override | ||
public AsyncEnumerator<T> enumerator() { | ||
return new FromCallableEnumerator<>(callable); | ||
} | ||
|
||
static final class FromCallableEnumerator<T> implements AsyncEnumerator<T> { | ||
|
||
final Callable<? extends T> callable; | ||
|
||
T result; | ||
|
||
boolean once; | ||
|
||
FromCallableEnumerator(Callable<? extends T> callable) { | ||
this.callable = callable; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Boolean> moveNext() { | ||
if (once) { | ||
result = null; | ||
return FALSE; | ||
} | ||
once = true; | ||
try { | ||
result = callable.call(); | ||
} catch (Exception ex) { | ||
return CompletableFuture.failedStage(ex); | ||
} | ||
return TRUE; | ||
} | ||
|
||
@Override | ||
public T current() { | ||
return result; | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/hu/akarnokd/asyncenum/AsyncIgnoreElements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2017 David Karnok | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package hu.akarnokd.asyncenum; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.BiConsumer; | ||
|
||
final class AsyncIgnoreElements<T> implements AsyncEnumerable<T> { | ||
|
||
final AsyncEnumerable<T> source; | ||
|
||
AsyncIgnoreElements(AsyncEnumerable<T> source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public AsyncEnumerator<T> enumerator() { | ||
return new IgnoreElementsEnumerator<>(source.enumerator()); | ||
} | ||
|
||
static final class IgnoreElementsEnumerator<T> | ||
extends AtomicInteger | ||
implements AsyncEnumerator<T>, BiConsumer<Boolean, Throwable> { | ||
|
||
final AsyncEnumerator<T> source; | ||
|
||
CompletableFuture<Boolean> completable; | ||
|
||
IgnoreElementsEnumerator(AsyncEnumerator<T> source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Boolean> moveNext() { | ||
CompletableFuture<Boolean> cf = new CompletableFuture<>(); | ||
completable = cf; | ||
nextSource(); | ||
return cf; | ||
} | ||
|
||
void nextSource() { | ||
if (getAndIncrement() == 0) { | ||
do { | ||
source.moveNext().whenComplete(this); | ||
} while (decrementAndGet() != 0); | ||
} | ||
} | ||
|
||
@Override | ||
public T current() { | ||
return null; // elements are ignored | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
source.cancel(); | ||
} | ||
|
||
@Override | ||
public void accept(Boolean aBoolean, Throwable throwable) { | ||
if (throwable != null) { | ||
completable.completeExceptionally(throwable); | ||
return; | ||
} | ||
if (aBoolean) { | ||
nextSource(); | ||
} else { | ||
completable.complete(false); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.