Created
March 22, 2017 17:37
-
-
Save thomasnield/66eeef3ca4415f7c63d5d2065a1dc225 to your computer and use it in GitHub Desktop.
Kotlin Extensions for RxJava2 and Google Guava Immutable Collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.common.collect.ImmutableList | |
| import com.google.common.collect.ImmutableListMultimap | |
| import com.google.common.collect.ImmutableMap | |
| import com.google.common.collect.ImmutableSet | |
| import io.reactivex.Flowable | |
| import io.reactivex.Observable | |
| fun <T> Observable<T>.toImmutableList() = | |
| collect( { ImmutableList.builder<T>() }, { b,t -> b.add(t) }) | |
| .map { it.build() } | |
| fun <T> Observable<T>.toImmutableSet() = | |
| collect( { ImmutableSet.builder<T>() }, { b,t -> b.add(t) }) | |
| .map { it.build() } | |
| fun <T,K> Observable<T>.toImmutableMap(keyMapper: (T) -> K) = | |
| collect( { ImmutableMap.builder<K,T>() }, { b,t -> b.put(keyMapper(t), t) }) | |
| .map { it.build() } | |
| fun <T,K,V> Observable<T>.toImmutableMap(keyMapper: (T) -> K, valueMapper: (T) -> V) = | |
| collect( { ImmutableMap.builder<K,V>() }, { b,t -> b.put(keyMapper(t), valueMapper(t)) }) | |
| .map { it.build() } | |
| fun <T,K> Observable<T>.toImmutableListMultimap(keyMapper: (T) -> K) = | |
| collect( { ImmutableListMultimap.builder<K,T>() }, { b,t -> b.put(keyMapper(t), t) }) | |
| .map { it.build() } | |
| fun <T,K,V> Observable<T>.toImmutableListMultimap(keyMapper: (T) -> K, valueMapper: (T) -> V) = | |
| collect( { ImmutableListMultimap.builder<K,V>() }, { b,t -> b.put(keyMapper(t), valueMapper(t)) }) | |
| .map { it.build() } | |
| fun <T> Flowable<T>.toImmutableList() = | |
| collect( { ImmutableList.builder<T>() }, { b,t -> b.add(t) }) | |
| .map { it.build() } | |
| fun <T> Flowable<T>.toImmutableSet() = | |
| collect( { ImmutableSet.builder<T>() }, { b,t -> b.add(t) }) | |
| .map { it.build() } | |
| fun <T,K> Flowable<T>.toImmutableMap(keyMapper: (T) -> K) = | |
| collect( { ImmutableMap.builder<K,T>() }, { b,t -> b.put(keyMapper(t), t) }) | |
| .map { it.build() } | |
| fun <T,K,V> Flowable<T>.toImmutableMap(keyMapper: (T) -> K, valueMapper: (T) -> V) = | |
| collect( { ImmutableMap.builder<K,V>() }, { b,t -> b.put(keyMapper(t), valueMapper(t)) }) | |
| .map { it.build() } | |
| fun <T,K> Flowable<T>.toImmutableListMultimap(keyMapper: (T) -> K) = | |
| collect( { ImmutableListMultimap.builder<K,T>() }, { b,t -> b.put(keyMapper(t), t) }) | |
| .map { it.build() } | |
| fun <T,K,V> Flowable<T>.toImmutableListMultimap(keyMapper: (T) -> K, valueMapper: (T) -> V) = | |
| collect( { ImmutableListMultimap.builder<K,V>() }, { b,t -> b.put(keyMapper(t), valueMapper(t)) }) | |
| .map { it.build() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment