Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x: Implement as() #5729

Merged
merged 17 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/io/reactivex/Completable.java
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,32 @@ public final Completable andThen(CompletableSource next) {
return concatWith(next);
}

/**
* Calls the specified converter function during assembly time and returns its resulting value.
* <p>
* This allows fluent conversion to any other type.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the resulting object type
* @param converter the function that receives the current Completable instance and returns a value
* @return the converted value
* @throws NullPointerException if converter is null
*/
@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> R as(@NonNull CompletableConverter<? extends R> converter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @Experimental and @since 2.1.7 - experimental to all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try {
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw ExceptionHelper.wrapOrThrow(ex);
}
}

/**
* Subscribes to and awaits the termination of this Completable instance in a blocking manner and
* rethrows any exception emitted.
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/reactivex/CompletableConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex;

import io.reactivex.annotations.*;

/**
* Convenience interface and callback used by the {@link Completable#as} operator to turn a Completable into another
* value fluently.
*
* @param <R> the output type
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @since 2.1.7 - experimental to these as well.

@Experimental
public interface CompletableConverter<R> {
/**
* Applies a function to the upstream Completable and returns a converted value of type {@code R}.
*
* @param upstream the upstream Completable instance
* @return the converted value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull Completable upstream) throws Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transformer variants don't define any throws Exception. I'd think we can live without them in these interfaces as well and thus no need to try-catch apply in the base classes' as() methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
29 changes: 29 additions & 0 deletions src/main/java/io/reactivex/Flowable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5237,6 +5237,35 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
return RxJavaPlugins.onAssembly(new FlowableAnySingle<T>(this, predicate));
}

/**
* Calls the specified converter function during assembly time and returns its resulting value.
* <p>
* This allows fluent conversion to any other type.
* <dl>
* <dt><b>Backpressure:</b></dt>
* <dd>The backpressure behavior depends on what happens in the {@code converter} function.</dd>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the resulting object type
* @param converter the function that receives the current Flowable instance and returns a value
* @return the converted value
* @throws NullPointerException if converter is null
*/
@Experimental
@CheckReturnValue
@BackpressureSupport(BackpressureKind.SPECIAL)
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> R as(@NonNull FlowableConverter<T, ? extends R> converter) {
try {
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw ExceptionHelper.wrapOrThrow(ex);
}
}

