Skip to content

Commit 31cfd19

Browse files
committed
add toMap from Java Observable
1 parent 2180f39 commit 31cfd19

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,7 @@ trait Observable[+T]
23532353
*
23542354
* @param index
23552355
* the zero-based index of the item to retrieve
2356-
* @param defaultValue
2356+
* @param default
23572357
* the default item
23582358
* @return an Observable that emits the item at the specified position in the sequence emitted by the source
23592359
* Observable, or the default item if that index is outside the bounds of the source sequence
@@ -2364,6 +2364,65 @@ trait Observable[+T]
23642364
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[U]]
23652365
toScalaObservable[U](thisJava.elementAtOrDefault(index, default))
23662366
}
2367+
2368+
/**
2369+
* Return an Observable that emits a single Map containing all items emitted by the source Observable,
2370+
* mapped by the keys returned by a specified {@code keySelector} function.
2371+
* <p>
2372+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2373+
* <p>
2374+
* If more than one source item maps to the same key, the Map will contain the latest of those items.
2375+
*
2376+
* @param keySelector
2377+
* the function that extracts the key from a source item to be used in the Map
2378+
* @return an Observable that emits a single item: a Map containing the mapped items from the source
2379+
* Observable
2380+
*/
2381+
2382+
def toMap[K] (keySelector: T => K): Observable[Map[K, T]]= {
2383+
toScalaObservable[Map[K,T]](asJavaObservable.toMap(keySelector))
2384+
}
2385+
2386+
/**
2387+
* Return an Observable that emits a single Map containing values corresponding to items emitted by the
2388+
* source Observable, mapped by the keys returned by a specified {@code keySelector} function.
2389+
* <p>
2390+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2391+
* <p>
2392+
* If more than one source item maps to the same key, the Map will contain a single entry that
2393+
* corresponds to the latest of those items.
2394+
*
2395+
* @param keySelector
2396+
* the function that extracts the key from a source item to be used in the Map
2397+
* @param valueSelector
2398+
* the function that extracts the value from a source item to be used in the Map
2399+
* @return an Observable that emits a single item: a HashMap containing the mapped items from the source
2400+
* Observable
2401+
*/
2402+
2403+
def toMap[K, V] (keySelector: T => K, valueSelector: T => V) : Observable[Map[K, V]] = {
2404+
toScalaObservable[Map[K,V]](asJavaObservable.toMap(keySelector, valueSelector))
2405+
}
2406+
2407+
/**
2408+
* Return an Observable that emits a single Map, returned by a specified {@code mapFactory} function, that
2409+
* contains keys and values extracted from the items emitted by the source Observable.
2410+
* <p>
2411+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2412+
*
2413+
* @param keySelector
2414+
* the function that extracts the key from a source item to be used in the Map
2415+
* @param valueSelector
2416+
* the function that extracts the value from the source items to be used as value in the Map
2417+
* @param mapFactory
2418+
* the function that returns a Map instance to be used
2419+
* @return an Observable that emits a single item: a Map that contains the mapped items emitted by the
2420+
* source Observable
2421+
*/
2422+
def toMap[K, V] (keySelector: T => K, valueSelector: T => V, mapFactory: () => Observable[Map[K,V]]): Observable[Map[K,V]] = {
2423+
toScalaObservable[Map[K,V]](asJavaObservable.toMap(keySelector, valueSelector, mapFactory))
2424+
}
2425+
23672426
}
23682427

23692428
/**

0 commit comments

Comments
 (0)