├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle ├── bintray.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main └── kotlin │ └── moe │ └── pine │ └── rx │ └── collections │ ├── Flowable.kt │ └── Observable.kt └── test └── kotlin └── moe └── pine └── rx └── collections ├── FlowableTest.kt └── ObservableTest.kt /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .gradle/ 3 | .idea/ 4 | *.iml 5 | local.properties 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | 5 | env: 6 | - GRADLE_OPTS="-Xmx512m -XX:MaxPermSize=512m" 7 | 8 | after_success: 9 | - bash <(curl -s https://codecov.io/bash) 10 | - ./gradlew coveralls 11 | 12 | notifications: 13 | slack: 14 | - secure: "aZ5D4vra4LlmFxbPc/fRQRiy7Y/P9vtBwTwbZIlBfuH17OX18Ez47jwsglnd2CsDXHBVCJ/LVYc81svgi04YW82r+sWYZ3EcPDARdSrQbERxmS1kN/ohDX5fPNa9b30zhvVqOjYm8JrxaES07Q30C2RdNu9SD5dVIbYPknZx9ofA0Lwnj+kkS0xsTNrv0MdSYVueijQYy3EE5FKUaivpRurHBqgYK89X9Yvu2FuXpTC71OEwSieFfdxJa8lHYGuw5oPtFAfZyw67BcckYusATY3rv3yThz5rhUjO7LW0UfeSk4Rb7PcwFy84xeP/a4I7+M25HWzMCWV5xb8v/w4Jo4/rZrZQrNrpZT9ORdW9XvB+PeuURTVC0CMOCooOOA/+g8Q3F6ZOR7MEZe6EutIFlD9BWKB1A92PXgSyVuLqr91YvPyAef2++eePxI9ebyUUiHG72ZI6HXz9LPjD/hI6m+9Bhw9ZGaZlcbnPQVfMESRwa32Gu0fcQZxGObGMoHBMVWyUNJmTr+0gjuPu4jFny1PGatICGQM6Pqk+jY8jFQIQKMO6vGIGli3diokaQocoFezQalVSMAlb2xJGgFW3k2IFCHsrUxmKKJQ9JF3Fin0vqI4vQEzx33N/3ShdQf+6Q5IXdGolf06oJ1wFfMbRa8230vG6miL47xjDtZy/bKw=" 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2017 Pine Mizune 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxKotlin Collections 2 | [![Build Status](https://img.shields.io/travis/pine/rxkotlin-collections/master.svg)](https://travis-ci.org/pine/rxkotlin-collections.svg?branch=master) [![Coverage Status](https://img.shields.io/coveralls/pine/rxkotlin-collections/master.svg)](https://coveralls.io/github/pine/rxkotlin-collections?branch=master) [![Dependency Status](https://img.shields.io/versioneye/d/user/projects/56f2a16f35630e0034fd9c8a.svg)](https://www.versioneye.com/user/projects/56f2a16f35630e0034fd9c8a) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpine%2Frxkotlin-collections.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fpine%2Frxkotlin-collections?ref=badge_shield) 3 | 4 | Kotlin Collections Methods for [RxJava2](https://github.com/ReactiveX/RxJava). 5 | 6 | ## Getting Started 7 | Please type it in your build.gradle file. 8 | 9 | ```groovy 10 | repositories { 11 | jcenter() 12 | } 13 | 14 | dependencies { 15 | compile 'moe.pine:rxkotlin-collections:0.2.10' // with RxJava 16 | compile 'moe.pine:rxkotlin-collections:0.3.0' // with RxJava 2 17 | } 18 | ``` 19 | 20 | ## Usage 21 | You can use Observable / Flowable extensions like [Kotlin collections](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/) as the following. 22 | 23 | ```kotlin 24 | import moe.pine.rx.collections.filterIndexed 25 | import io.reactivex.Observable 26 | 27 | val observable: Observable = Observable.fromArray("a", "b", "c") 28 | observable.filterIndexed { i, value -> i % 2 == 0 }.subscribe { println(it) } 29 | // => "a", "c" 30 | ``` 31 | 32 | In addition, this library provides an extensions of the following. 33 | 34 | ### Observable 35 | - filterIndexed 36 | - filterIsInstance 37 | - filterNot 38 | - flatten 39 | - forEachIndexed 40 | - isNotEmpty 41 | - mapIndexed 42 | - none 43 | - orEmpty 44 | - reduceIndexed 45 | - withIndex 46 | 47 | ### Flowable 48 | - filterIndexed 49 | - filterIsInstance 50 | - filterNot 51 | - flatten 52 | - forEachIndexed 53 | - isNotEmpty 54 | - mapIndexed 55 | - none 56 | - orEmpty 57 | - reduceIndexed 58 | - withIndex 59 | 60 | ## Test 61 | 62 | ``` 63 | $ ./gradlew clean test 64 | ``` 65 | 66 | ## Upload Bintray 67 | 68 | ``` 69 | $ export BINTRAY_USER=username 70 | $ export BINTRAY_API_KEY=apiKey 71 | $ ./gradlew clean assemble bintrayUpload 72 | ``` 73 | 74 | ## License 75 | MIT © Pine Mizune 76 | 77 | [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpine%2Frxkotlin-collections.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fpine%2Frxkotlin-collections?ref=badge_large) 78 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.1.61' 3 | ext.rxjava_version = '2.1.7' 4 | 5 | repositories { 6 | jcenter() 7 | } 8 | 9 | dependencies { 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.6.3' 12 | classpath 'net.rdrei.android.buildtimetracker:gradle-plugin:0.5.0' 13 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 15 | } 16 | } 17 | 18 | apply plugin: 'kotlin' 19 | apply plugin: 'signing' 20 | apply plugin: 'jacoco' 21 | apply plugin: 'com.github.kt3k.coveralls' 22 | 23 | group = 'moe.pine' 24 | version = '0.3.0' 25 | archivesBaseName = 'rxkotlin-collections' 26 | 27 | repositories { 28 | jcenter() 29 | } 30 | 31 | dependencies { 32 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 33 | compile "io.reactivex.rxjava2:rxjava:$rxjava_version" 34 | testCompile 'junit:junit:4.12' 35 | testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" 36 | } 37 | 38 | jacoco { 39 | toolVersion = '0.7.6.201602180812' 40 | } 41 | 42 | jacocoTestReport { 43 | reports { 44 | xml.enabled true 45 | csv.enabled false 46 | html.enabled true 47 | } 48 | } 49 | 50 | sourceSets { 51 | main.java.srcDirs += 'src/main/kotlin' 52 | test.java.srcDirs += 'src/test/kotlin' 53 | } 54 | 55 | task javadocJar(type: Jar, dependsOn: javadoc) { 56 | classifier = 'javadoc' 57 | from javadoc.destinationDir 58 | } 59 | 60 | task sourcesJar(type: Jar) { 61 | from sourceSets.main.java.srcDirs 62 | classifier = 'sources' 63 | } 64 | 65 | artifacts { 66 | archives javadocJar 67 | archives sourcesJar 68 | } 69 | 70 | task wrapper(type: Wrapper) { 71 | gradleVersion = '2.11' 72 | } 73 | 74 | task findConvensions << { 75 | println project.getConvention() 76 | } 77 | 78 | apply plugin: 'build-time-tracker' 79 | buildtimetracker { 80 | reporters { 81 | summary { 82 | ordered false 83 | threshold 50 84 | barstyle "unicode" 85 | } 86 | } 87 | } 88 | 89 | apply from: './gradle/bintray.gradle' 90 | 91 | check.dependsOn jacocoTestReport 92 | -------------------------------------------------------------------------------- /gradle/bintray.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | apply plugin: 'com.jfrog.bintray' 3 | 4 | def siteUrl = 'https://github.com/pine/rxkotlin-collections' 5 | def gitUrl = 'https://github.com/pine/rxkotlin-collections.git' 6 | def issueTrackerUrl = 'https://github.com/pine/rxkotlin-collections/issues' 7 | 8 | bintray { 9 | user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER') 10 | key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY') 11 | 12 | configurations = ['archives'] 13 | dryRun = false 14 | publish = true 15 | 16 | pkg { 17 | userOrg = 'pinemz' 18 | repo = 'maven' 19 | name = project.name 20 | desc = 'Kotlin Collections Methods for RxJava' 21 | licenses = ['MIT'] 22 | vcsUrl = siteUrl 23 | issueTrackerUrl = issueTrackerUrl 24 | publicDownloadNumbers = true 25 | 26 | version { 27 | name = project.version 28 | vcsTag = project.version 29 | } 30 | } 31 | } 32 | 33 | install { 34 | repositories.mavenInstaller { 35 | pom { 36 | project { 37 | name project.name 38 | url siteUrl 39 | licenses { 40 | license { 41 | name 'MIT License' 42 | url 'http://opensource.org/licenses/mit-license.php' 43 | } 44 | } 45 | developers { 46 | developer { 47 | id 'pinemz' 48 | name 'Pine Mizune' 49 | email 'pinemz@gmail.com' 50 | } 51 | } 52 | scm { 53 | connection gitUrl 54 | developerConnection gitUrl 55 | url siteUrl 56 | } 57 | } 58 | } 59 | } 60 | } 61 | 62 | artifacts { 63 | archives sourcesJar 64 | } 65 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pine/rxkotlin-collections/6d1bcb0c5b971e56fad277bb72f5883e10d2e9dc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 28 00:43:26 JST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/kotlin/moe/pine/rx/collections/Flowable.kt: -------------------------------------------------------------------------------- 1 | package moe.pine.rx.collections 2 | 3 | import io.reactivex.Flowable 4 | import io.reactivex.Maybe 5 | import io.reactivex.Observable 6 | import io.reactivex.Single 7 | import io.reactivex.functions.BiFunction 8 | 9 | /** 10 | * Collection Utils for Flowable 11 | * Created by pine on 2017/11/29. 12 | */ 13 | 14 | 15 | fun Flowable.filterIndexed(predicate: (Int, T) -> Boolean): Flowable { 16 | return this.withIndex().filter { predicate(it.index, it.value) }.map { it.value } 17 | } 18 | 19 | inline fun Flowable<*>.filterIsInstance(): Flowable { 20 | return this.filter { it is R }.map { it as R } 21 | } 22 | 23 | fun Flowable<*>.filterIsInstance(klass: Class): Flowable { 24 | @Suppress("UNCHECKED_CAST") 25 | return this.filter { klass.isInstance(it) }.map { it as R } 26 | } 27 | 28 | fun Flowable.filterNot(predicate: (T) -> Boolean): Flowable { 29 | return this.filter { !predicate(it) } 30 | } 31 | 32 | fun Flowable>.flatten(): Flowable { 33 | return this.flatMapIterable { it } 34 | } 35 | 36 | fun Flowable.forEachIndexed(action: (Int, T) -> Unit) { 37 | this.withIndex().forEach { action(it.index, it.value) } 38 | } 39 | 40 | fun Flowable.isNotEmpty(): Single { 41 | return this.isEmpty.map { !it } 42 | } 43 | 44 | fun Flowable.mapIndexed(transform: (Int, T) -> R): Flowable { 45 | return this.withIndex().map { transform(it.index, it.value) } 46 | } 47 | 48 | fun Flowable.none(): Single { 49 | return this.isEmpty 50 | } 51 | 52 | fun Flowable.none(predicate: (T) -> Boolean): Single { 53 | return this.filter(predicate).isEmpty 54 | } 55 | 56 | fun Flowable?.orEmpty(): Flowable { 57 | return this ?: Flowable.empty() 58 | } 59 | 60 | fun Flowable.reduceIndexed(operation: (Int, T, T) -> T): Maybe { 61 | return this.withIndex().reduce { accumulator, value -> 62 | IndexedValue(value.index, operation(value.index, accumulator.value, value.value)) 63 | }.map { it.value } 64 | } 65 | 66 | fun Flowable.reduceIndexed(initialValue: S, operation: (Int, S, T) -> T): Single { 67 | return this.withIndex().reduce(initialValue) { accumulator, value -> 68 | operation(value.index, accumulator, value.value) 69 | } 70 | } 71 | 72 | fun Flowable.withIndex(): Flowable> { 73 | return this.zipWith( 74 | Flowable.range(0, Int.MAX_VALUE), 75 | BiFunction { value, index -> IndexedValue(index, value) } 76 | ) 77 | } 78 | -------------------------------------------------------------------------------- /src/main/kotlin/moe/pine/rx/collections/Observable.kt: -------------------------------------------------------------------------------- 1 | package moe.pine.rx.collections 2 | 3 | import io.reactivex.Maybe 4 | import io.reactivex.Observable 5 | import io.reactivex.Single 6 | import io.reactivex.functions.BiFunction 7 | 8 | 9 | /** 10 | * Collection Utils for Observable 11 | * Created by pine on 2016/02/20. 12 | */ 13 | 14 | fun Observable.filterIndexed(predicate: (Int, T) -> Boolean): Observable { 15 | return this.withIndex().filter { predicate(it.index, it.value) }.map { it.value } 16 | } 17 | 18 | inline fun Observable<*>.filterIsInstance(): Observable { 19 | return this.filter { it is R }.map { it as R } 20 | } 21 | 22 | fun Observable<*>.filterIsInstance(klass: Class): Observable { 23 | @Suppress("UNCHECKED_CAST") 24 | return this.filter { klass.isInstance(it) }.map { it as R } 25 | } 26 | 27 | fun Observable.filterNot(predicate: (T) -> Boolean): Observable { 28 | return this.filter { !predicate(it) } 29 | } 30 | 31 | fun Observable>.flatten(): Observable { 32 | return this.flatMapIterable { it } 33 | } 34 | 35 | fun Observable.forEachIndexed(action: (Int, T) -> Unit) { 36 | this.withIndex().forEach { action(it.index, it.value) } 37 | } 38 | 39 | fun Observable.isNotEmpty(): Single { 40 | return this.isEmpty.map { !it } 41 | } 42 | 43 | fun Observable.mapIndexed(transform: (Int, T) -> R): Observable { 44 | return this.withIndex().map { transform(it.index, it.value) } 45 | } 46 | 47 | fun Observable.none(): Single { 48 | return this.isEmpty 49 | } 50 | 51 | fun Observable.none(predicate: (T) -> Boolean): Single { 52 | return this.filter(predicate).isEmpty 53 | } 54 | 55 | fun Observable?.orEmpty(): Observable { 56 | return this ?: Observable.empty() 57 | } 58 | 59 | fun Observable.reduceIndexed(operation: (Int, T, T) -> T): Maybe { 60 | return this.withIndex().reduce { accumulator, value -> 61 | IndexedValue(value.index, operation(value.index, accumulator.value, value.value)) 62 | }.map { it.value } 63 | } 64 | 65 | fun Observable.reduceIndexed(initialValue: S, operation: (Int, S, T) -> T): Single { 66 | return this.withIndex().reduce(initialValue) { accumulator, value -> 67 | operation(value.index, accumulator, value.value) 68 | } 69 | } 70 | 71 | fun Observable.withIndex(): Observable> { 72 | return this.zipWith( 73 | Observable.range(0, Int.MAX_VALUE), 74 | BiFunction { value, index -> IndexedValue(index, value) } 75 | ) 76 | } 77 | -------------------------------------------------------------------------------- /src/test/kotlin/moe/pine/rx/collections/FlowableTest.kt: -------------------------------------------------------------------------------- 1 | package moe.pine.rx.collections 2 | 3 | import io.reactivex.Flowable 4 | import io.reactivex.observers.TestObserver 5 | import io.reactivex.subscribers.TestSubscriber 6 | import org.junit.After 7 | import org.junit.Assert 8 | import org.junit.Before 9 | import org.junit.Test 10 | 11 | 12 | /** 13 | * Test for Flowable Extensions 14 | * Created by pine on 2017/11/29. 15 | */ 16 | class FlowableTest { 17 | @Before 18 | fun setup() { 19 | } 20 | 21 | @After 22 | fun tearDown() { 23 | } 24 | 25 | @Test 26 | fun filterIndexed() { 27 | val flowable: Flowable = Flowable.fromArray("a", "b", "c") 28 | TestSubscriber().apply { 29 | flowable.filterIndexed { i, s -> i == 0 || s == "c" }.subscribe(this) 30 | this.assertNoErrors() 31 | this.assertValueCount(2) 32 | this.assertValues("a", "c") 33 | this.assertComplete() 34 | } 35 | } 36 | 37 | @Test 38 | fun filterIsInstance() { 39 | val flowable: Flowable = Flowable.just(1, false, "a") 40 | TestSubscriber().apply { 41 | flowable.filterIsInstance().subscribe(this) 42 | this.assertNoErrors() 43 | this.assertValueCount(1) 44 | this.assertValue(false) 45 | this.assertComplete() 46 | } 47 | 48 | TestSubscriber().apply { 49 | flowable.filterIsInstance(String::class.java).subscribe(this) 50 | this.assertNoErrors() 51 | this.assertValueCount(1) 52 | this.assertValue("a") 53 | this.assertComplete() 54 | } 55 | } 56 | 57 | @Test 58 | fun filterNot() { 59 | val flowable: Flowable = Flowable.fromArray(1, 2, 3, 4) 60 | 61 | TestSubscriber().apply { 62 | flowable.filterNot { it % 2 == 0 }.subscribe(this) 63 | this.assertNoErrors() 64 | this.assertValueCount(2) 65 | this.assertValues(1, 3) 66 | this.assertComplete() 67 | } 68 | } 69 | 70 | @Test 71 | fun flatten() { 72 | val flowable: Flowable> = Flowable.just(listOf("a", "b", "c")) 73 | TestSubscriber().apply { 74 | flowable.flatten().subscribe(this) 75 | this.assertNoErrors() 76 | this.assertValueCount(3) 77 | this.assertValues("a", "b", "c") 78 | this.assertComplete() 79 | } 80 | } 81 | 82 | @Test 83 | fun forEachIndexed() { 84 | val flowable: Flowable = Flowable.fromArray("a", "b", "c") 85 | TestObserver>().apply { 86 | flowable.forEachIndexed { index, value -> this.onNext(IndexedValue(index, value)) } 87 | this.onComplete() 88 | this.assertValueCount(3) 89 | this.assertValues(IndexedValue(0, "a"), IndexedValue(1, "b"), IndexedValue(2, "c")) 90 | this.assertComplete() 91 | } 92 | } 93 | 94 | @Test 95 | fun isNotEmpty() { 96 | TestObserver().apply { 97 | Flowable.empty().isNotEmpty().subscribe(this) 98 | this.assertNoErrors() 99 | this.assertValueCount(1) 100 | this.assertValue(false) 101 | this.assertComplete() 102 | } 103 | 104 | TestObserver().apply { 105 | Flowable.just(1).isNotEmpty().subscribe(this) 106 | this.assertNoErrors() 107 | this.assertValueCount(1) 108 | this.assertValue(true) 109 | this.assertComplete() 110 | } 111 | } 112 | 113 | @Test 114 | fun mapIndexed() { 115 | val flowable = Flowable.fromArray("a", "b", "c") 116 | TestSubscriber>().apply { 117 | flowable.mapIndexed { index, value -> IndexedValue(index + 1, value) }.subscribe(this) 118 | this.assertNoErrors() 119 | this.assertValueCount(3) 120 | this.assertValues(IndexedValue(1, "a"), IndexedValue(2, "b"), IndexedValue(3, "c")) 121 | this.assertComplete() 122 | } 123 | } 124 | 125 | @Test 126 | fun none() { 127 | TestObserver().apply { 128 | Flowable.empty().none().subscribe(this) 129 | this.assertNoErrors() 130 | this.assertValueCount(1) 131 | this.assertValue(true) 132 | this.assertComplete() 133 | } 134 | 135 | TestObserver().apply { 136 | Flowable.just(0).none { it != 0 }.subscribe(this) 137 | this.assertNoErrors() 138 | this.assertValueCount(1) 139 | this.assertValue(true) 140 | this.assertComplete() 141 | } 142 | 143 | TestObserver().apply { 144 | Flowable.just(0).none().subscribe(this) 145 | this.assertNoErrors() 146 | this.assertValueCount(1) 147 | this.assertValue(false) 148 | this.assertComplete() 149 | } 150 | 151 | TestObserver().apply { 152 | Flowable.just(0).none { it == 0 }.subscribe(this) 153 | this.assertNoErrors() 154 | this.assertValueCount(1) 155 | this.assertValue(false) 156 | this.assertComplete() 157 | } 158 | } 159 | 160 | @Test 161 | fun orEmpty() { 162 | TestSubscriber().apply { 163 | (null as Flowable?).orEmpty().subscribe(this) 164 | this.assertNoErrors() 165 | this.assertValueCount(0) 166 | this.assertComplete() 167 | } 168 | 169 | TestSubscriber().apply { 170 | (Flowable.empty() as Flowable?).orEmpty().subscribe(this) 171 | this.assertNoErrors() 172 | this.assertValueCount(0) 173 | this.assertComplete() 174 | } 175 | 176 | TestSubscriber().apply { 177 | (Flowable.just(100) as Flowable?).orEmpty().subscribe(this) 178 | this.assertNoErrors() 179 | this.assertValueCount(1) 180 | this.assertValue(100) 181 | this.assertComplete() 182 | } 183 | } 184 | 185 | @Test 186 | fun reduceIndexed() { 187 | TestObserver().apply { 188 | var indexes = ArrayList() 189 | Flowable.just(1, 2, 3, 4).reduceIndexed { index, a, b -> indexes.add(index); a + b }.subscribe(this) 190 | 191 | this.assertNoErrors() 192 | this.assertValueCount(1) 193 | this.assertValue(10) 194 | this.assertComplete() 195 | 196 | Assert.assertEquals(listOf(1, 2, 3), indexes.toList()) 197 | } 198 | 199 | TestObserver().apply { 200 | var indexes = ArrayList() 201 | Flowable.just(1, 2, 3, 4).reduceIndexed(0) { index, a, b -> indexes.add(index); a + b }.subscribe(this) 202 | 203 | this.assertNoErrors() 204 | this.assertValueCount(1) 205 | this.assertValue(10) 206 | this.assertComplete() 207 | 208 | Assert.assertEquals(listOf(0, 1, 2, 3), indexes.toList()) 209 | } 210 | } 211 | 212 | @Test 213 | fun withIndex() { 214 | TestSubscriber>().apply { 215 | val flowable = Flowable.fromArray("a", "b", "c") 216 | flowable.withIndex().subscribe(this) 217 | this.assertNoErrors() 218 | this.assertValueCount(3) 219 | this.assertValues(IndexedValue(0, "a"), IndexedValue(1, "b"), IndexedValue(2, "c")) 220 | this.assertComplete() 221 | } 222 | } 223 | } -------------------------------------------------------------------------------- /src/test/kotlin/moe/pine/rx/collections/ObservableTest.kt: -------------------------------------------------------------------------------- 1 | package moe.pine.rx.collections 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.observers.TestObserver 5 | import org.junit.After 6 | import org.junit.Assert 7 | import org.junit.Before 8 | import org.junit.Test 9 | 10 | /** 11 | * Test for Observable Extensions 12 | * Created by pine on 2016/02/20. 13 | */ 14 | class ObservableTest { 15 | @Before 16 | fun setup() { 17 | } 18 | 19 | @After 20 | fun tearDown() { 21 | } 22 | 23 | @Test 24 | fun filterIndexed() { 25 | val observable: Observable = Observable.fromArray("a", "b", "c") 26 | TestObserver().apply { 27 | observable.filterIndexed { i, s -> i == 0 || s == "c" }.subscribe(this) 28 | this.assertNoErrors() 29 | this.assertValueCount(2) 30 | this.assertValues("a", "c") 31 | this.assertComplete() 32 | } 33 | } 34 | 35 | @Test 36 | fun filterIsInstance() { 37 | val observable: Observable = Observable.just(1, false, "a") 38 | TestObserver().apply { 39 | observable.filterIsInstance().subscribe(this) 40 | this.assertNoErrors() 41 | this.assertValueCount(1) 42 | this.assertValue(false) 43 | this.assertComplete() 44 | } 45 | 46 | TestObserver().apply { 47 | observable.filterIsInstance(String::class.java).subscribe(this) 48 | this.assertNoErrors() 49 | this.assertValueCount(1) 50 | this.assertValue("a") 51 | this.assertComplete() 52 | } 53 | } 54 | 55 | @Test 56 | fun filterNot() { 57 | val observable: Observable = Observable.fromArray(1, 2, 3, 4) 58 | 59 | TestObserver().apply { 60 | observable.filterNot { it % 2 == 0 }.subscribe(this) 61 | this.assertNoErrors() 62 | this.assertValueCount(2) 63 | this.assertValues(1, 3) 64 | this.assertComplete() 65 | } 66 | } 67 | 68 | @Test 69 | fun flatten() { 70 | val observable: Observable> = Observable.just(listOf("a", "b", "c")) 71 | TestObserver().apply { 72 | observable.flatten().subscribe(this) 73 | this.assertNoErrors() 74 | this.assertValueCount(3) 75 | this.assertValues("a", "b", "c") 76 | this.assertComplete() 77 | } 78 | } 79 | 80 | @Test 81 | fun forEachIndexed() { 82 | val observable: Observable = Observable.fromArray("a", "b", "c") 83 | TestObserver>().apply { 84 | observable.forEachIndexed { index, value -> this.onNext(IndexedValue(index, value)) } 85 | this.onComplete() 86 | this.assertValueCount(3) 87 | this.assertValues(IndexedValue(0, "a"), IndexedValue(1, "b"), IndexedValue(2, "c")) 88 | this.assertComplete() 89 | } 90 | } 91 | 92 | @Test 93 | fun isNotEmpty() { 94 | TestObserver().apply { 95 | Observable.empty().isNotEmpty().subscribe(this) 96 | this.assertNoErrors() 97 | this.assertValueCount(1) 98 | this.assertValue(false) 99 | this.assertComplete() 100 | } 101 | 102 | TestObserver().apply { 103 | Observable.just(1).isNotEmpty().subscribe(this) 104 | this.assertNoErrors() 105 | this.assertValueCount(1) 106 | this.assertValue(true) 107 | this.assertComplete() 108 | } 109 | } 110 | 111 | @Test 112 | fun mapIndexed() { 113 | val observable = Observable.fromArray("a", "b", "c") 114 | TestObserver>().apply { 115 | observable.mapIndexed { index, value -> IndexedValue(index + 1, value) }.subscribe(this) 116 | this.assertNoErrors() 117 | this.assertValueCount(3) 118 | this.assertValues(IndexedValue(1, "a"), IndexedValue(2, "b"), IndexedValue(3, "c")) 119 | this.assertComplete() 120 | } 121 | } 122 | 123 | @Test 124 | fun none() { 125 | TestObserver().apply { 126 | Observable.empty().none().subscribe(this) 127 | this.assertNoErrors() 128 | this.assertValueCount(1) 129 | this.assertValue(true) 130 | this.assertComplete() 131 | } 132 | 133 | TestObserver().apply { 134 | Observable.just(0).none { it != 0 }.subscribe(this) 135 | this.assertNoErrors() 136 | this.assertValueCount(1) 137 | this.assertValue(true) 138 | this.assertComplete() 139 | } 140 | 141 | TestObserver().apply { 142 | Observable.just(0).none().subscribe(this) 143 | this.assertNoErrors() 144 | this.assertValueCount(1) 145 | this.assertValue(false) 146 | this.assertComplete() 147 | } 148 | 149 | TestObserver().apply { 150 | Observable.just(0).none { it == 0 }.subscribe(this) 151 | this.assertNoErrors() 152 | this.assertValueCount(1) 153 | this.assertValue(false) 154 | this.assertComplete() 155 | } 156 | } 157 | 158 | 159 | @Test 160 | fun orEmpty() { 161 | TestObserver().apply { 162 | (null as Observable?).orEmpty().subscribe(this) 163 | this.assertNoErrors() 164 | this.assertValueCount(0) 165 | this.assertComplete() 166 | } 167 | 168 | TestObserver().apply { 169 | (Observable.empty() as Observable?).orEmpty().subscribe(this) 170 | this.assertNoErrors() 171 | this.assertValueCount(0) 172 | this.assertComplete() 173 | } 174 | 175 | TestObserver().apply { 176 | (Observable.just(100) as Observable?).orEmpty().subscribe(this) 177 | this.assertNoErrors() 178 | this.assertValueCount(1) 179 | this.assertValue(100) 180 | this.assertComplete() 181 | } 182 | } 183 | 184 | @Test 185 | fun reduceIndexed() { 186 | 187 | TestObserver().apply { 188 | var indexes = ArrayList() 189 | Observable.just(1, 2, 3, 4).reduceIndexed { index, a, b -> indexes.add(index); a + b }.subscribe(this) 190 | 191 | this.assertNoErrors() 192 | this.assertValueCount(1) 193 | this.assertValue(10) 194 | this.assertComplete() 195 | 196 | Assert.assertEquals(listOf(1, 2, 3), indexes.toList()) 197 | } 198 | 199 | TestObserver().apply { 200 | var indexes = ArrayList() 201 | Observable.just(1, 2, 3, 4).reduceIndexed(0) { index, a, b -> indexes.add(index); a + b }.subscribe(this) 202 | 203 | this.assertNoErrors() 204 | this.assertValueCount(1) 205 | this.assertValue(10) 206 | this.assertComplete() 207 | 208 | Assert.assertEquals(listOf(0, 1, 2, 3), indexes.toList()) 209 | } 210 | } 211 | 212 | @Test 213 | fun withIndex() { 214 | TestObserver>().apply { 215 | val observable = Observable.fromArray("a", "b", "c") 216 | observable.withIndex().subscribe(this) 217 | this.assertNoErrors() 218 | this.assertValueCount(3) 219 | this.assertValues(IndexedValue(0, "a"), IndexedValue(1, "b"), IndexedValue(2, "c")) 220 | this.assertComplete() 221 | } 222 | } 223 | } --------------------------------------------------------------------------------