Skip to content

Kotlin Language Adaptor #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions language-adaptors/rxjava-kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Kotlin Adaptor for RxJava

Kotlin has support for SAM (Single Abstract Method) Interfaces as Functions (i.e. Java 8 Lambdas). So you could use Kotlin in RxJava whitout this adaptor

```kotlin
Observable.create<String>{ observer ->
observer!!.onNext("Hello")
observer.onCompleted()
Subscriptions.empty()
}!!.subscribe { result ->
a!!.received(result)
}
```

This adaptor exposes a set of Extension functions that allow a more idiomatic Kotlin usage

```kotlin
import rx.lang.kotlin.*

{(observer: Observer<in String>) ->
observer.onNext("Hello")
observer.onCompleted()
Subscriptions.empty()!!
}.asObservable().subscribe { result ->
a!!.received(result)
}
```

## Binaries

Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-kotlin%22).

Example for Maven:

```xml
<dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-kotlin</artifactId>
<version>x.y.z</version>
</dependency>
```

and for Ivy:

```xml
<dependency org="com.netflix.rxjava" name="rxjava-kotlin" rev="x.y.z" />
```

and for Gradle:

```groovy
compile 'com.netflix.rxjava:rxjava-kotlin:x.y.z'
```
29 changes: 29 additions & 0 deletions language-adaptors/rxjava-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
buildscript {
repositories() {
mavenCentral()
}

dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.800'
}
}

apply plugin: 'kotlin'
apply plugin: 'osgi'

dependencies {
compile project(':rxjava-core')
compile 'org.jetbrains.kotlin:kotlin-stdlib:0.6.800'
provided 'junit:junit-dep:4.10'
provided 'org.mockito:mockito-core:1.8.5'
}

jar {
manifest {
name = 'rxjava-kotlin'
instruction 'Bundle-Vendor', 'Netflix'
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava'
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*'
instruction 'Fragment-Host', 'com.netflix.rxjava.core'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rx.lang.kotlin

import rx.Subscription
import rx.Observer
import rx.Observable

public fun<T> Function1<Observer<in T>, Subscription>.asObservable(): Observable<T> {
return Observable.create { this(it!!) }!!
}

public fun<T> Function0<Observable<out T>>.defer(): Observable<T> {
return Observable.defer(this)!!
}

public fun<T> Iterable<T>.asObservable(): Observable<T> {
return Observable.from(this)!!
}

public fun<T> T.asObservable(): Observable<T> {
return Observable.from(this)!!
}

public fun<T> Throwable.asObservable(): Observable<T> {
return Observable.error(this)!!
}

public fun<T> Pair<T, T>.asObservable(): Observable<T> {
return Observable.from(this.component1(), this.component2())!!
}

public fun<T> Triple<T, T, T>.asObservable(): Observable<T> {
return Observable.from(this.component1(), this.component2(), this.component3())!!
}

public fun<T> Pair<Observable<T>, Observable<T>>.merge(): Observable<T> {
return Observable.merge(this.component1(), this.component2())!!
}

public fun<T> Triple<Observable<T>, Observable<T>, Observable<T>>.merge(): Observable<T> {
return Observable.merge(this.component1(), this.component2(), this.component3())!!
}

public fun<T> Pair<Observable<T>, Observable<T>>.mergeDelayError(): Observable<T> {
return Observable.mergeDelayError(this.component1(), this.component2())!!
}

public fun<T> Triple<Observable<T>, Observable<T>, Observable<T>>.mergeDelayError(): Observable<T> {
return Observable.mergeDelayError(this.component1(), this.component2(), this.component3())!!
}
Loading