hu.akarnokd.rxjava2.interop.FlowInterop Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava2-jdk9-interop Show documentation
Show all versions of rxjava2-jdk9-interop Show documentation
rxjava2-jdk9-interop developed by David Karnok
The newest version!
/*
* Copyright 2016-2018 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.rxjava2.interop;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.processors.FlowableProcessor;
import java.util.concurrent.Flow;
/**
* Converters to and from Java 9 Flow components.
* @since 0.1.0
*/
public final class FlowInterop {
/** Utility class. */
private FlowInterop() {
throw new IllegalStateException("No instances!");
}
/**
* Wraps a Flow.Publisher into a Flowable.
* @param source the source Flow.Publisher, not null
* @param the value type
* @return the new Flowable instance
* @throws NullPointerException if source is null
*/
@SuppressWarnings("unchecked")
public static Flowable fromFlowPublisher(Flow.Publisher source) {
if (source instanceof Flowable) {
return (Flowable)source;
}
if (source instanceof org.reactivestreams.Publisher) {
return Flowable.fromPublisher((org.reactivestreams.Publisher)source);
}
ObjectHelper.requireNonNull(source, "source is null");
return RxJavaPlugins.onAssembly(new FlowableFromFlowPublisher<>(source));
}
/**
* Converter function from a Flowable into a Publisher.
* @param the value type
* @return the Function instance to be used with {@code Flowable.to()}.
*/
public static Function, Flow.Publisher> toFlow() {
return FlowFromPublisher::new;
}
/**
* Wraps a Flow.Processor (identity) into a FlowableProcessor.
* @param source the source Flow.Processor, not null
* @param the input and output type of the Flow.Processor
* @return the new FlowableProcessor instance
* @throws NullPointerException if source is null
*/
@SuppressWarnings("unchecked")
public static FlowableProcessor fromFlowProcessor(Flow.Processor source) {
if (source instanceof FlowableProcessor) {
return (FlowableProcessor)source;
}
ObjectHelper.requireNonNull(source, "source is null");
return new FlowableProcessorFromFlowProcessor<>(source);
}
/**
* Wraps an RS Publisher into a Flow.Publisher.
* @param source the source RS Publisher instance, not null
* @param the value type
* @return the new Flow.Publisher instance
* @throws NullPointerException if source is null
*/
@SuppressWarnings("unchecked")
public static Flow.Publisher toFlowPublisher(org.reactivestreams.Publisher source) {
if (source instanceof Flow.Publisher) {
return (Flow.Publisher)source;
}
ObjectHelper.requireNonNull(source, "source is null");
return new FlowFromPublisher<>(source);
}
/**
* Wraps an RS Processor into a Flow.Processor.
* @param source the source RS Processor
* @param the input value type
* @param the output value type
* @return the new Flow.Processor instance
* @throws NullPointerException if source is null
*/
@SuppressWarnings("unchecked")
public static Flow.Processor toFlowProcessor(org.reactivestreams.Processor source) {
if (source instanceof Flow.Processor) {
return (Flow.Processor)source;
}
ObjectHelper.requireNonNull(source, "source is null");
return new FlowProcessorFromProcessor<>(source);
}
}