All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.driver.internal.reactive.RxUtils Maven / Gradle / Ivy

There is a newer version: 5.21.0
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * 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 org.neo4j.driver.internal.reactive;

import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import org.neo4j.driver.internal.util.Futures;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

public class RxUtils {
    /**
     * The publisher created by this method will either succeed without publishing anything or fail with an error.
     * @param supplier supplies a {@link CompletionStage}.
     * @return A publisher that publishes nothing on completion or fails with an error.
     */
    public static  Publisher createEmptyPublisher(Supplier> supplier) {
        return Mono.create(sink -> supplier.get().whenComplete((ignore, completionError) -> {
            Throwable error = Futures.completionExceptionCause(completionError);
            if (error != null) {
                sink.error(error);
            } else {
                sink.success();
            }
        }));
    }

    /**
     * The publisher created by this method will either succeed with exactly one item or fail with an error.
     *
     * @param supplier                    supplies a {@link CompletionStage} that MUST produce a non-null result when completed successfully.
     * @param nullResultThrowableSupplier supplies a {@link Throwable} that is used as an error when the supplied completion stage completes successfully with
     *                                    null.
     * @param                          the type of the item to publish.
     * @return A publisher that succeeds exactly one item or fails with an error.
     */
    public static  Publisher createSingleItemPublisher(
            Supplier> supplier, Supplier nullResultThrowableSupplier) {
        return Mono.create(sink -> supplier.get().whenComplete((item, completionError) -> {
            if (completionError == null) {
                if (item != null) {
                    sink.success(item);
                } else {
                    sink.error(nullResultThrowableSupplier.get());
                }
            } else {
                Throwable error = Optional.ofNullable(Futures.completionExceptionCause(completionError))
                        .orElse(completionError);
                sink.error(error);
            }
        }));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy