Skip to content

Commit

Permalink
Add Scheduler v1->v2 conversion, cleanup v2->v1
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd committed Jan 27, 2018
1 parent 913f2de commit 1961f60
Show file tree
Hide file tree
Showing 27 changed files with 422 additions and 45 deletions.
2 changes: 1 addition & 1 deletion HEADER
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright ${year} David Karnok
Copyright 2016-${year} David Karnok

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Library to convert between RxJava 1.x and 2.x reactive types.

```
dependencies {
compile "com.github.akarnokd:rxjava2-interop:0.11.5"
compile "com.github.akarnokd:rxjava2-interop:0.12.0"
}
```

Expand All @@ -28,7 +28,7 @@ Maven search:

# Usage

### Converting between the reactive base types
### Convert between the reactive base types

```java
import hu.akarnokd.rxjava.interop.RxJavaInterop;
Expand Down Expand Up @@ -62,7 +62,7 @@ rx.Single s1m = RxJavaInterop.toV1Single(MaybeSource);
rx.Completable c1m = RxJavaInterop.toV1Completable(MaybeSource);
```

### Converting between Subjects and Processors.
### Convert between Subjects and Processors.

Note that 2.x `Subject`s and `FlowableProcessor`s support only the same input and output types.

Expand All @@ -80,7 +80,7 @@ rx.subjects.Subject sj1 = RxJavaInterop.toV1Subject(io.reactivex.subjects.Subjec
rx.subjects.Subject sj1b = RxJavaInterop.toV1Subject(io.reactivex.processors.FlowableProcessor);
```

### Converting between 1.x `X.Transformer`s and 2.x `XTransformer`s.
### Convert between 1.x `X.Transformer`s and 2.x `XTransformer`s.

```java
// convert from 1.x to 2.x
Expand Down Expand Up @@ -130,9 +130,14 @@ io.reactivex.disposables.Disposable d2 = RxJavaInterop.toV2Disposable(rx.Subscri
rx.Subscription s1 = RxJavaInterop.toV1Subscription(io.reactivex.disposables.Disposable);
```

### Convert between 2.x `Scheduler` and 1.x `Scheduler`

### Convert between 1.x `Scheduler`s and 2.x `Scheduler`s

```java
// convert from 1.x to 2.x

io.reactivex.Scheduler s2 = RxJavaInterop.toV2Scheduler(rx.Scheduler);

// convert from 2.x to 1.x

rx.Scheduler s1 = RxJavaInterop.toV1Scheduler(io.reactivex.Scheduler);
Expand Down
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies {
signature 'org.codehaus.mojo.signature:java16:1.1@signature'

compile "org.reactivestreams:reactive-streams:1.0.2"
compile "io.reactivex:rxjava:1.3.4"
compile "io.reactivex:rxjava:1.3.5"
compile "io.reactivex.rxjava2:rxjava:2.1.9"

testCompile group: 'junit', name: 'junit', version: '4.12'
Expand Down Expand Up @@ -123,6 +123,10 @@ plugins.withType(EclipsePlugin) {

javadoc {
failOnError = false

options.links("http://docs.oracle.com/javase/7/docs/api/")
options.links("http://reactivex.io/RxJava/1.x/javadoc")
options.links("http://reactivex.io/RxJava/2.x/javadoc")
}

test {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.11.5
version=0.12.0
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/hu/akarnokd/rxjava/interop/RxJavaInterop.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 David Karnok
* 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.
Expand All @@ -16,8 +16,6 @@

package hu.akarnokd.rxjava.interop;

import io.reactivex.Scheduler;

/**
* Conversion methods for converting between 1.x and 2.x reactive types, composing backpressure
* and cancellation through.
Expand Down Expand Up @@ -663,8 +661,19 @@ public static rx.Subscription toV1Subscription(final io.reactivex.disposables.Di
* @return the new 1.x Scheduler instance
* @since 0.12.0
*/
public static rx.Scheduler toV1Scheduler(Scheduler scheduler) {
public static rx.Scheduler toV1Scheduler(io.reactivex.Scheduler scheduler) {
io.reactivex.internal.functions.ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return new SchedulerV2ToSchedulerV1(scheduler);
}

/**
* Convert the 2.x {@link io.reactivex.Scheduler} into a 1.x {@link rx.Scheduler}.
* @param scheduler the 2.x Scheduler to convert
* @return the new 1.x Scheduler instance
* @since 0.12.0
*/
public static io.reactivex.Scheduler toV2Scheduler(rx.Scheduler scheduler) {
io.reactivex.internal.functions.ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return new SchedulerV1ToSchedulerV2(scheduler);
}
}
114 changes: 114 additions & 0 deletions src/main/java/hu/akarnokd/rxjava/interop/SchedulerV1ToSchedulerV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.rxjava.interop;

import java.util.concurrent.TimeUnit;

/**
* Wraps a 1.x {@link rx.Scheduler} and exposes it as a
* 2.x {@link io.reactivex.Scheduler}.
* @since 0.12.0
*/
final class SchedulerV1ToSchedulerV2 extends io.reactivex.Scheduler {

final rx.Scheduler source;

SchedulerV1ToSchedulerV2(rx.Scheduler source) {
this.source = source;
}

@Override
public long now(TimeUnit unit) {
return unit.convert(source.now(), TimeUnit.MILLISECONDS);
}

@Override
public void start() {
if (source instanceof rx.internal.schedulers.SchedulerLifecycle) {
((rx.internal.schedulers.SchedulerLifecycle)source).start();
}
}

@Override
public void shutdown() {
if (source instanceof rx.internal.schedulers.SchedulerLifecycle) {
((rx.internal.schedulers.SchedulerLifecycle)source).shutdown();
}
}

@Override
public Worker createWorker() {
return new WorkerV1ToWorkerV2(source.createWorker());
}

static final class WorkerV1ToWorkerV2 extends io.reactivex.Scheduler.Worker {

final rx.Scheduler.Worker v1Worker;

WorkerV1ToWorkerV2(rx.Scheduler.Worker v1Worker) {
this.v1Worker = v1Worker;
}

@Override
public io.reactivex.disposables.Disposable schedule(Runnable action) {
final RunnableToV1Action0 runnable = new RunnableToV1Action0(action);
return RxJavaInterop.toV2Disposable(v1Worker.schedule(runnable));
}

@Override
public io.reactivex.disposables.Disposable schedule(Runnable action, long delayTime, TimeUnit unit) {
final RunnableToV1Action0 runnable = new RunnableToV1Action0(action);
return RxJavaInterop.toV2Disposable(v1Worker.schedule(runnable, delayTime, unit));
}

@Override
public io.reactivex.disposables.Disposable schedulePeriodically(Runnable action, long initialDelay, long period, TimeUnit unit) {
final RunnableToV1Action0 runnable = new RunnableToV1Action0(action);
return RxJavaInterop.toV2Disposable(v1Worker.schedulePeriodically(runnable, initialDelay, period, unit));
}

@Override
public long now(TimeUnit unit) {
return unit.convert(v1Worker.now(), TimeUnit.MILLISECONDS);
}

@Override
public void dispose() {
v1Worker.unsubscribe();
}

@Override
public boolean isDisposed() {
return v1Worker.isUnsubscribed();
}
}

static final class RunnableToV1Action0 implements rx.functions.Action0 {

final Runnable source;

RunnableToV1Action0(Runnable source) {
io.reactivex.internal.functions.ObjectHelper.requireNonNull(source, "Source 2.x Runnable is null");
this.source = source;
}

@Override
public void call() {
source.run();
}
}
}
Loading

0 comments on commit 1961f60

Please sign in to comment.