com.mongodb.internal.async.AsyncSupplier Maven / Gradle / Ivy
Show all versions of mongodb-driver-core Show documentation
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 com.mongodb.internal.async;
import com.mongodb.lang.Nullable;
import java.util.function.Predicate;
/**
* See {@link AsyncRunnable}
*
* This class is not part of the public API and may be removed or changed at any time
*/
@FunctionalInterface
public interface AsyncSupplier extends AsyncFunction {
/**
* This should not be called externally to this API. It should be
* implemented as a lambda. To "finish" an async chain, use one of
* the "finish" methods.
*
* @see #finish(SingleResultCallback)
*/
void unsafeFinish(SingleResultCallback callback);
/**
* This is the async variant of a supplier's get method.
* This method must only be used when this AsyncSupplier corresponds
* to a {@link java.util.function.Supplier} (and is therefore being
* used within an async chain method lambda).
* @param callback the callback
*/
default void getAsync(final SingleResultCallback callback) {
finish(callback);
}
@Override
default void unsafeFinish(@Nullable final Void value, final SingleResultCallback callback) {
unsafeFinish(callback);
}
/**
* Must be invoked at end of async chain.
* @param callback the callback provided by the method the chain is used in
*/
default void finish(final SingleResultCallback callback) {
final boolean[] callbackInvoked = {false};
try {
this.unsafeFinish((v, e) -> {
callbackInvoked[0] = true;
callback.onResult(v, e);
});
} catch (Throwable t) {
if (callbackInvoked[0]) {
throw t;
} else {
callback.completeExceptionally(t);
}
}
}
/**
* @param function The async function to run after this supplier
* @return the composition of this supplier and the function, a supplier
* @param The return type of the resulting supplier
*/
default AsyncSupplier thenApply(final AsyncFunction function) {
return (c) -> {
this.unsafeFinish((v, e) -> {
if (e == null) {
function.unsafeFinish(v, c);
} else {
c.completeExceptionally(e);
}
});
};
}
/**
* @param consumer The async consumer to run after this supplier
* @return the composition of this supplier and the consumer, a runnable
*/
default AsyncRunnable thenConsume(final AsyncConsumer consumer) {
return (c) -> {
this.unsafeFinish((v, e) -> {
if (e == null) {
consumer.unsafeFinish(v, c);
} else {
c.completeExceptionally(e);
}
});
};
}
/**
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow
* @param errorFunction The branch to execute if the error matches
* @return The composition of this, and the conditional branch
*/
default AsyncSupplier onErrorIf(
final Predicate errorCheck,
final AsyncFunction errorFunction) {
// finish is used here instead of unsafeFinish to ensure that
// exceptions thrown from the callback are properly handled
return (callback) -> this.finish((r, e) -> {
if (e == null) {
callback.complete(r);
return;
}
boolean errorMatched;
try {
errorMatched = errorCheck.test(e);
} catch (Throwable t) {
t.addSuppressed(e);
callback.completeExceptionally(t);
return;
}
if (errorMatched) {
errorFunction.unsafeFinish(e, callback);
} else {
callback.completeExceptionally(e);
}
});
}
}