/**
* Returns the first item emitted by this {@code Flowable}, or throws
* {@code NoSuchElementException} if it emits no items.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/reactivex/FlowableConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex;

import io.reactivex.annotations.*;

/**
* Convenience interface and callback used by the {@link Flowable#as} operator to turn a Flowable into another
* value fluently.
*
* @param <T> the upstream type
* @param <R> the output type
*/
@Experimental
public interface FlowableConverter<T, R> {
/**
* Applies a function to the upstream Flowable and returns a converted value of type {@code R}.
*
* @param upstream the upstream Flowable instance
* @return the converted value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull Flowable<T> upstream) throws Exception;
}
26 changes: 26 additions & 0 deletions src/main/java/io/reactivex/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,32 @@ public final Maybe<T> ambWith(MaybeSource<? extends T> other) {
return ambArray(this, other);
}

/**
* Calls the specified converter function during assembly time and returns its resulting value.
* <p>
* This allows fluent conversion to any other type.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the resulting object type
* @param converter the function that receives the current Maybe instance and returns a value
* @return the converted value
* @throws NullPointerException if converter is null
*/
@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> R as(@NonNull MaybeConverter<T, ? extends R> converter) {
try {
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw ExceptionHelper.wrapOrThrow(ex);
}
}

/**
* Waits in a blocking fashion until the current Maybe signals a success value (which is returned),
* null if completed or an exception (which is propagated).
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/reactivex/MaybeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex;

import io.reactivex.annotations.*;

/**
* Convenience interface and callback used by the {@link Maybe#as} operator to turn a Maybe into another
* value fluently.
*
* @param <T> the upstream type
* @param <R> the output type
*/
@Experimental
public interface MaybeConverter<T, R> {
/**
* Applies a function to the upstream Maybe and returns a converted value of type {@code R}.
*
* @param upstream the upstream Maybe instance
* @return the converted value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull Maybe<T> upstream) throws Exception;
}
26 changes: 26 additions & 0 deletions src/main/java/io/reactivex/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4800,6 +4800,32 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
return RxJavaPlugins.onAssembly(new ObservableAnySingle<T>(this, predicate));
}

/**
* Calls the specified converter function during assembly time and returns its resulting value.
* <p>
* This allows fluent conversion to any other type.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the resulting object type
* @param converter the function that receives the current Observable instance and returns a value
* @return the converted value
* @throws NullPointerException if converter is null
*/
@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> R as(@NonNull ObservableConverter<T, ? extends R> converter) {
try {
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw ExceptionHelper.wrapOrThrow(ex);
}
}

/**
* Returns the first item emitted by this {@code Observable}, or throws
* {@code NoSuchElementException} if it emits no items.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/reactivex/ObservableConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex;

import io.reactivex.annotations.*;

/**
* Convenience interface and callback used by the {@link Observable#as} operator to turn an Observable into another
* value fluently.
*
* @param <T> the upstream type
* @param <R> the output type
*/
@Experimental
public interface ObservableConverter<T, R> {
/**
* Applies a function to the upstream Observable and returns a converted value of type {@code R}.
*
* @param upstream the upstream Observable instance
* @return the converted value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull Observable<T> upstream) throws Exception;
}
26 changes: 26 additions & 0 deletions src/main/java/io/reactivex/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,32 @@ public final Single<T> ambWith(SingleSource<? extends T> other) {
return ambArray(this, other);
}

/**
* Calls the specified converter function during assembly time and returns its resulting value.
* <p>
* This allows fluent conversion to any other type.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the resulting object type
* @param converter the function that receives the current Single instance and returns a value
* @return the converted value
* @throws NullPointerException if converter is null
*/
@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> R as(@NonNull SingleConverter<T, ? extends R> converter) {
try {
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw ExceptionHelper.wrapOrThrow(ex);
}
}

/**
* Hides the identity of the current Single, including the Disposable that is sent
* to the downstream via {@code onSubscribe()}.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/reactivex/SingleConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex;

import io.reactivex.annotations.*;

/**
* Convenience interface and callback used by the {@link Single#as} operator to turn a Single into another
* value fluently.
*
* @param <T> the upstream type
* @param <R> the output type
*/
@Experimental
public interface SingleConverter<T, R> {
/**
* Applies a function to the upstream Single and returns a converted value of type {@code R}.
*
* @param upstream the upstream Single instance
* @return the converted value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull Single<T> upstream) throws Exception;
}
32 changes: 32 additions & 0 deletions src/test/java/io/reactivex/completable/CompletableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2796,11 +2796,43 @@ public void accept(Object e) { }
});
}

@Test(timeout = 5000)
public void asNormal() {
Flowable<Object> flow = normal.completable.as(new CompletableConverter<Flowable<Object>>() {
@Override
public Flowable<Object> apply(Completable c) {
return c.toFlowable();
}
});

flow.blockingForEach(new Consumer<Object>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not assertion is this wanted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferring to @akarnokd on this as I was matching the analogous to() test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change these into proper assertions such as .test().assertResult().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would I assert for these since there's no value? Or just rewrite the rest in general?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there is the onComplete event.

Copy link
Contributor Author

@ZacSweers ZacSweers Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is but completable.normal doesn't complete. I wasn't sure if you wanted me to change it to just use Completable.complete() or something similar since the original test didn't seem to be testing completion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal.completable does complete otherwise blockingForEach would block indefinitely and the test would have failed a long a go. A .test().assertResult(/* no values */) should suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, I missed that line 🙄

Done in e0d793f

@Override
public void accept(Object e) { }
});
}

@Test
public void as() {
Completable.complete().as(new CompletableConverter<Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Completable v) throws Exception {
return v.toFlowable();
}
})
.test()
.assertComplete();
}

@Test(expected = NullPointerException.class)
public void toNull() {
normal.completable.to(null);
}

@Test(expected = NullPointerException.class)
public void asNull() {
normal.completable.as(null);
}

@Test(timeout = 5000)
public void toFlowableNormal() {
normal.completable.toFlowable().blockingForEach(Functions.emptyConsumer());
Expand Down
Loading