-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RxJava2 extensions for Observable. (#2)
- Loading branch information
1 parent
a0867dd
commit 3a98d92
Showing
11 changed files
with
283 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
ext.versions = [ | ||
kotlin : '1.1.1', | ||
kotlin : '1.1.1', | ||
rxJava2 : '2.1.0', | ||
|
||
junit : '4.12', | ||
junitPlatform : '1.0.0-M4', | ||
spek : '1.1.2', | ||
assertJ : '3.5.2', | ||
junit : '4.12', | ||
junitPlatform: '1.0.0-M4', | ||
spek : '1.1.2', | ||
assertJ : '3.5.2', | ||
] | ||
|
||
ext.libraries = [ | ||
kotlinStd : "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin", | ||
kotlinRuntime : "org.jetbrains.kotlin:kotlin-runtime:$versions.kotlin", | ||
kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin", | ||
koptional : project(':koptional'), | ||
koptionalRxJava2Extensions: project(':koptional-rxjava2-extensions'), | ||
|
||
junit : "junit:junit:$versions.junit", | ||
spek : "org.jetbrains.spek:spek-api:$versions.spek", | ||
spekJunitPlatformEngine: "org.jetbrains.spek:spek-junit-platform-engine:$versions.spek", | ||
assertJ : "org.assertj:assertj-core:$versions.assertJ", | ||
kotlinStd : "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin", | ||
kotlinRuntime : "org.jetbrains.kotlin:kotlin-runtime:$versions.kotlin", | ||
kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin", | ||
rxJava2 : "io.reactivex.rxjava2:rxjava:$versions.rxJava2", | ||
|
||
|
||
junit : "junit:junit:$versions.junit", | ||
spek : "org.jetbrains.spek:spek-api:$versions.spek", | ||
spekJunitPlatformEngine : "org.jetbrains.spek:spek-junit-platform-engine:$versions.spek", | ||
assertJ : "org.assertj:assertj-core:$versions.assertJ", | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
apply plugin: 'maven' | ||
apply plugin: 'maven-publish' | ||
apply plugin: 'com.jfrog.bintray' | ||
|
||
def gitTag() { | ||
def tag = 'git tag --list --points-at HEAD'.execute((List) null, rootProject.projectDir).text.trim() | ||
|
||
if (tag.split(System.lineSeparator()).length > 1) { | ||
throw new IllegalStateException("gitTag is accessed but commit has multiple tags: $tag") | ||
} | ||
|
||
return tag | ||
} | ||
|
||
def projectVersion() { | ||
def tag = gitTag() | ||
|
||
if (tag.startsWith('v')) { | ||
return tag.substring(1) | ||
} else if (tag.isEmpty()) { | ||
return "development" | ||
} | ||
|
||
return tag | ||
} | ||
|
||
def validateTagAndVersion() { | ||
if (gitTag().isEmpty()) { | ||
throw new IllegalStateException('Publishing is not allowed because current commit has no tag') | ||
} | ||
|
||
if (projectVersion().isEmpty()) { | ||
throw new IllegalStateException('Publishing is not allowed because current projectVersion is empty') | ||
} | ||
} | ||
|
||
def pomConfig = { | ||
licenses { | ||
license { | ||
name 'The Apache Software License, Version 2.0' | ||
url 'http://www.apache.org/licenses/LICENSE-2.0.txt' | ||
distribution 'repo' | ||
} | ||
} | ||
developers { | ||
developer { | ||
id 'gojuno' | ||
name 'Juno Inc.' | ||
email '[email protected]' | ||
} | ||
} | ||
} | ||
|
||
|
||
task sourcesJar(type: Jar, dependsOn: classes) { | ||
classifier = 'sources' | ||
from sourceSets.main.allSource | ||
} | ||
|
||
task javadocJar(type: Jar, dependsOn: javadoc) { | ||
classifier = 'javadoc' | ||
from javadoc.destinationDir | ||
} | ||
|
||
task validatePublishing { | ||
doLast { | ||
validateTagAndVersion() | ||
} | ||
} | ||
|
||
bintrayUpload.dependsOn validatePublishing | ||
|
||
publishing { | ||
publications { | ||
KoptionalPublication(MavenPublication) { | ||
from components.java | ||
|
||
artifact sourcesJar | ||
artifact javadocJar | ||
|
||
groupId 'com.gojuno.koptional' | ||
artifactId project.name | ||
version projectVersion() | ||
|
||
pom.withXml { | ||
def root = asNode() | ||
root.appendNode('description', project.description) | ||
root.appendNode('name', project.name) | ||
root.appendNode('url', 'https://github.com/gojuno/koptional') | ||
root.children().last() + pomConfig | ||
} | ||
} | ||
} | ||
} | ||
|
||
bintray { | ||
user = System.getenv('BINTRAY_USER') | ||
key = System.getenv('BINTRAY_API_KEY') | ||
publish = true | ||
|
||
pkg { | ||
repo = 'maven' | ||
name = 'koptional' | ||
licenses = ['Apache-2.0'] | ||
vcsUrl = 'https://github.com/gojuno/koptional.git' | ||
issueTrackerUrl = 'https://github.com/gojuno/koptional/issues' | ||
publications = ['KoptionalPublication'] | ||
|
||
version { | ||
name = projectVersion() | ||
vcsTag = gitTag() | ||
|
||
gpg { | ||
sign = true | ||
passphrase = System.getenv('BINTRAY_GPG_PASSPHRASE') | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apply plugin: 'kotlin' | ||
apply plugin: 'org.junit.platform.gradle.plugin' | ||
apply from: '../gradle/publishing.gradle' | ||
|
||
dependencies { | ||
compile libraries.koptional | ||
compile libraries.rxJava2 | ||
} | ||
|
||
dependencies { | ||
testCompile libraries.spek | ||
testCompile libraries.spekJunitPlatformEngine | ||
testCompile libraries.assertJ | ||
} | ||
|
||
junitPlatform { | ||
platformVersion = versions.junitPlatform | ||
|
||
filters { | ||
engines { | ||
include 'spek' | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
description=Extensions to work with Optional in RxJava2 streams. |
36 changes: 36 additions & 0 deletions
36
koptional-rxjava2-extensions/src/main/kotlin/com/gojuno/koptional/rxjava2/rxjava2.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.gojuno.koptional.rxjava2 | ||
|
||
import com.gojuno.koptional.None | ||
import com.gojuno.koptional.Optional | ||
import com.gojuno.koptional.Some | ||
import io.reactivex.Observable | ||
|
||
private inline fun <reified R : Any> Observable<*>.ofType(): Observable<R> = ofType(R::class.java) | ||
|
||
/** | ||
* Filters items emitted by an ObservableSource by only emitting those that are `Some`. | ||
* <p> | ||
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt=""> | ||
* <dl> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @return an Observable that emits `Some.value` of those items emitted by the source ObservableSource that are `Some`. | ||
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a> | ||
*/ | ||
fun <T : Any> Observable<Optional<T>>.filterSome(): Observable<T> = ofType<Some<T>>().map { it.value } | ||
|
||
/** | ||
* Filters items emitted by an ObservableSource by only emitting those that are `None`. | ||
* <p> | ||
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt=""> | ||
* <dl> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @return an Observable that emits `Unit` for each item emitted by the source ObservableSource that is `None`. | ||
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a> | ||
*/ | ||
fun <T : Any> Observable<Optional<T>>.filterNone(): Observable<Unit> = ofType<None>().map { Unit } |
62 changes: 62 additions & 0 deletions
62
...-rxjava2-extensions/src/test/kotlin/com/gojuno/koptional/rxjava2/RxJava2ExtensionsSpec.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.gojuno.koptional.rxjava2 | ||
|
||
import com.gojuno.koptional.None | ||
import com.gojuno.koptional.Some | ||
import io.reactivex.Observable | ||
import io.reactivex.observers.TestObserver | ||
import org.jetbrains.spek.api.Spek | ||
import org.jetbrains.spek.api.dsl.context | ||
import org.jetbrains.spek.api.dsl.it | ||
|
||
class RxJava2ExtensionsSpec : Spek({ | ||
|
||
val stream by memoized { | ||
Observable.just(Some("a"), None, Some("b"), Some("c"), None) | ||
} | ||
|
||
context("filterSome") { | ||
|
||
val subscriber by memoized { | ||
TestObserver<String>().apply { | ||
stream | ||
.filterSome() | ||
.subscribe(this) | ||
} | ||
} | ||
|
||
it("passes only Some values") { | ||
subscriber.assertValues("a", "b", "c") | ||
} | ||
|
||
it("completes stream") { | ||
subscriber.assertComplete() | ||
} | ||
|
||
it("does not emit error") { | ||
subscriber.assertNoErrors() | ||
} | ||
} | ||
|
||
context("filterNone") { | ||
|
||
val subscriber by memoized { | ||
TestObserver<Unit>().apply { | ||
stream | ||
.filterNone() | ||
.subscribe(this) | ||
} | ||
} | ||
|
||
it("passes only None values as Unit") { | ||
subscriber.assertValues(Unit, Unit) | ||
} | ||
|
||
it("completes stream") { | ||
subscriber.assertComplete() | ||
} | ||
|
||
it("does not emit error") { | ||
subscriber.assertNoErrors() | ||
} | ||
} | ||
}) |
Oops, something went wrong.