Skip to content

Commit a5c0cd0

Browse files
inferno23akarnokd
authored andcommitted
Add the cast operator to Single. (#5332)
* Add the cast operator to Single. Signed-off-by: Mike Burns <[email protected]> * Create a separate operator for Single.cast and test it. Signed-off-by: Inferno23 <[email protected]> * Update spacing and javadoc. Signed-off-by: Inferno23 <[email protected]> * Add missing @experimental annotation. Signed-off-by: Inferno23 <[email protected]> * Fix the copyrights. Signed-off-by: Inferno23 <[email protected]> * Remove extra verify call. Signed-off-by: Inferno23 <[email protected]>
1 parent daf1022 commit a5c0cd0

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/main/java/rx/Single.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ private static <T> Observable<T> asObservable(Single<T> t) {
210210
* *********************************************************************************************************
211211
*/
212212

213+
/**
214+
* Casts the success value of the current Single into the target type or signals a
215+
* ClassCastException if not compatible.
216+
* <dl>
217+
* <dt><b>Scheduler:</b></dt>
218+
* <dd>{@code cast} does not operate by default on a particular {@link Scheduler}.</dd>
219+
* </dl>
220+
* @param <R> the target type
221+
* @param klass the type token to use for casting the success result from the current Single
222+
* @return the new Single instance
223+
* @since 1.3.1 - experimental
224+
*/
225+
@Experimental
226+
public final <R> Single<R> cast(final Class<R> klass) {
227+
return map(new SingleOperatorCast<T, R>(klass));
228+
}
229+
213230
/**
214231
* Returns an Observable that emits the items emitted by two Singles, one after the other.
215232
* <p>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.internal.operators;
17+
18+
import rx.functions.Func1;
19+
20+
/**
21+
* Converts the element of a Single to the specified type.
22+
* @param <T> the input value type
23+
* @param <R> the output value type
24+
*/
25+
public class SingleOperatorCast<T, R> implements Func1<T, R> {
26+
27+
final Class<R> castClass;
28+
29+
public SingleOperatorCast(Class<R> castClass) {
30+
this.castClass = castClass;
31+
}
32+
33+
@Override
34+
public R call(T t) {
35+
return castClass.cast(t);
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.internal.operators;
17+
;
18+
import org.junit.Test;
19+
import rx.Observer;
20+
import rx.Single;
21+
22+
import static org.mockito.Mockito.*;
23+
24+
public class SingleOperatorCastTest {
25+
26+
@Test
27+
public void testSingleCast() {
28+
Single<?> source = Single.just(1);
29+
Single<Integer> single = source.cast(Integer.class);
30+
31+
@SuppressWarnings("unchecked")
32+
Observer<Integer> observer = mock(Observer.class);
33+
single.subscribe(observer);
34+
verify(observer, times(1)).onNext(1);
35+
verify(observer, never()).onError(
36+
org.mockito.Matchers.any(Throwable.class));
37+
verify(observer, times(1)).onCompleted();
38+
}
39+
40+
@Test
41+
public void testSingleCastWithWrongType() {
42+
Single<?> source = Single.just(1);
43+
Single<Boolean> single = source.cast(Boolean.class);
44+
45+
@SuppressWarnings("unchecked")
46+
Observer<Boolean> observer = mock(Observer.class);
47+
single.subscribe(observer);
48+
verify(observer, times(1)).onError(
49+
org.mockito.Matchers.any(ClassCastException.class));
50+
}
51+
}

0 commit comments

Comments
 (0)