RxJava 3 interop library for Java 8

RxJava 3 interop library for supporting Java 8 features such as Optional, Stream and CompletableFuture.

License

License

Categories

Categories

RxJava Container Microservices Reactive libraries
GroupId

GroupId

com.github.akarnokd
ArtifactId

ArtifactId

rxjava3-jdk8-interop
Last Version

Last Version

3.0.0-RC6
Release Date

Release Date

Type

Type

jar
Description

Description

RxJava 3 interop library for Java 8
RxJava 3 interop library for supporting Java 8 features such as Optional, Stream and CompletableFuture.
Project URL

Project URL

https://github.com/akarnokd/RxJavaJdk8Interop/
Source Code Management

Source Code Management

https://github.com/akarnokd/RxJavaJdk8Interop/

Download rxjava3-jdk8-interop

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.akarnokd/rxjava3-jdk8-interop/ -->
<dependency>
    <groupId>com.github.akarnokd</groupId>
    <artifactId>rxjava3-jdk8-interop</artifactId>
    <version>3.0.0-RC6</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.akarnokd/rxjava3-jdk8-interop/
implementation 'com.github.akarnokd:rxjava3-jdk8-interop:3.0.0-RC6'
// https://jarcasting.com/artifacts/com.github.akarnokd/rxjava3-jdk8-interop/
implementation ("com.github.akarnokd:rxjava3-jdk8-interop:3.0.0-RC6")
'com.github.akarnokd:rxjava3-jdk8-interop:jar:3.0.0-RC6'
<dependency org="com.github.akarnokd" name="rxjava3-jdk8-interop" rev="3.0.0-RC6">
  <artifact name="rxjava3-jdk8-interop" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.akarnokd', module='rxjava3-jdk8-interop', version='3.0.0-RC6')
)
libraryDependencies += "com.github.akarnokd" % "rxjava3-jdk8-interop" % "3.0.0-RC6"
[com.github.akarnokd/rxjava3-jdk8-interop "3.0.0-RC6"]

Dependencies

compile (1)

Group / Artifact Type Version
io.reactivex.rxjava3 : rxjava jar 3.0.0-RC6

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

RxJavaJdk8Interop

⚠️ Discontinued

The features of this library (and more) have been integrated into RxJava 3 proper starting with version 3.0.0-RC7.


codecov.io Maven Central

RxJava 3.x: RxJava 3.x

RxJava 3 interop library for supporting Java 8 features such as Optional, Stream and CompletableFuture.

Release

RxJava 3

compile 'com.github.akarnokd:rxjava3-jdk8-interop:3.0.0-RC6'

RxJava 2

compile 'com.github.akarnokd:rxjava2-jdk8-interop:0.3.7'

Examples

Javadocs: https://akarnokd.github.com/RxJavaJdk8Interop/javadoc/index.html

The main entry points are:

  • FlowableInterop
  • ObservableInterop
  • SingleInterop
  • MaybeInterop
  • CompletableInterop

Stream to RxJava

Note that java.util.stream.Stream can be consumed at most once and only synchronously.

Stream<T> stream = ...

Flowable<T> flow = FlowableInterop.fromStream(stream);

Observable<T> obs = ObservableInterop.fromStream(stream);

Optional to RxJava

Optional<T> opt = ...

Flowable<T> flow = FlowableInterop.fromOptional(opt);

Observable<T> obs = ObservableInterop.fromOptional(opt);

CompletionStage to RxJava

Note that cancelling the Subscription won't cancel the CompletionStage.

CompletionStage<T> cs = ...

Flowable<T> flow = FlowableInterop.fromFuture(cs);

Observable<T> flow = ObservableInterop.fromFuture(cs);

Using Stream Collectors

Flowable.range(1, 10)
.compose(FlowableInterop.collect(Collectors.toList()))
.test()
.assertResult(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

Return the first/single/last element as a CompletionStage

CompletionStage<Integer> cs = Flowable.just(1)
.delay(1, TimeUnit.SECONDS)
// return first
.to(FlowableInterop.first());

// return single
// .to(FlowableInterop.single());

// return last
// .to(FlowableInterop.last());

cs.whenComplete((v, e) -> {
   System.out.println(v);
   System.out.println(e);
});

Return the only element as a CompletionStage

Single

CompletionStage<Integer> cs = Single.just(1)
.delay(1, TimeUnit.SECONDS)
.to(SingleInterop.get());

cs.whenComplete((v, e) -> {
   System.out.println(v);
   System.out.println(e);
});

Maybe

CompletionStage<Integer> cs = Maybe.just(1)
.delay(1, TimeUnit.SECONDS)
.to(MaybeInterop.get());

cs.whenComplete((v, e) -> {
   System.out.println(v);
   System.out.println(e);
});

Await completion as CompletionStage

Completable

CompletionStage<Void> cs = Completable.complete()
.delay(1, TimeUnit.SECONDS)
.to(CompletableInterop.await());

cs.whenComplete((v, e) -> {
   System.out.println(v);
   System.out.println(e);
});

Return the first/last element optionally

This is a blocking operation

Optional<Integer> opt = Flowable.just(1)
.to(FlowableInterop.firstElement());

System.out.println(opt.map(v -> v + 1).orElse(-1));

Convert to Java Stream

This is a blocking operation. Closing the stream will cancel the RxJava sequence.

Flowable.range(1, 10)
.to(FlowableInterop.toStream())
.parallel()
.map(v -> v + 1)
.forEach(System.out::println);

FlatMap Java Streams

Note that since consuming a stream is practically blocking, there is no need for a maxConcurrency parameter.

Flowable.range(1, 5)
.compose(FlowableInterop.flatMapStream(v -> Arrays.asList(v, v + 1).stream()))
.test()
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6);

Map based on Java Optional

Flowable.range(1, 5)
.compose(FlowableInterop.mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty()))
.test()
.assertResult(2, 4);

Versions

Version
3.0.0-RC6
3.0.0-RC5
3.0.0-RC4
3.0.0-RC3
3.0.0-RC2
3.0.0-RC1