Skip to content

Commit 7a85774

Browse files
Merge pull request #1005 from suncelesta/scala-observable-update
add toMap from Java Observable
2 parents 9dd7648 + 16b0417 commit 7a85774

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,28 @@ class RxScalaDemo extends JUnitSuite {
573573
val o : Observable[Seq[Char]] = List("red".toList, "green".toList, "blue".toList).toObservable.elementAtOrDefault(3, "black".toSeq)
574574
println(o.toBlockingObservable.single)
575575
}
576+
577+
@Test def toMapExample1(): Unit = {
578+
val o : Observable[String] = List("alice", "bob", "carol").toObservable
579+
val keySelector = (s: String) => s.head
580+
val m = o.toMap(keySelector)
581+
println(m.toBlockingObservable.single)
582+
}
583+
584+
@Test def toMapExample2(): Unit = {
585+
val o : Observable[String] = List("alice", "bob", "carol").toObservable
586+
val keySelector = (s: String) => s.head
587+
val valueSelector = (s: String) => s.tail
588+
val m = o.toMap(keySelector, valueSelector)
589+
println(m.toBlockingObservable.single)
590+
}
591+
592+
@Test def toMapExample3(): Unit = {
593+
val o : Observable[String] = List("alice", "bob", "carol").toObservable
594+
val keySelector = (s: String) => s.head
595+
val valueSelector = (s: String) => s.tail
596+
val mapFactory = () => Map(('s',"tart"))
597+
val m = o.toMap(keySelector, valueSelector, mapFactory)
598+
println(m.toBlockingObservable.single)
599+
}
576600
}

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

Lines changed: 66 additions & 1 deletion
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
/**
@@ -2353,7 +2355,7 @@ trait Observable[+T]
23532355
*
23542356
* @param index
23552357
* the zero-based index of the item to retrieve
2356-
* @param defaultValue
2358+
* @param default
23572359
* the default item
23582360
* @return an Observable that emits the item at the specified position in the sequence emitted by the source
23592361
* Observable, or the default item if that index is outside the bounds of the source sequence
@@ -2364,6 +2366,69 @@ trait Observable[+T]
23642366
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[U]]
23652367
toScalaObservable[U](thisJava.elementAtOrDefault(index, default))
23662368
}
2369+
2370+
/**
2371+
* Return an Observable that emits a single Map containing all items emitted by the source Observable,
2372+
* mapped by the keys returned by a specified {@code keySelector} function.
2373+
* <p>
2374+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2375+
* <p>
2376+
* If more than one source item maps to the same key, the Map will contain the latest of those items.
2377+
*
2378+
* @param keySelector
2379+
* the function that extracts the key from a source item to be used in the Map
2380+
* @return an Observable that emits a single item: a Map containing the mapped items from the source
2381+
* Observable
2382+
*/
2383+
def toMap[K] (keySelector: T => K): Observable[Map[K, T]]= {
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)
2387+
}
2388+
2389+
/**
2390+
* Return an Observable that emits a single Map containing values corresponding to items emitted by the
2391+
* source Observable, mapped by the keys returned by a specified {@code keySelector} function.
2392+
* <p>
2393+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2394+
* <p>
2395+
* If more than one source item maps to the same key, the Map will contain a single entry that
2396+
* corresponds to the latest of those items.
2397+
*
2398+
* @param keySelector
2399+
* the function that extracts the key from a source item to be used in the Map
2400+
* @param valueSelector
2401+
* the function that extracts the value from a source item to be used in the Map
2402+
* @return an Observable that emits a single item: a HashMap containing the mapped items from the source
2403+
* Observable
2404+
*/
2405+
def toMap[K, V] (keySelector: T => K, valueSelector: T => V) : Observable[Map[K, V]] = {
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)
2409+
}
2410+
2411+
/**
2412+
* Return an Observable that emits a single Map, returned by a specified {@code mapFactory} function, that
2413+
* contains keys and values extracted from the items emitted by the source Observable.
2414+
* <p>
2415+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2416+
*
2417+
* @param keySelector
2418+
* the function that extracts the key from a source item to be used in the Map
2419+
* @param valueSelector
2420+
* the function that extracts the value from the source items to be used as value in the Map
2421+
* @param mapFactory
2422+
* the function that returns a Map instance to be used
2423+
* @return an Observable that emits a single item: a Map that contains the mapped items emitted by the
2424+
* source Observable
2425+
*/
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)
2430+
}
2431+
23672432
}
23682433

23692434
/**

0 commit comments

Comments
 (0)