1
1
/**
2
2
* Copyright 2013 Netflix, Inc.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
*/
16
16
package rx .lang .scala
17
17
18
-
19
- import java .{lang => jlang }
20
- import rx .util .functions .Action0
21
- import rx .util .functions .Action1
22
- import rx .util .functions .Func0
23
- import rx .util .functions .Func1
24
- import rx .util .functions .Func2
25
- import rx .util .functions .Func3
26
- import rx .util .functions .Func4
27
- import java .{lang => jlang }
18
+ import java .{ lang => jlang }
19
+ import rx .util .functions ._
28
20
29
21
/**
30
- * These function conversions are only used by the ScalaAdapter, users of RxScala don't need them.
22
+ * These function conversions convert between Scala functions and Rx Funcs and Actions.
23
+ * Most users RxScala won't need them, but they might be useful if one wants to use
24
+ * the rx.Observable directly instead of using rx.lang.scala.Observable or if one wants
25
+ * to use a Java library taking/returning Funcs and Actions.
31
26
*/
32
27
object ImplicitFunctionConversions {
33
- // code below is copied from
34
- // https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/RxImplicits.scala
35
-
36
- import java .{ lang => jlang }
37
- import language .implicitConversions
38
-
39
- import rx .observables .BlockingObservable
40
- import rx .util .functions ._
41
- import rx .{Observer , Subscription }
42
-
43
- implicit def scalaFunction1ToOnSubscribeFunc [T ](f : rx.lang.scala.Observer [T ] => Subscription ) =
44
- new rx.Observable .OnSubscribeFunc [T ] {
45
- def onSubscribe (obs : Observer [_ >: T ]): Subscription = {
46
- f(obs)
47
- }
48
- }
49
-
50
- /* implicit def scalaFunction1ToOnSubscribeFunc[T](f: Observer[_ >: T] => Subscription) =
51
- new rx.Observable.OnSubscribeFunc[T] {
52
- def onSubscribe(obs: Observer[_ >: T]): Subscription = {
53
- f(obs)
54
- }
55
- }*/
56
-
57
- /**
58
- * Converts a by-name parameter to a Rx Func0
59
- */
60
- implicit def scalaByNameParamToFunc0 [B ](param : => B ): Func0 [B ] =
61
- new Func0 [B ]{
62
- def call (): B = param
63
- }
64
-
65
-
66
- /**
67
- * Converts 0-arg function to Rx Action0
68
- */
69
- implicit def scalaFunction0ProducingUnitToAction0 (f : (() => Unit )): Action0 =
70
- new Action0 {
71
- def call (): Unit = f()
72
- }
73
-
74
- /**
75
- * Converts 1-arg function to Rx Action1
76
- */
77
- implicit def scalaFunction1ProducingUnitToAction1 [A ](f : (A => Unit )): Action1 [A ] =
78
- new Action1 [A ] {
79
- def call (a : A ): Unit = f(a)
80
- }
81
-
82
- /**
83
- * Converts 1-arg predicate to Rx Func1[A, java.lang.Boolean]
84
- */
85
- implicit def scalaBooleanFunction1ToRxBooleanFunc1 [A ](f : (A => Boolean )): Func1 [A , jlang.Boolean ] =
86
- new Func1 [A , jlang.Boolean ] {
87
- def call (a : A ): jlang.Boolean = f(a).booleanValue
88
- }
89
-
90
- /**
91
- * Converts 2-arg predicate to Rx Func2[A, B, java.lang.Boolean]
92
- */
93
- implicit def scalaBooleanFunction2ToRxBooleanFunc1 [A , B ](f : ((A , B ) => Boolean )): Func2 [A , B , jlang.Boolean ] =
94
- new Func2 [A , B , jlang.Boolean ] {
95
- def call (a : A , b : B ): jlang.Boolean = f(a, b).booleanValue
96
- }
97
-
98
- /**
99
- * Converts a specific function shape (used in takeWhile) to the equivalent Java types with an Rx Func2
100
- */
101
- implicit def convertTakeWhileFuncToRxFunc2 [A ](f : (A , Int ) => Boolean ): Func2 [A , jlang.Integer , jlang.Boolean ] =
102
- new Func2 [A , jlang.Integer , jlang.Boolean ] {
103
- def call (a : A , b : jlang.Integer ): jlang.Boolean = f(a, b).booleanValue
104
- }
105
-
106
- /**
107
- * Converts a function shaped ilke compareTo into the equivalent Rx Func2
108
- */
109
- implicit def convertComparisonFuncToRxFunc2 [A ](f : (A , A ) => Int ): Func2 [A , A , jlang.Integer ] =
110
- new Func2 [A , A , jlang.Integer ] {
111
- def call (a1 : A , a2 : A ): jlang.Integer = f(a1, a2).intValue
112
- }
113
-
114
- /*
115
- * This implicit allows Scala code to use any exception type and still work
116
- * with invariant Func1 interface
117
- */
118
- implicit def exceptionFunction1ToRxExceptionFunc1 [A <: Exception , B ](f : (A => B )): Func1 [Exception , B ] =
119
- new Func1 [Exception , B ] {
120
- def call (ex : Exception ): B = f(ex.asInstanceOf [A ])
121
- }
122
-
123
- /**
124
- * The following implicits convert functions of different arities into the Rx equivalents
125
- */
126
- implicit def scalaFunction0ToRxFunc0 [A ](f : () => A ): Func0 [A ] =
127
- new Func0 [A ] {
128
- def call (): A = f()
129
- }
130
-
131
- implicit def scalaFunction1ToRxFunc1 [A , B ](f : (A => B )): Func1 [A , B ] =
132
- new Func1 [A , B ] {
133
- def call (a : A ): B = f(a)
134
- }
135
-
136
- implicit def scalaFunction2ToRxFunc2 [A , B , C ](f : (A , B ) => C ): Func2 [A , B , C ] =
137
- new Func2 [A , B , C ] {
138
- def call (a : A , b : B ) = f(a, b)
139
- }
140
-
141
- implicit def scalaFunction3ToRxFunc3 [A , B , C , D ](f : (A , B , C ) => D ): Func3 [A , B , C , D ] =
142
- new Func3 [A , B , C , D ] {
143
- def call (a : A , b : B , c : C ) = f(a, b, c)
144
- }
145
-
146
- implicit def scalaFunction4ToRxFunc4 [A , B , C , D , E ](f : (A , B , C , D ) => E ): Func4 [A , B , C , D , E ] =
147
- new Func4 [A , B , C , D , E ] {
148
- def call (a : A , b : B , c : C , d : D ) = f(a, b, c, d)
149
- }
150
-
151
- }
28
+ import language .implicitConversions
29
+
30
+ implicit def scalaFunction1ToOnSubscribeFunc [T ](f : rx.lang.scala.Observer [T ] => Subscription ) =
31
+ new rx.Observable .OnSubscribeFunc [T ] {
32
+ def onSubscribe (obs : Observer [_ >: T ]): Subscription = {
33
+ f(obs)
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Converts a by-name parameter to a Rx Func0
39
+ */
40
+ implicit def scalaByNameParamToFunc0 [B ](param : => B ): Func0 [B ] =
41
+ new Func0 [B ] {
42
+ def call (): B = param
43
+ }
44
+
45
+ /**
46
+ * Converts 0-arg function to Rx Action0
47
+ */
48
+ implicit def scalaFunction0ProducingUnitToAction0 (f : (() => Unit )): Action0 =
49
+ new Action0 {
50
+ def call (): Unit = f()
51
+ }
52
+
53
+ /**
54
+ * Converts 1-arg function to Rx Action1
55
+ */
56
+ implicit def scalaFunction1ProducingUnitToAction1 [A ](f : (A => Unit )): Action1 [A ] =
57
+ new Action1 [A ] {
58
+ def call (a : A ): Unit = f(a)
59
+ }
60
+
61
+ /**
62
+ * Converts 1-arg predicate to Rx Func1[A, java.lang.Boolean]
63
+ */
64
+ implicit def scalaBooleanFunction1ToRxBooleanFunc1 [A ](f : (A => Boolean )): Func1 [A , jlang.Boolean ] =
65
+ new Func1 [A , jlang.Boolean ] {
66
+ def call (a : A ): jlang.Boolean = f(a).booleanValue
67
+ }
68
+
69
+ /**
70
+ * Converts 2-arg predicate to Rx Func2[A, B, java.lang.Boolean]
71
+ */
72
+ implicit def scalaBooleanFunction2ToRxBooleanFunc1 [A , B ](f : ((A , B ) => Boolean )): Func2 [A , B , jlang.Boolean ] =
73
+ new Func2 [A , B , jlang.Boolean ] {
74
+ def call (a : A , b : B ): jlang.Boolean = f(a, b).booleanValue
75
+ }
76
+
77
+ /**
78
+ * Converts a specific function shape (used in takeWhile) to the equivalent Java types with an Rx Func2
79
+ */
80
+ implicit def convertTakeWhileFuncToRxFunc2 [A ](f : (A , Int ) => Boolean ): Func2 [A , jlang.Integer , jlang.Boolean ] =
81
+ new Func2 [A , jlang.Integer , jlang.Boolean ] {
82
+ def call (a : A , b : jlang.Integer ): jlang.Boolean = f(a, b).booleanValue
83
+ }
84
+
85
+ /**
86
+ * Converts a function shaped ilke compareTo into the equivalent Rx Func2
87
+ */
88
+ implicit def convertComparisonFuncToRxFunc2 [A ](f : (A , A ) => Int ): Func2 [A , A , jlang.Integer ] =
89
+ new Func2 [A , A , jlang.Integer ] {
90
+ def call (a1 : A , a2 : A ): jlang.Integer = f(a1, a2).intValue
91
+ }
92
+
93
+ /**
94
+ * This implicit allows Scala code to use any exception type and still work
95
+ * with invariant Func1 interface
96
+ */
97
+ implicit def exceptionFunction1ToRxExceptionFunc1 [A <: Exception , B ](f : (A => B )): Func1 [Exception , B ] =
98
+ new Func1 [Exception , B ] {
99
+ def call (ex : Exception ): B = f(ex.asInstanceOf [A ])
100
+ }
101
+
102
+ /**
103
+ * The following implicits convert functions of different arities into the Rx equivalents
104
+ */
105
+ implicit def scalaFunction0ToRxFunc0 [A ](f : () => A ): Func0 [A ] =
106
+ new Func0 [A ] {
107
+ def call (): A = f()
108
+ }
109
+
110
+ implicit def scalaFunction1ToRxFunc1 [A , B ](f : (A => B )): Func1 [A , B ] =
111
+ new Func1 [A , B ] {
112
+ def call (a : A ): B = f(a)
113
+ }
114
+
115
+ implicit def scalaFunction2ToRxFunc2 [A , B , C ](f : (A , B ) => C ): Func2 [A , B , C ] =
116
+ new Func2 [A , B , C ] {
117
+ def call (a : A , b : B ) = f(a, b)
118
+ }
119
+
120
+ implicit def scalaFunction3ToRxFunc3 [A , B , C , D ](f : (A , B , C ) => D ): Func3 [A , B , C , D ] =
121
+ new Func3 [A , B , C , D ] {
122
+ def call (a : A , b : B , c : C ) = f(a, b, c)
123
+ }
124
+
125
+ implicit def scalaFunction4ToRxFunc4 [A , B , C , D , E ](f : (A , B , C , D ) => E ): Func4 [A , B , C , D , E ] =
126
+ new Func4 [A , B , C , D , E ] {
127
+ def call (a : A , b : B , c : C , d : D ) = f(a, b, c, d)
128
+ }
129
+ }
0 commit comments