Skip to content

Commit 62564d6

Browse files
Scala Bindings Refactor
1 parent 30b6b08 commit 62564d6

34 files changed

+1626
-1709
lines changed

language-adaptors/rxjava-scala/src/examples/java/rx/lang/scala/examples/MovieLibUsage.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@
1515
*/
1616
package rx.lang.scala.examples;
1717

18-
import org.junit.Test;
19-
2018
import rx.Observable;
21-
import rx.util.functions.Action1;
19+
import rx.lang.scala.examples.Movie;
20+
import rx.lang.scala.examples.MovieLib;
2221

22+
import static rx.lang.scala.ImplicitFunctionConversions.toScalaObservable;
2323

2424
public class MovieLibUsage {
25-
26-
Action1<Movie> moviePrinter = new Action1<Movie>() {
27-
public void call(Movie m) {
28-
System.out.println("A movie of length " + m.lengthInSeconds() + "s");
29-
}
30-
};
31-
32-
@Test
33-
public void test() {
34-
MovieLib lib = new MovieLib(Observable.from(new Movie(3000), new Movie(1000), new Movie(2000)));
35-
36-
lib.longMovies().subscribe(moviePrinter);
37-
}
25+
26+
public static void main(String[] args) {
27+
28+
Observable<Movie> movies = Observable.from(
29+
new Movie(3000),
30+
new Movie(1000),
31+
new Movie(2000)
32+
);
33+
34+
MovieLib lib = new MovieLib(toScalaObservable(movies));
35+
36+
lib.longMovies().asJavaObservable().subscribe(m ->
37+
System.out.println("A movie of length " + m.lengthInSeconds() + "s")
38+
);
39+
}
3840

3941
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2013 Netflix, Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,11 +20,11 @@ import rx.lang.scala.Observable
2020
class Movie(val lengthInSeconds: Int) { }
2121

2222
class MovieLib(val moviesStream: Observable[Movie]) {
23-
23+
2424
val threshold = 1200
25-
25+
2626
def shortMovies: Observable[Movie] = moviesStream.filter(_.lengthInSeconds <= threshold)
27-
27+
2828
def longMovies: Observable[Movie] = moviesStream.filter(_.lengthInSeconds > threshold)
2929

3030
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.concurrent.duration._
2020

2121
object Olympics {
2222
case class Medal(val year: Int, val games: String, val discipline: String, val medal: String, val athlete: String, val country: String)
23-
23+
2424
def mountainBikeMedals: Observable[Medal] = Observable(
2525
Observable(
2626
Medal(1996, "Atlanta 1996", "cross-country men", "Gold", "Bart BRENTJENS", "Netherlands"),
@@ -31,7 +31,7 @@ object Olympics {
3131
Medal(1996, "Atlanta 1996", "cross-country women", "Bronze", "Susan DEMATTEI", "United States of America")
3232
),
3333
fourYearsEmpty,
34-
Observable(
34+
Observable(
3535
Medal(2000, "Sydney 2000", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
3636
Medal(2000, "Sydney 2000", "cross-country women", "Silver", "Barbara BLATTER", "Switzerland"),
3737
Medal(2000, "Sydney 2000", "cross-country women", "Bronze", "Marga FULLANA", "Spain"),
@@ -40,7 +40,7 @@ object Olympics {
4040
Medal(2000, "Sydney 2000", "cross-country men", "Bronze", "Christoph SAUSER", "Switzerland")
4141
),
4242
fourYearsEmpty,
43-
Observable(
43+
Observable(
4444
Medal(2004, "Athens 2004", "cross-country men", "Gold", "Julien ABSALON", "France"),
4545
Medal(2004, "Athens 2004", "cross-country men", "Silver", "Jose Antonio HERMIDA RAMOS", "Spain"),
4646
Medal(2004, "Athens 2004", "cross-country men", "Bronze", "Bart BRENTJENS", "Netherlands"),
@@ -49,7 +49,7 @@ object Olympics {
4949
Medal(2004, "Athens 2004", "cross-country women", "Bronze", "Sabine SPITZ", "Germany")
5050
),
5151
fourYearsEmpty,
52-
Observable(
52+
Observable(
5353
Medal(2008, "Beijing 2008", "cross-country women", "Gold", "Sabine SPITZ", "Germany"),
5454
Medal(2008, "Beijing 2008", "cross-country women", "Silver", "Maja WLOSZCZOWSKA", "Poland"),
5555
Medal(2008, "Beijing 2008", "cross-country women", "Bronze", "Irina KALENTYEVA", "Russian Federation"),
@@ -67,12 +67,12 @@ object Olympics {
6767
Medal(2012, "London 2012", "cross-country women", "Bronze", "Georgia GOULD", "United States of America")
6868
)
6969
).concat
70-
70+
7171
// speed it up :D
7272
val fourYears = 4000.millis
73-
73+
7474
val neverUsedDummyMedal = Medal(3333, "?", "?", "?", "?", "?")
75-
75+
7676
def fourYearsEmpty: Observable[Medal] = {
7777
// TODO this should return an observable which emits nothing during fourYears and then completes
7878
// Because of https://github.com/Netflix/RxJava/issues/388, we get non-terminating tests
@@ -82,5 +82,5 @@ object Olympics {
8282
// But we just return empty, which completes immediately
8383
Observable()
8484
}
85-
85+
8686
}
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package rx.lang.scala
1717

18+
import java.lang.Exception
1819
import java.{ lang => jlang }
20+
import rx.lang.scala._
1921
import rx.util.functions._
22+
import scala.collection.Seq
23+
import rx.lang.scala.subscriptions.Subscription
2024

2125
/**
2226
* These function conversions convert between Scala functions and Rx `Func`s and `Action`s.
@@ -28,20 +32,28 @@ object ImplicitFunctionConversions {
2832
import language.implicitConversions
2933

3034
implicit def schedulerActionToFunc2[T](action: (Scheduler, T) => Subscription) =
31-
new Func2[rx.Scheduler, T, Subscription] {
32-
def call(s: rx.Scheduler, t: T): Subscription = {
33-
action(s, t)
35+
new Func2[rx.Scheduler, T, rx.Subscription] {
36+
def call(s: rx.Scheduler, t: T): rx.Subscription = {
37+
action(s, t).asJavaSubscription
3438
}
35-
}
36-
37-
implicit def scalaSchedulerToJavaScheduler(s: Scheduler): rx.Scheduler = s.asJava
38-
39+
}
40+
41+
implicit def toJavaSubscription(s: Subscription): rx.Subscription = s.asJavaSubscription
42+
implicit def toScalaSubscription(s: rx.Subscription): Subscription = Subscription(s)
43+
44+
implicit def scalaSchedulerToJavaScheduler(s: Scheduler): rx.Scheduler = s.asJavaScheduler
3945
implicit def javaSchedulerToScalaScheduler(s: rx.Scheduler): Scheduler = Scheduler(s)
46+
47+
implicit def toJavaObserver[T](s: Observer[T]): rx.Observer[_ >: T] = s.asJavaObserver
48+
implicit def toScalaObserver[T](s: rx.Observer[T]): Observer[T] = Observer(s)
49+
50+
implicit def toJavaObservable[T](s: Observable[T]): rx.Observable[_ <: T] = s.asJavaObservable
51+
implicit def toScalaObservable[T](s: rx.Observable[T]): Observable[T] = Observable(s)
4052

4153
implicit def scalaFunction1ToOnSubscribeFunc[T](f: rx.lang.scala.Observer[T] => Subscription) =
4254
new rx.Observable.OnSubscribeFunc[T] {
4355
def onSubscribe(obs: rx.Observer[_ >: T]): rx.Subscription = {
44-
f(obs)
56+
f(Observer(obs))
4557
}
4658
}
4759

@@ -55,6 +67,10 @@ object ImplicitFunctionConversions {
5567
def call(): Unit = f()
5668
}
5769

70+
implicit def Action1toScalaFunction1ProducingUnit[A](f: Action1[A]): (A=>Unit) = {
71+
a => f(a)
72+
}
73+
5874
implicit def scalaFunction1ProducingUnitToAction1[A](f: (A => Unit)): Action1[A] =
5975
new Action1[A] {
6076
def call(a: A): Unit = f(a)

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Notification.scala renamed to language-adaptors/rxjava-scala/src/main/scala/Notification.scala

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
/**
2-
* Copyright 2013 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-
*/
161
package rx.lang.scala
172

183
/**
19-
* Emitted by Observables returned by [[Observable.materialize]].
4+
* Emitted by Observables returned by [[rx.lang.scala.Observable.materialize]].
205
*/
216
sealed trait Notification[+T] {
227
def asJava: rx.Notification[_ <: T]
@@ -58,7 +43,7 @@ object Notification {
5843
}
5944

6045
class OnError[+T](val asJava: rx.Notification[_ <: T]) extends Notification[T] {
61-
def error: Throwable = asJava.getThrowable()
46+
def error: Throwable = asJava.getThrowable
6247
}
6348

6449
object OnError {

0 commit comments

Comments
 (0)