hu.akarnokd.asyncenum.AsyncUsing Maven / Gradle / Ivy
/*
* 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.AtomicBoolean;
import java.util.function.*;
final class AsyncUsing implements AsyncEnumerable {
final Supplier resource;
final Function super U, ? extends AsyncEnumerable> handler;
final Consumer super U> releaseResource;
AsyncUsing(Supplier resource, Function super U, ? extends AsyncEnumerable> handler, Consumer super U> releaseResource) {
this.resource = resource;
this.handler = handler;
this.releaseResource = releaseResource;
}
@Override
public AsyncEnumerator enumerator() {
U res = resource.get();
return new UsingEnumerator<>(res, handler.apply(res).enumerator(), releaseResource);
}
static final class UsingEnumerator
extends AtomicBoolean
implements AsyncEnumerator, BiConsumer {
final U resource;
final AsyncEnumerator source;
final Consumer super U> release;
T result;
CompletableFuture completable;
UsingEnumerator(U resource, AsyncEnumerator source, Consumer super U> release) {
this.resource = resource;
this.source = source;
this.release = release;
}
@Override
public CompletionStage moveNext() {
result = null;
CompletableFuture cf = new CompletableFuture<>();
completable = cf;
source.moveNext().whenComplete(this);
return cf;
}
@Override
public T current() {
return result;
}
@Override
public void cancel() {
source.cancel();
cleanup();
}
void cleanup() {
if (compareAndSet(false, true)) {
release.accept(resource);
}
}
@Override
public void accept(Boolean aBoolean, Throwable throwable) {
if (throwable != null) {
completable.completeExceptionally(throwable);
cleanup();
return;
}
if (aBoolean) {
result = source.current();
completable.complete(true);
} else {
completable.complete(false);
cleanup();
}
}
}
}