Skip to content

Commit 16b0417

Browse files
committed
corrected implementation
Implementation corrected to not raise exceptions
1 parent 69e5ffd commit 16b0417

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class RxScalaDemo extends JUnitSuite {
593593
val o : Observable[String] = List("alice", "bob", "carol").toObservable
594594
val keySelector = (s: String) => s.head
595595
val valueSelector = (s: String) => s.tail
596-
val mapFactory = () => Map('s'->"tart")
596+
val mapFactory = () => Map(('s',"tart"))
597597
val m = o.toMap(keySelector, valueSelector, mapFactory)
598598
println(m.toBlockingObservable.single)
599599
}

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import rx.functions.FuncN
2020
import rx.Observable.OnSubscribeFunc
2121
import rx.lang.scala.observables.ConnectableObservable
2222
import scala.concurrent.duration
23+
import java.util
24+
import collection.JavaConversions._
2325

2426

2527
/**
@@ -2379,7 +2381,9 @@ trait Observable[+T]
23792381
* Observable
23802382
*/
23812383
def toMap[K] (keySelector: T => K): Observable[Map[K, T]]= {
2382-
toScalaObservable[Map[K,T]](asJavaObservable.toMap(keySelector))
2384+
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[T]]
2385+
val o: rx.Observable[util.Map[K, T]] = thisJava.toMap[K](keySelector)
2386+
toScalaObservable[util.Map[K,T]](o).map(m => m.toMap)
23832387
}
23842388

23852389
/**
@@ -2399,7 +2403,9 @@ trait Observable[+T]
23992403
* Observable
24002404
*/
24012405
def toMap[K, V] (keySelector: T => K, valueSelector: T => V) : Observable[Map[K, V]] = {
2402-
toScalaObservable[Map[K,V]](asJavaObservable.toMap(keySelector, valueSelector))
2406+
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[T]]
2407+
val o: rx.Observable[util.Map[K, V]] = thisJava.toMap[K, V](keySelector, valueSelector)
2408+
toScalaObservable[util.Map[K, V]](o).map(m => m.toMap)
24032409
}
24042410

24052411
/**
@@ -2417,8 +2423,10 @@ trait Observable[+T]
24172423
* @return an Observable that emits a single item: a Map that contains the mapped items emitted by the
24182424
* source Observable
24192425
*/
2420-
def toMap[K, V] (keySelector: T => K, valueSelector: T => V, mapFactory: () => Map[K,V]): Observable[Map[K,V]] = {
2421-
toScalaObservable[Map[K,V]](asJavaObservable.toMap(keySelector, valueSelector, mapFactory))
2426+
def toMap[K, V] (keySelector: T => K, valueSelector: T => V, mapFactory: () => Map[K, V]): Observable[Map[K, V]] = {
2427+
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[T]]
2428+
val o: rx.Observable[util.Map[K, V]] = thisJava.toMap[K, V](keySelector, valueSelector)
2429+
toScalaObservable[util.Map[K, V]](o).map(m => mapFactory() ++ m.toMap)
24222430
}
24232431

24242432
}

0 commit comments

Comments
 (0)