├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── optional ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── java │ │ └── com │ │ └── eccyan │ │ └── optional │ │ ├── Optional.java │ │ └── utils │ │ └── Objects.java │ └── test │ └── java │ └── com │ └── eccyan │ ├── optional │ └── OptionalTest.java │ └── utils │ └── Strings.java ├── pom.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA 2 | .idea 3 | *.iml 4 | gen 5 | 6 | # Maven 7 | target/ 8 | pom.xml.versionsBackup 9 | 10 | # Gradle 11 | .gradle 12 | gradlew.bat 13 | build 14 | local.properties 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | # Turning off caching to avoid caching Issues 4 | # cache: false 5 | # Using the new Container-Based Infrastructure 6 | sudo: false 7 | 8 | before_install: 9 | # Making sure gradlew has executable permissions 10 | - chmod +x gradlew 11 | 12 | script: 13 | ./gradlew optional:clean optional:check 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 1.1.2 5 | ------------- 6 | 7 | * Fixed for using old APIs. 8 | 9 | Version 1.1.1 10 | ------------- 11 | 12 | * Fixed ``ifPresent`` throws 'Sequence contains no elements' when empty 13 | 14 | Version 1.1.0 15 | ------------- 16 | 17 | * Design changed to Optional has Observable 18 | 19 | Version 1.0.3 20 | ------------- 21 | 22 | * Fixed ``isPresent`` result has inversed 23 | * Added ``filter``,``map``and``flatMap`` 24 | 25 | Version 1.0.2 26 | ------------- 27 | 28 | * Replaced to gradle 29 | 30 | 31 | Version 1.0.1 32 | ------------- 33 | 34 | * Fixed pathes for jcenter 35 | 36 | Version 1.0.0 37 | ------------- 38 | 39 | * Initial release. 40 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RxJava-Optional 2 | ====================== 3 | 4 | [![Build Status](https://travis-ci.org/eccyan/RxJava-Optional.svg?branch=master)](https://travis-ci.org/eccyan/RxJava-Optional) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-RxJava--Optional-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1487) 5 | 6 | RxJava-Optional is an library that allows developers to use Optional using RxJava 7 | 8 | ## Install 9 | 10 | Download [the latest JAR](https://search.maven.org/remote_content?g=com.eccyan&a=rxjava-optional&v=LATEST) or grab via Gradle: 11 | 12 | ### Gradle 13 | ```groovy 14 | repositories { 15 | jcenter() 16 | } 17 | 18 | dependencies { 19 | compile 'io.reactivex:rxandroid:0.24.0' 20 | // compile 'io.reactivex:rxjava:1.0.5' 21 | compile 'com.eccyan:rxjava-optional:1.1.0' 22 | } 23 | ``` 24 | 25 | ## How to use 26 | 27 | ```java 28 | Optional lastName = Optional.ofNullable("Daisuke"); 29 | Optional firstName = Optional.ofNullable("Sato"); 30 | Optional fullname = 31 | lastName.flatMap(new Func1>() { 32 | @Override 33 | public Optional call(final String ln) { 34 | return firstName.map(new Func1() { 35 | @Override 36 | public String call(final String fn) { 37 | return Strings.join(" ", ln, fn); 38 | } 39 | }); 40 | } 41 | }); 42 | 43 | // With retrolambda 44 | fullName = lastName.flatMap(ln -> firstName.map(fn -> Strings.join(" ", ln, fn))); 45 | ``` 46 | 47 | Developed By 48 | ------- 49 | Daisuke Sato(eccyan) - 50 | 51 | 52 | 53 | Thanks 54 | ------- 55 | 56 | * Inspired by [visible true](http://sys1yagi.hatenablog.com/entry/2015/01/26/183000). 57 | 58 | * Using [RxJava](https://github.com/ReactiveX/RxJava) 59 | 60 | License 61 | ------- 62 | 63 | Copyright 2015 eccyan 64 | 65 | Licensed under the Apache License, Version 2.0 (the "License"); 66 | you may not use this file except in compliance with the License. 67 | You may obtain a copy of the License at 68 | 69 | http://www.apache.org/licenses/LICENSE-2.0 70 | 71 | Unless required by applicable law or agreed to in writing, software 72 | distributed under the License is distributed on an "AS IS" BASIS, 73 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 74 | See the License for the specific language governing permissions and 75 | limitations under the License. 76 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | def localProperty(name) { 2 | final Properties props = new Properties() 3 | final file = project.rootProject.file('local.properties') 4 | if (file.exists()) { 5 | props.load(file.newDataInputStream()) 6 | } 7 | 8 | return props.getProperty(name.toString()) 9 | } 10 | 11 | def bintrayUser() { 12 | return localProperty("bintrayUser") ?: bintrayUser ?: ""; 13 | } 14 | 15 | def bintrayApiKey() { 16 | return localProperty("bintrayApiKey") ?: bintrayApiKey ?: ""; 17 | } 18 | 19 | def gpgPassphrase() { 20 | return localProperty("gpgPassphrase") ?: gpgPassphrase ?: ""; 21 | } 22 | 23 | buildscript { 24 | repositories { 25 | jcenter() 26 | } 27 | dependencies { 28 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1' 29 | } 30 | } 31 | 32 | allprojects { 33 | apply plugin: 'idea' 34 | 35 | group = pkgGroup 36 | version = pkgVersion 37 | } 38 | 39 | subprojects { 40 | apply plugin: 'java' 41 | apply plugin: 'maven-publish' 42 | apply plugin: 'com.jfrog.bintray' 43 | 44 | sourceCompatibility = JavaVersion.VERSION_1_7 45 | targetCompatibility = JavaVersion.VERSION_1_7 46 | 47 | dependencies { 48 | testCompile 'junit:junit:4.11' 49 | } 50 | 51 | // custom tasks for creating source/javadoc jars 52 | task sourcesJar(type: Jar, dependsOn: classes) { 53 | classifier = 'sources' 54 | from sourceSets.main.allSource 55 | } 56 | 57 | task javadocJar(type: Jar, dependsOn: javadoc) { 58 | classifier = 'javadoc' 59 | from sourceSets.main.allJava 60 | } 61 | 62 | // add javadoc/source jar tasks as artifacts 63 | artifacts { 64 | archives sourcesJar, javadocJar 65 | } 66 | 67 | repositories { 68 | jcenter() 69 | } 70 | 71 | publishing { 72 | publications { 73 | mavenJava(MavenPublication) { 74 | groupId pkgGroup 75 | version pkgVersion 76 | artifactId pkgArtifactId 77 | artifact sourcesJar 78 | artifact javadocJar 79 | pom.withXml { 80 | final Node root = asNode() 81 | root.appendNode('name', "${pkgGroup}:${pkgArtifactId}") 82 | root.appendNode('description', pkgDesc) 83 | root.appendNode('url', pkgSiteUrl) 84 | 85 | final developer = root.appendNode('developers').appendNode('developer') 86 | developer.appendNode('id', 'eccyan') 87 | developer.appendNode('name', 'Daisuke Sato') 88 | developer.appendNode('email', 'g00.eccyan@gmail.com') 89 | developer.appendNode('url', 'https://twitter.com/eccyan') 90 | developer.appendNode('timezone', '+9') 91 | 92 | final issues = root.appendNode('issueManagement') 93 | issues.appendNode('system', 'Github Issues') 94 | issues.appendNode('url', "${pkgSiteUrl}/issues") 95 | 96 | final scm = root.appendNode('scm') 97 | scm.appendNode('url', pkgSiteUrl) 98 | scm.appendNode('connection', "scm:git:git://github.com/eccyan/${pkgName}.git") 99 | scm.appendNode('developerConnection', "scm:git:ssh://github.com/eccyan/${pkgName}.git") 100 | 101 | def license = root.appendNode('licenses').appendNode('license') 102 | license.appendNode('name', 'Apache 2.0') 103 | license.appendNode('url', 'http://www.apache.org/licenses/LICENSE-2.0.txt') 104 | } 105 | 106 | 107 | if (plugins.hasPlugin('war')) { 108 | from components.web 109 | } else { 110 | from components.java 111 | } 112 | } 113 | } 114 | } 115 | 116 | bintray { 117 | user = bintrayUser() 118 | key = bintrayApiKey() 119 | 120 | publications = ['mavenJava'] //When uploading Maven-based publication files 121 | dryRun = false //Whether to run this as dry-run, without deploying 122 | publish = false //If version should be auto published after an upload 123 | pkg { 124 | repo = 'maven' 125 | name = pkgName 126 | desc = pkgDesc 127 | websiteUrl = pkgSiteUrl 128 | issueTrackerUrl = "${pkgSiteUrl}/issues" 129 | vcsUrl = 'https://github.com/eccyan/RxJava-Optional.git' 130 | licenses = ['Apache-2.0'] 131 | labels = ['optional', 'backport'] 132 | publicDownloadNumbers = true 133 | version { 134 | name = pkgVersion 135 | vcsTag = pkgVersion 136 | gpg { 137 | sign = true //Determines whether to GPG sign the files. The default is false 138 | passphrase = gpgPassphrase() 139 | } 140 | } 141 | } 142 | } 143 | } 144 | 145 | 146 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | pkgGroup=com.eccyan 2 | pkgName=RxJava-Optional 3 | pkgDesc=RxJava-Optional is an library that allows developers to use Optional using RxJava 4 | pkgVersion=1.1.2 5 | pkgArtifactId=rxjava-optional 6 | pkgSiteUrl=https://github.com/eccyan/RxJava-Optional 7 | bintrayUser= 8 | bintrayApiKey= 9 | gpgPassphrase= 10 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eccyan/RxJava-Optional/d3c289e14d4c195a269a69ac7fc0998c670ddcee/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Dec 10 16:22:55 JST 2014 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.2.1-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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /optional/build.gradle: -------------------------------------------------------------------------------- 1 | configurations { 2 | provided 3 | } 4 | 5 | sourceSets { 6 | main { compileClasspath += configurations.provided } 7 | } 8 | 9 | dependencies { 10 | provided "io.reactivex:rxjava:1.0.5" 11 | } 12 | 13 | tasks.withType(Javadoc) { 14 | dependencies { 15 | compile "io.reactivex:rxjava:1.0.5" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /optional/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=rxjava-optional 2 | POM_ARTIFACT_ID=rxjava-optional 3 | POM_PACKAGING=jar 4 | -------------------------------------------------------------------------------- /optional/src/main/java/com/eccyan/optional/Optional.java: -------------------------------------------------------------------------------- 1 | package com.eccyan.optional; 2 | 3 | import com.eccyan.optional.utils.Objects; 4 | 5 | import rx.Observable; 6 | import rx.functions.Action1; 7 | import rx.functions.Func0; 8 | import rx.functions.Func1; 9 | 10 | /** 11 | * Created by Daisuke Sato on 2/5/15. 12 | */ 13 | public class Optional { 14 | 15 | private final Observable observable; 16 | 17 | public static Optional of(U data) { 18 | if (data == null) { 19 | throw new NullPointerException(); 20 | } 21 | 22 | return new Optional(Observable.just(data)); 23 | } 24 | 25 | public static Optional ofNullable(U data) { 26 | if (data == null) { 27 | return empty(); 28 | } else { 29 | return of(data); 30 | } 31 | } 32 | 33 | public static Optional empty() { 34 | return new Optional(Observable.empty()); 35 | } 36 | 37 | protected Optional(Observable observable) { 38 | this.observable = observable; 39 | } 40 | 41 | public boolean isPresent() { 42 | return !this.observable.isEmpty().toBlocking().single(); 43 | } 44 | 45 | public void ifPresent(Action1 action) { 46 | if (isPresent()) { 47 | Objects.requireNonNull(action); 48 | this.observable.subscribe(action); 49 | } 50 | } 51 | 52 | public T get() { 53 | return this.observable.toBlocking().single(); 54 | } 55 | 56 | public T orElse(T other) { 57 | return this.observable.defaultIfEmpty(other).toBlocking().single(); 58 | } 59 | 60 | public T orElseCall(Func0 other) { 61 | return isPresent() ? get() : other.call(); 62 | } 63 | 64 | public T orElseThrow(Func0 other) throws X { 65 | if (!isPresent()) { 66 | throw other.call(); 67 | } 68 | 69 | return get(); 70 | } 71 | 72 | public Optional filter(final Func1 predicate) { 73 | Objects.requireNonNull(predicate); 74 | 75 | return Optional.ofNullable(this.observable.filter(new Func1() { 76 | @Override 77 | public Boolean call(T t) { 78 | return predicate.call(t); 79 | } 80 | }).toBlocking().singleOrDefault(null)); 81 | } 82 | 83 | public Optional map(final Func1 mapper) { 84 | Objects.requireNonNull(mapper); 85 | 86 | return Optional.ofNullable(this.observable.map(new Func1() { 87 | @Override 88 | public U call(T t) { 89 | return mapper.call(t); 90 | } 91 | }).toBlocking().singleOrDefault(null)); 92 | } 93 | 94 | public Optional flatMap(final Func1> mapper) { 95 | Objects.requireNonNull(mapper); 96 | 97 | final Optional optional = this.observable.flatMap(new Func1>>() { 98 | @Override 99 | public Observable> call(T t) { 100 | return Observable.just(Objects.requireNonNull(mapper.call(t))); 101 | } 102 | }).toBlocking().singleOrDefault(Optional.empty()); 103 | 104 | return optional; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /optional/src/main/java/com/eccyan/optional/utils/Objects.java: -------------------------------------------------------------------------------- 1 | package com.eccyan.optional.utils; 2 | 3 | public final class Objects { 4 | public static T requireNonNull(T obj) { 5 | if (obj == null) 6 | throw new NullPointerException(); 7 | return obj; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /optional/src/test/java/com/eccyan/optional/OptionalTest.java: -------------------------------------------------------------------------------- 1 | package com.eccyan.optional; 2 | 3 | 4 | import com.eccyan.utils.Strings; 5 | 6 | import org.junit.Test; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | import rx.functions.Action1; 11 | import rx.functions.Func1; 12 | 13 | import static org.hamcrest.CoreMatchers.is; 14 | import static org.junit.Assert.assertFalse; 15 | import static org.junit.Assert.assertThat; 16 | import static org.junit.Assert.assertTrue; 17 | import static org.junit.Assert.fail; 18 | 19 | /** 20 | * Created by Daisuke Sato on 2/10/15. 21 | */ 22 | public class OptionalTest { 23 | 24 | @Test 25 | public void thisAlwaysPasses() { 26 | assertTrue(true); 27 | } 28 | 29 | @Test(expected = NoSuchElementException.class) 30 | public void optionalEmpty_get_throwsNoSuchElementException() { 31 | final Optional actual = Optional.empty(); 32 | Optional.empty().get(); 33 | } 34 | 35 | @Test 36 | public void isPresent_returnsTrueWhenValuePresent() { 37 | final Optional actual = Optional.ofNullable(1); 38 | assertTrue(actual.isPresent()); 39 | } 40 | 41 | @Test 42 | public void isPresent_returnsFalseWhenValuePresent() { 43 | final Optional actual = Optional.ofNullable(null); 44 | assertFalse(actual.isPresent()); 45 | } 46 | 47 | @Test 48 | public void ifPresent_doNothingWhenEmpty() { 49 | final Optional actual = Optional.ofNullable(null); 50 | actual.ifPresent(new Action1() { 51 | @Override 52 | public void call(Integer integer) { 53 | fail(); 54 | } 55 | }); 56 | } 57 | 58 | @Test(expected = NullPointerException.class) 59 | public void ifPresent_throwNullPointerExceptionWhenValuePresentAndActionIsNull() { 60 | final Optional actual = Optional.ofNullable(1); 61 | actual.ifPresent(null); 62 | } 63 | 64 | @Test 65 | public void ifPresent_notThrowWhenEmptyAndActionIsNull() { 66 | try { 67 | final Optional actual = Optional.ofNullable(null); 68 | actual.ifPresent(null); 69 | } catch (Exception e) { 70 | fail(); 71 | } 72 | } 73 | 74 | @Test 75 | public void filter_returnsOptionalDescribedValueWhenPredicateReturnsTrue() { 76 | final int expected = 12345; 77 | final Optional actual = Optional.ofNullable(expected) 78 | .filter(new Func1() { 79 | @Override 80 | public Boolean call( 81 | Integer integer) { 82 | return true; 83 | } 84 | }); 85 | 86 | assertThat(actual.get(), is(expected)); 87 | } 88 | 89 | @Test 90 | public void filter_returnsEmptyWhenPredicateReturnsFalse() { 91 | final Optional actual = Optional.ofNullable(12345) 92 | .filter(new Func1() { 93 | @Override 94 | public Boolean call(Integer integer) { 95 | return false; 96 | } 97 | }); 98 | 99 | assertFalse(actual.isPresent()); 100 | } 101 | 102 | @Test 103 | public void filter_returnsEmptyWhenValueIsEmpty() { 104 | final Optional empty = Optional.ofNullable(null); 105 | final Optional actual = empty.filter(new Func1() { 106 | @Override 107 | public Boolean call(Integer integer) { 108 | return true; 109 | } 110 | }); 111 | 112 | assertFalse(actual.isPresent()); 113 | } 114 | 115 | @Test 116 | public void map_returnsOptionalDescribedValueWhenValuePresent() { 117 | final Optional actual = Optional.ofNullable(1) 118 | .map(new Func1() { 119 | @Override 120 | public Integer call(Integer integer) { 121 | return integer + 1; 122 | } 123 | }); 124 | assertTrue(actual.isPresent()); 125 | assertThat(actual.get(), is(2)); 126 | } 127 | 128 | @Test 129 | public void map_returnsEmptyWhenValueNotPresent() { 130 | final Optional empty = Optional.ofNullable(null); 131 | final Optional actual = empty.map(new Func1() { 132 | @Override 133 | public Integer call(Integer integer) { 134 | return integer + 1; 135 | } 136 | }); 137 | 138 | assertFalse(actual.isPresent()); 139 | } 140 | 141 | @Test 142 | public void flatMap_returnsOptionalDescribedValueWhenValuePresent() { 143 | final Optional actual = Optional.ofNullable(1) 144 | .flatMap(new Func1>() { 145 | @Override 146 | public Optional call(Integer integer) { 147 | return Optional.ofNullable(integer + 2); 148 | } 149 | }); 150 | 151 | assertTrue(actual.isPresent()); 152 | assertThat(actual.get(), is(3)); 153 | } 154 | 155 | @Test 156 | public void flatMap_returnsEmptyWhenValueNotPresent() { 157 | final Optional empty = Optional.ofNullable(null); 158 | final Optional actual = empty 159 | .flatMap(new Func1>() { 160 | @Override 161 | public Optional call(Integer integer) { 162 | return Optional.ofNullable(integer + 1); 163 | } 164 | }); 165 | 166 | assertFalse(actual.isPresent()); 167 | } 168 | 169 | @Test(expected = NullPointerException.class) 170 | public void flatMap_throwsNullPointerExceptionWhenMapperReturnsNull() { 171 | final Optional actual = Optional.ofNullable(1) 172 | .flatMap(new Func1>() { 173 | @Override 174 | public Optional call(Integer integer) { 175 | return null; 176 | } 177 | }); 178 | } 179 | 180 | @Test 181 | public void returnsFullName() { 182 | final String expected = "Daisuke Sato"; 183 | final Optional lastName = Optional.ofNullable(expected.split(" ")[0]); 184 | final Optional firstName = Optional.ofNullable(expected.split(" ")[1]); 185 | final Optional actual = 186 | lastName.flatMap(new Func1>() { 187 | @Override 188 | public Optional call(final String ln) { 189 | return firstName.map(new Func1() { 190 | @Override 191 | public String call(final String fn) { 192 | return Strings.join(" ", ln, fn); 193 | } 194 | }); 195 | } 196 | }); 197 | 198 | assertThat(actual.get(), is(expected)); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /optional/src/test/java/com/eccyan/utils/Strings.java: -------------------------------------------------------------------------------- 1 | package com.eccyan.utils; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | import java.util.Objects; 6 | 7 | /** 8 | * Created by Daisuke Sato on 2/11/15. 9 | */ 10 | public final class Strings { 11 | 12 | public static String join(String separator, String... strings) { 13 | final Iterator iterator = Arrays.asList(strings).iterator(); 14 | 15 | if (!iterator.hasNext()) { 16 | return ""; 17 | } 18 | Object first = iterator.next(); 19 | if (!iterator.hasNext()) { 20 | return Objects.toString(first); 21 | } 22 | 23 | final StringBuilder builder = new StringBuilder(); 24 | if (first != null) { 25 | builder.append(first); 26 | } 27 | 28 | while (iterator.hasNext()) { 29 | if (separator != null) { 30 | builder.append(separator); 31 | } 32 | Object obj = iterator.next(); 33 | if (obj != null) { 34 | builder.append(obj); 35 | } 36 | } 37 | return builder.toString(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.eccyan 8 | rxjava-optional 9 | 1.0.1 10 | jar 11 | 12 | RxJava-Optional 13 | 14 | RxJava-Optional is an library that allows developers to use Optional using RxJava 15 | 16 | http://github.com/eccyan/RxJava-Optional 17 | 18 | 19 | UTF-8 20 | 21 | 1.0.5 22 | 23 | 24 | 25 | http://github.com/eccyan/RxJava-Optional/ 26 | scm:git:git://github.com/eccyan/RxJava-Optional.git 27 | scm:git:ssh://git@github.com/eccyan/RxJava-Optional.git 28 | 29 | HEAD 30 | 31 | 32 | 33 | GitHub Issues 34 | http://github.com/eccyan/RxJava-Optional/issues 35 | 36 | 37 | 38 | 39 | Apache 2.0 40 | http://www.apache.org/licenses/LICENSE-2.0.txt 41 | 42 | 43 | 44 | 45 | 46 | 47 | eccyan 48 | Daisuke Sato 49 | g00.eccyan@gmail.com 50 | eccyan 51 | http://twitter.com/eccyan 52 | 53 | Developer 54 | 55 | +9 56 | 57 | 58 | 59 | 60 | eccyan 61 | https://twitter.com/eccyan 62 | 63 | 64 | 65 | 66 | 67 | io.reactivex 68 | rxjava 69 | ${rxjava.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | 2.5 81 | 82 | 1.7 83 | 1.7 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-release-plugin 94 | 2.5 95 | 96 | true 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':optional' 2 | --------------------------------------------------------------------------------