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

hu.akarnokd.rxjava3.fibers.FiberInterop Maven / Gradle / Ivy

Go to download

Library to interoperate between RxJava 3 and Virtual Threads (aka Fibers, Project Loom)

There is a newer version: 0.0.18
Show newest version
/*
 * Copyright 2019 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.rxjava3.fibers;

import java.util.Objects;
import java.util.concurrent.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/**
 * Sources, transformers and consumers working with fiber-based suspendable methods.
 * 
 * @since 0.0.1
 */
public final class FiberInterop {

    /** Utility class. */
    private FiberInterop() {
        throw new IllegalStateException("No instances!");
    }

    public static  Flowable create(FiberGenerator generator) {
        return create(generator, ForkJoinPool.commonPool());
    }

    public static  Flowable create(FiberGenerator generator, Scheduler scheduler) {
        Objects.requireNonNull(generator, "generator is null");
        Objects.requireNonNull(scheduler, "scheduler is null");
        return RxJavaPlugins.onAssembly(new FlowableCreateFiberScheduler<>(generator, scheduler));
    }

    public static  Flowable create(FiberGenerator generator, ExecutorService executor) {
        Objects.requireNonNull(generator, "generator is null");
        Objects.requireNonNull(executor, "executor is null");
        return RxJavaPlugins.onAssembly(new FlowableCreateFiberExecutor<>(generator, executor));
    }

    public static  FlowableTransformer transform(FiberTransformer transformer, Scheduler scheduler) {
        return transform(transformer, scheduler, Flowable.bufferSize());
    }

    public static  FlowableTransformer transform(FiberTransformer transformer, Scheduler scheduler, int prefetch) {
        Objects.requireNonNull(transformer, "transformer is null");
        Objects.requireNonNull(scheduler, "scheduler is null");
        ObjectHelper.verifyPositive(prefetch, "prefetch");
        return new FlowableTransformFiberScheduler<>(null, transformer, scheduler, prefetch);
    }

    public static  FlowableTransformer transform(FiberTransformer transformer) {
        return transform(transformer, ForkJoinPool.commonPool(), Flowable.bufferSize());
    }

    public static  FlowableTransformer transform(FiberTransformer transformer, int prefetch) {
        return transform(transformer, ForkJoinPool.commonPool(), prefetch);
    }

    public static  FlowableTransformer transform(FiberTransformer transformer, ExecutorService scheduler) {
        return transform(transformer, scheduler, Flowable.bufferSize());
    }

    public static  FlowableTransformer transform(FiberTransformer transformer, ExecutorService scheduler, int prefetch) {
        Objects.requireNonNull(transformer, "transformer is null");
        Objects.requireNonNull(scheduler, "scheduler is null");
        ObjectHelper.verifyPositive(prefetch, "prefetch");
        return new FlowableTransformFiberExecutor<>(null, transformer, scheduler, prefetch);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy