├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── build.gradle ├── ci └── build.sh ├── dependencies.gradle ├── gradle ├── publishing.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── koptional-reactor-extensions ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── kotlin │ │ └── com │ │ └── gojuno │ │ └── koptional │ │ └── reactor │ │ └── Flux.kt │ └── test │ └── kotlin │ └── com │ └── gojuno │ └── koptional │ └── reactor │ └── FluxSpec.kt ├── koptional-rxjava2-extensions ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── kotlin │ │ └── com │ │ └── gojuno │ │ └── koptional │ │ └── rxjava2 │ │ ├── Flowable.kt │ │ ├── Maybe.kt │ │ ├── Observable.kt │ │ └── Single.kt │ └── test │ └── kotlin │ └── com │ └── gojuno │ └── koptional │ └── rxjava2 │ ├── FlowableSpec.kt │ ├── MaybeSpec.kt │ ├── ObservableSpec.kt │ └── SingleSpec.kt ├── koptional-rxjava3-extensions ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── kotlin │ │ └── com │ │ └── gojuno │ │ └── koptional │ │ └── rxjava3 │ │ ├── Flowable.kt │ │ ├── Maybe.kt │ │ ├── Observable.kt │ │ └── Single.kt │ └── test │ └── kotlin │ └── com │ └── gojuno │ └── koptional │ └── rxjava3 │ ├── FlowableSpec.kt │ ├── MaybeSpec.kt │ ├── ObservableSpec.kt │ └── SingleSpec.kt ├── koptional ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── kotlin │ │ └── com │ │ └── gojuno │ │ └── koptional │ │ └── Optional.kt │ └── test │ ├── java │ └── com │ │ └── gojuno │ │ └── koptional │ │ └── OptionalTests.java │ └── kotlin │ └── com │ └── gojuno │ └── koptional │ └── OptionalSpec.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/ 4 | *.iml 5 | .DS_Store 6 | build/ 7 | artifacts/ 8 | /captures 9 | gradle/local.properties 10 | /docs 11 | 12 | # Share code style. 13 | !.idea/codeStyleSettings.xml 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: shell 2 | 3 | # We use Docker → we need sudo. 4 | sudo: required 5 | 6 | services: 7 | - docker 8 | 9 | script: 10 | - ci/build.sh 11 | 12 | deploy: 13 | - provider: script 14 | script: PUBLISH=true ci/build.sh 15 | skip_cleanup: true 16 | on: 17 | tags: true 18 | 19 | notifications: 20 | email: false 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # See [Releases](https://github.com/gojuno/koptional/releases) 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 Juno Inc. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Koptional — Minimalistic `Optional` type for Kotlin 2 | 3 | > We don't think that Kotlin itself needs `Optional` because it has strong 4 | > `null`-safe type system that effectively eliminates need in such a wrapper. 5 | > However there are Java APIs and libraries like [RxJava 2][rxjava2] and [RxJava 3][rxjava3] which don't accept `null` values 6 | > and language-level nullability cannot help with that. 7 | 8 | > We also think that in many cases you can use `sealed class`es to express absent values, 9 | > however in simple cases like passing `String?` through RxJava stream `Optional` is a more convenient solution. 10 | 11 | The goal of this implementation is to be convenient to use and fit Kotlin's `null`-safe type system, which resulted in: 12 | 13 | * Only two functions: `toOptional()` and `toNullable()`. 14 | * Mimics Kotlin functions like `toInt()` and `toBoolean()`. 15 | * `Some` and `None` are declared as **top level types**. 16 | * No need to write `Optional.Some` or `Optional.None`. 17 | * No functions like `map()`, `getOrElse()`, `filter()`, etc. 18 | * Use `toNullable()` and Kotlin `stdlib` functions like `let()`, `takeIf()` instead. 19 | 20 | ## Usage 21 | 22 | ### Create 23 | 24 | ```kotlin 25 | val some = Some(value) 26 | val none = None // It's an object! 27 | ``` 28 | 29 | ### Convert 30 | 31 | ```kotlin 32 | // T? → Optional 33 | // If value is null — you'll get None, otherwise you'll get Some(value). 34 | val o = value.toOptional() 35 | 36 | // Optional → T? 37 | // If optional is None — you'll get null, otherwise you'll get non-null T value. 38 | val t = optional.toNullable() 39 | ``` 40 | 41 | ### Leverage Kotlin Features 42 | 43 | #### Fallback from `None` (like `java.util.Optional.getOrElse()`) 44 | 45 | ```kotlin 46 | val f = optional.toNullable() ?: "fallback" 47 | ``` 48 | #### [Smart Cast](http://kotlinlang.org/docs/reference/typecasts.html#smart-casts) 49 | 50 | ```kotlin 51 | when (optional) { 52 | is Some -> println(optional.value) 53 | is None -> println("Nope!") 54 | } 55 | ``` 56 | 57 | #### [Destructuring](https://kotlinlang.org/docs/reference/multi-declarations.html) 58 | 59 | ```kotlin 60 | // If Optional is None — you'll get null, otherwise you'll get non-null T value. 61 | val (value) = optional 62 | ``` 63 | 64 | ### Java Interop 65 | 66 | Use the static `Optional.toOptional()` to wrap an instance of `T` into `Optional`. 67 | 68 | ### RxJava Extensions 69 | 70 | ```kotlin 71 | val values = Observable.just(Some("a"), None, Some("b")) 72 | 73 | // Filter Some values. 74 | values 75 | .filterSome() 76 | .test() 77 | .assertValues("a", "b") 78 | 79 | // Filter None values. 80 | values 81 | .filterNone() 82 | .test() 83 | .assertValues(Unit) // filterNone() maps None to Unit. 84 | ``` 85 | 86 | ### Reactor Extensions 87 | 88 | ```kotlin 89 | val values = Flux.just(Some("a"), None, Some("b")) 90 | 91 | // Filter Some values. 92 | values.filterSome() 93 | 94 | // Filter None values. 95 | values.filterNone() 96 | ``` 97 | 98 | ## Download 99 | 100 | Koptional is [available on jcenter](https://jcenter.bintray.com/com/gojuno/koptional). 101 | All the releases and changelogs can be found on [Releases Page](https://github.com/gojuno/koptional/releases). 102 | 103 | ### `Optional` 104 | 105 | ```groovy 106 | implementation "com.gojuno.koptional:koptional:$koptional_version" 107 | ``` 108 | 109 | ### [RxJava 2][rxjava2] Extensions 110 | 111 | ```groovy 112 | implementation "com.gojuno.koptional:koptional-rxjava2-extensions:$koptional_version" 113 | ``` 114 | 115 | ### [RxJava 3][rxjava3] Extensions 116 | 117 | ```groovy 118 | implementation "com.gojuno.koptional:koptional-rxjava3-extensions:$koptional_version" 119 | ``` 120 | 121 | ### [Reactor](https://projectreactor.io/) Extensions 122 | 123 | ```groovy 124 | implementation "com.gojuno.koptional:koptional-reactor-extensions:$koptional_version" 125 | ``` 126 | 127 | 128 | ## License 129 | 130 | ``` 131 | Copyright 2017 Juno, Inc. 132 | 133 | Licensed under the Apache License, Version 2.0 (the "License"); 134 | you may not use this file except in compliance with the License. 135 | You may obtain a copy of the License at 136 | 137 | http://www.apache.org/licenses/LICENSE-2.0 138 | 139 | Unless required by applicable law or agreed to in writing, software 140 | distributed under the License is distributed on an "AS IS" BASIS, 141 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 142 | See the License for the specific language governing permissions and 143 | limitations under the License. 144 | ``` 145 | 146 | [rxjava2]: https://github.com/ReactiveX/RxJava/tree/2.x 147 | [rxjava3]: https://github.com/ReactiveX/RxJava/tree/3.x 148 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply from: 'dependencies.gradle' 2 | 3 | buildscript { 4 | // Gradle will not find vars defined in an external file when referring to them 5 | // in the buildscript block, unless you link it from the buildscript block, too. 6 | apply from: 'dependencies.gradle' 7 | 8 | repositories { 9 | jcenter() 10 | } 11 | 12 | dependencies { 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin" 14 | classpath 'com.github.ben-manes:gradle-versions-plugin:0.28.0' 15 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' 16 | } 17 | } 18 | 19 | apply plugin: 'com.github.ben-manes.versions' 20 | 21 | subprojects { 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | tasks.withType(Test) { 27 | maxParallelForks = Runtime.getRuntime().availableProcessors() / 2 28 | 29 | reports.junitXml.enabled = false 30 | 31 | useJUnitPlatform { 32 | includeEngines("spek") 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /ci/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | # You can run it from any directory. 5 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 | PROJECT_DIR="$DIR/.." 7 | 8 | pushd "$PROJECT_DIR" 9 | 10 | # Files created in mounted volume by container should have same owner as host machine user to prevent chmod problems. 11 | USER_ID=`id -u $USER` 12 | 13 | BUILD_COMMAND="set -xe && " 14 | BUILD_COMMAND+="apt-get update && apt-get --assume-yes install git && " 15 | 16 | if [ "$USER_ID" == "0" ]; then 17 | echo "Warning: running as r00t." 18 | else 19 | BUILD_COMMAND+="apt-get --assume-yes install sudo && " 20 | BUILD_COMMAND+="groupadd --gid $USER_ID build_user && " 21 | BUILD_COMMAND+="useradd --shell /bin/bash --uid $USER_ID --gid $USER_ID --create-home build_user && " 22 | BUILD_COMMAND+="sudo --set-home --preserve-env -u build_user " 23 | fi 24 | 25 | BUILD_COMMAND+="/opt/project/gradlew " 26 | BUILD_COMMAND+="--no-daemon --info --stacktrace " 27 | BUILD_COMMAND+="clean build " 28 | 29 | if [ "$PUBLISH" == "true" ]; then 30 | BUILD_COMMAND+="bintrayUpload " 31 | fi 32 | 33 | BUILD_COMMAND+="--project-dir /opt/project" 34 | 35 | docker run \ 36 | --env BINTRAY_USER="$BINTRAY_USER" \ 37 | --env BINTRAY_API_KEY="$BINTRAY_API_KEY" \ 38 | --env BINTRAY_GPG_PASSPHRASE="$BINTRAY_GPG_PASSPHRASE" \ 39 | --volume `"pwd"`:/opt/project \ 40 | --rm \ 41 | openjdk:8u252-jdk \ 42 | bash -c "$BUILD_COMMAND" 43 | 44 | popd 45 | -------------------------------------------------------------------------------- /dependencies.gradle: -------------------------------------------------------------------------------- 1 | ext.versions = [ 2 | kotlin : '1.3.72', 3 | rxJava2 : '2.2.19', 4 | rxJava3 : '3.0.4', 5 | reactor : '3.0.7.RELEASE', 6 | 7 | assertJ : '3.5.2', 8 | junitPlatform: '1.6.2', 9 | junitJupiter : "5.6.2", 10 | spek : '1.1.5', 11 | ] 12 | 13 | ext.libraries = [ 14 | koptional : project(':koptional'), 15 | koptionalRxJava2Extensions: project(':koptional-rxjava2-extensions'), 16 | koptionalRxJava3Extensions: project(':koptional-rxjava3-extensions'), 17 | 18 | kotlinStd : "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin", 19 | kotlinRuntime : "org.jetbrains.kotlin:kotlin-runtime:$versions.kotlin", 20 | kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin", 21 | rxJava2 : "io.reactivex.rxjava2:rxjava:$versions.rxJava2", 22 | rxJava3 : "io.reactivex.rxjava3:rxjava:$versions.rxJava3", 23 | reactor : "io.projectreactor:reactor-core:$versions.reactor", 24 | reactorTest : "io.projectreactor.addons:reactor-test:$versions.reactor", 25 | 26 | assertJ : "org.assertj:assertj-core:$versions.assertJ", 27 | junitPlatformCommons : "org.junit.platform:junit-platform-commons:$versions.junitPlatform", 28 | junitPlatformEngine : "org.junit.platform:junit-platform-engine:$versions.junitPlatform", 29 | junitJupiterApi : "org.junit.jupiter:junit-jupiter-api:$versions.junitPlatformEngineJupiter", 30 | junitJupiterEngine : "org.junit.jupiter:junit-jupiter-engine:$versions.junitPlatformEngineJupiter", 31 | junitSpekApi : "org.jetbrains.spek:spek-api:$versions.spek", 32 | junitSpekEngine : "org.jetbrains.spek:spek-junit-platform-engine:$versions.spek", 33 | ] 34 | -------------------------------------------------------------------------------- /gradle/publishing.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'maven' 2 | apply plugin: 'maven-publish' 3 | apply plugin: 'com.jfrog.bintray' 4 | 5 | def gitTag() { 6 | def tag = 'git tag --list --points-at HEAD'.execute((List) null, rootProject.projectDir).text.trim() 7 | 8 | if (tag.split(System.lineSeparator()).length > 1) { 9 | throw new IllegalStateException("gitTag is accessed but commit has multiple tags: $tag") 10 | } 11 | 12 | return tag 13 | } 14 | 15 | def projectVersion() { 16 | def tag = gitTag() 17 | 18 | if (tag.startsWith('v')) { 19 | return tag.substring(1) 20 | } else if (tag.isEmpty()) { 21 | return "development" 22 | } 23 | 24 | return tag 25 | } 26 | 27 | def validateTagAndVersion() { 28 | if (gitTag().isEmpty()) { 29 | throw new IllegalStateException('Publishing is not allowed because current commit has no tag') 30 | } 31 | 32 | if (projectVersion().isEmpty()) { 33 | throw new IllegalStateException('Publishing is not allowed because current projectVersion is empty') 34 | } 35 | } 36 | 37 | def pomConfig = { 38 | licenses { 39 | license { 40 | name 'The Apache Software License, Version 2.0' 41 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 42 | distribution 'repo' 43 | } 44 | } 45 | developers { 46 | developer { 47 | id 'gojuno' 48 | name 'Juno Inc.' 49 | email 'opensource@gojuno.com' 50 | } 51 | } 52 | } 53 | 54 | 55 | task sourcesJar(type: Jar, dependsOn: classes) { 56 | classifier = 'sources' 57 | from sourceSets.main.allSource 58 | } 59 | 60 | task javadocJar(type: Jar, dependsOn: javadoc) { 61 | classifier = 'javadoc' 62 | from javadoc.destinationDir 63 | } 64 | 65 | task validatePublishing { 66 | doLast { 67 | validateTagAndVersion() 68 | } 69 | } 70 | 71 | bintrayUpload.dependsOn validatePublishing 72 | 73 | publishing { 74 | publications { 75 | KoptionalPublication(MavenPublication) { 76 | from components.java 77 | 78 | artifact sourcesJar 79 | artifact javadocJar 80 | 81 | groupId 'com.gojuno.koptional' 82 | artifactId project.name 83 | version projectVersion() 84 | 85 | pom.withXml { 86 | def root = asNode() 87 | root.appendNode('description', project.description) 88 | root.appendNode('name', project.name) 89 | root.appendNode('url', 'https://github.com/gojuno/koptional') 90 | root.children().last() + pomConfig 91 | } 92 | } 93 | } 94 | } 95 | 96 | bintray { 97 | user = System.getenv('BINTRAY_USER') 98 | key = System.getenv('BINTRAY_API_KEY') 99 | publish = true 100 | 101 | pkg { 102 | repo = 'maven' 103 | name = 'koptional' 104 | licenses = ['Apache-2.0'] 105 | vcsUrl = 'https://github.com/gojuno/koptional.git' 106 | issueTrackerUrl = 'https://github.com/gojuno/koptional/issues' 107 | publications = ['KoptionalPublication'] 108 | 109 | version { 110 | name = projectVersion() 111 | vcsTag = gitTag() 112 | 113 | gpg { 114 | sign = true 115 | passphrase = System.getenv('BINTRAY_GPG_PASSPHRASE') 116 | } 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gojuno/koptional/841f7c33e5f3a258cb2f24bb6f7e4155a22f45cd/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 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 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /koptional-reactor-extensions/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply from: '../gradle/publishing.gradle' 3 | 4 | dependencies { 5 | compile libraries.koptional 6 | compile libraries.reactor 7 | } 8 | 9 | dependencies { 10 | testImplementation(libraries.junitPlatformCommons) 11 | testImplementation(libraries.junitSpekApi) 12 | testImplementation(libraries.kotlinReflect) 13 | testImplementation(libraries.reactorTest) 14 | } 15 | 16 | dependencies { 17 | testRuntimeOnly(libraries.junitPlatformEngine) 18 | testRuntimeOnly(libraries.junitSpekEngine) 19 | } 20 | 21 | tasks 22 | .withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) 23 | .findAll { task -> task.name.toLowerCase().contains('test') } 24 | .forEach { task -> 25 | configure(task) { 26 | kotlinOptions { 27 | // Reactor's test api exposed as static methods on an interface which requires jvmTarget 1.8. 28 | jvmTarget = '1.8' 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /koptional-reactor-extensions/gradle.properties: -------------------------------------------------------------------------------- 1 | description=Extensions to work with Optional in Reactor streams. 2 | -------------------------------------------------------------------------------- /koptional-reactor-extensions/src/main/kotlin/com/gojuno/koptional/reactor/Flux.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.reactor 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import reactor.core.publisher.Flux 7 | 8 | private inline fun Flux<*>.ofType(): Flux = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by a [Flux] by only emitting those that are [Some]. 12 | * 13 | * @return a [Flux] that emits [Some.value] of those items emitted by the source [Flux] that are [Some]. 14 | * @see [Flux.filter] 15 | */ 16 | fun Flux>.filterSome(): Flux = ofType>().map { it.value } 17 | 18 | /** 19 | * Filters items emitted by a [Flux] by only emitting those that are [None]. 20 | * 21 | * @return a [Flux] that emits [Unit] for each item emitted by the source [Flux] that is [None]. 22 | * @see [Flux.filter] 23 | */ 24 | fun Flux>.filterNone(): Flux = ofType().map { Unit } 25 | -------------------------------------------------------------------------------- /koptional-reactor-extensions/src/test/kotlin/com/gojuno/koptional/reactor/FluxSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.reactor 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import org.jetbrains.spek.api.Spek 7 | import org.jetbrains.spek.api.dsl.it 8 | import org.junit.platform.commons.annotation.Testable 9 | import reactor.core.publisher.Flux 10 | import reactor.test.StepVerifier 11 | 12 | @Testable 13 | class FluxSpec : Spek({ 14 | 15 | val flux: Flux> by memoized { 16 | Flux.just(Some("a"), None, Some("b"), Some("c"), None) 17 | } 18 | 19 | it("passes Some values on filterSome") { 20 | StepVerifier.create(flux.filterSome()) 21 | .expectNext("a", "b", "c") 22 | .expectComplete() 23 | .verify() 24 | } 25 | 26 | it("passes None values as Unit on filterNone") { 27 | StepVerifier.create(flux.filterNone()) 28 | .expectNext(Unit, Unit) 29 | .expectComplete() 30 | .verify() 31 | } 32 | 33 | }) 34 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply from: '../gradle/publishing.gradle' 3 | 4 | dependencies { 5 | compile libraries.koptional 6 | compile libraries.rxJava2 7 | } 8 | 9 | dependencies { 10 | testImplementation(libraries.junitPlatformCommons) 11 | testImplementation(libraries.junitSpekApi) 12 | testImplementation(libraries.kotlinReflect) 13 | } 14 | 15 | dependencies { 16 | testRuntimeOnly(libraries.junitPlatformEngine) 17 | testRuntimeOnly(libraries.junitSpekEngine) 18 | } 19 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/gradle.properties: -------------------------------------------------------------------------------- 1 | description=Extensions to work with Optional in RxJava2 streams. 2 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/main/kotlin/com/gojuno/koptional/rxjava2/Flowable.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Flowable 7 | 8 | private inline fun Flowable<*>.ofType(): Flowable = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by a [Flowable] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return a [Flowable] that emits [Some.value] of those items emitted by the source [Flowable] that are [Some]. 16 | * @see [Flowable.filter] 17 | */ 18 | fun Flowable>.filterSome(): Flowable = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by a [Flowable] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return a Flowable that emits [Unit] for each item emitted by the source [Flowable] that is [None]. 26 | * @see [Flowable.filter] 27 | */ 28 | fun Flowable>.filterNone(): Flowable = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/main/kotlin/com/gojuno/koptional/rxjava2/Maybe.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Maybe 7 | 8 | private inline fun Maybe<*>.ofType(): Maybe = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by a [Maybe] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return a [Maybe] that emits [Some.value] of those items emitted by the source [Maybe] that are [Some]. 16 | * @see [Maybe.filter] 17 | */ 18 | fun Maybe>.filterSome(): Maybe = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by a [Maybe] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return a [Maybe] that emits [Unit] for each item emitted by the source [Maybe] that is [None]. 26 | * @see [Maybe.filter] 27 | */ 28 | fun Maybe>.filterNone(): Maybe = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/main/kotlin/com/gojuno/koptional/rxjava2/Observable.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Observable 7 | 8 | private inline fun Observable<*>.ofType(): Observable = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by an [Observable] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return an [Observable] that emits [Some.value] of those items emitted by the source [Observable] that are [Some]. 16 | * @see [Observable.filter] 17 | */ 18 | fun Observable>.filterSome(): Observable = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by an [Observable] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return an [Observable] that emits [Unit] for each item emitted by the source [Observable] that is [None]. 26 | * @see [Observable.filter] 27 | */ 28 | fun Observable>.filterNone(): Observable = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/main/kotlin/com/gojuno/koptional/rxjava2/Single.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Maybe 7 | import io.reactivex.Single 8 | 9 | private inline fun Single<*>.ofType(): Maybe = filter { it is R }.cast(R::class.java) 10 | 11 | /** 12 | * Filters items emitted by a [Single] by only emitting those that are [Some]. 13 | * 14 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 15 | * 16 | * @return a [Maybe] that emits [Some.value] of those items emitted by the source [Single] that are [Some]. 17 | * @see [Single.filter] 18 | */ 19 | fun Single>.filterSome(): Maybe = ofType>().map { it.value } 20 | 21 | /** 22 | * Filters items emitted by a [Single] by only emitting those that are [None]. 23 | * 24 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 25 | * 26 | * @return an [Maybe] that emits [Unit] for each item emitted by the source [Single] that is [None]. 27 | * @see [Single.filter] 28 | */ 29 | fun Single>.filterNone(): Maybe = ofType().map { Unit } 30 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/test/kotlin/com/gojuno/koptional/rxjava2/FlowableSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Flowable 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.it 9 | import org.junit.platform.commons.annotation.Testable 10 | 11 | @Testable 12 | class FlowableSpec : Spek({ 13 | 14 | val flowable: Flowable> by memoized { 15 | Flowable.just(Some("a"), None, Some("b"), Some("c"), None) 16 | } 17 | 18 | it("passes Some values on filterSome") { 19 | flowable 20 | .filterSome() 21 | .test() 22 | .assertResult("a", "b", "c") 23 | } 24 | 25 | it("passes None values as Unit on filterNone") { 26 | flowable 27 | .filterNone() 28 | .test() 29 | .assertResult(Unit, Unit) 30 | } 31 | 32 | }) 33 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/test/kotlin/com/gojuno/koptional/rxjava2/MaybeSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Maybe 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.context 9 | import org.jetbrains.spek.api.dsl.it 10 | import org.junit.platform.commons.annotation.Testable 11 | 12 | @Testable 13 | class MaybeSpec : Spek({ 14 | 15 | context("value is Some") { 16 | 17 | val maybe: Maybe> by memoized { 18 | Maybe.just(Some("a")) 19 | } 20 | 21 | it("passes Some value on filterSome") { 22 | maybe 23 | .filterSome() 24 | .test() 25 | .assertResult("a") 26 | } 27 | 28 | it("does not pass value on filterNone") { 29 | maybe 30 | .filterNone() 31 | .test() 32 | .assertNoValues() 33 | .assertComplete() 34 | .assertNoErrors() 35 | } 36 | } 37 | 38 | context("value is None") { 39 | 40 | val maybe: Maybe> by memoized { 41 | Maybe.just(None) 42 | } 43 | 44 | it("does not pass Some value on filterSome") { 45 | maybe 46 | .filterSome() 47 | .test() 48 | .assertNoValues() 49 | .assertComplete() 50 | .assertNoErrors() 51 | } 52 | 53 | it("passes None value as Unit on filterNone") { 54 | maybe 55 | .filterNone() 56 | .test() 57 | .assertResult(Unit) 58 | } 59 | } 60 | 61 | }) 62 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/test/kotlin/com/gojuno/koptional/rxjava2/ObservableSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Observable 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.it 9 | import org.junit.platform.commons.annotation.Testable 10 | 11 | @Testable 12 | class ObservableSpec : Spek({ 13 | 14 | val observable: Observable> by memoized { 15 | Observable.just(Some("a"), None, Some("b"), Some("c"), None) 16 | } 17 | 18 | it("passes Some values on filterSome") { 19 | observable 20 | .filterSome() 21 | .test() 22 | .assertResult("a", "b", "c") 23 | } 24 | 25 | it("passes None values as Unit on filterNone") { 26 | observable 27 | .filterNone() 28 | .test() 29 | .assertResult(Unit, Unit) 30 | } 31 | 32 | }) 33 | -------------------------------------------------------------------------------- /koptional-rxjava2-extensions/src/test/kotlin/com/gojuno/koptional/rxjava2/SingleSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava2 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.Single 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.context 9 | import org.jetbrains.spek.api.dsl.it 10 | import org.junit.platform.commons.annotation.Testable 11 | 12 | @Testable 13 | class SingleSpec : Spek({ 14 | 15 | context("value is Some") { 16 | 17 | val single: Single> by memoized { 18 | Single.just(Some("a")) 19 | } 20 | 21 | it("passes Some value on filterSome") { 22 | single 23 | .filterSome() 24 | .test() 25 | .assertResult("a") 26 | } 27 | 28 | it("does not pass value on filterNone") { 29 | single 30 | .filterNone() 31 | .test() 32 | .assertNoValues() 33 | .assertComplete() 34 | .assertNoErrors() 35 | } 36 | } 37 | 38 | context("value is None") { 39 | 40 | val single: Single> by memoized { 41 | Single.just(None) 42 | } 43 | 44 | it("does not pass Some value on filterSome") { 45 | single 46 | .filterSome() 47 | .test() 48 | .assertNoValues() 49 | .assertComplete() 50 | .assertNoErrors() 51 | } 52 | 53 | it("passes None value as Unit on filterNone") { 54 | single 55 | .filterNone() 56 | .test() 57 | .assertResult(Unit) 58 | } 59 | } 60 | 61 | }) 62 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply from: '../gradle/publishing.gradle' 3 | 4 | dependencies { 5 | compile libraries.koptional 6 | compile libraries.rxJava3 7 | } 8 | 9 | dependencies { 10 | testImplementation(libraries.junitPlatformCommons) 11 | testImplementation(libraries.junitSpekApi) 12 | testImplementation(libraries.kotlinReflect) 13 | } 14 | 15 | dependencies { 16 | testRuntimeOnly(libraries.junitPlatformEngine) 17 | testRuntimeOnly(libraries.junitSpekEngine) 18 | } 19 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/gradle.properties: -------------------------------------------------------------------------------- 1 | description=Extensions to work with Optional in RxJava3 streams. 2 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/main/kotlin/com/gojuno/koptional/rxjava3/Flowable.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Flowable 7 | 8 | private inline fun Flowable<*>.ofType(): Flowable = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by a [Flowable] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return a [Flowable] that emits [Some.value] of those items emitted by the source [Flowable] that are [Some]. 16 | * @see [Flowable.filter] 17 | */ 18 | fun Flowable>.filterSome(): Flowable = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by a [Flowable] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return a Flowable that emits [Unit] for each item emitted by the source [Flowable] that is [None]. 26 | * @see [Flowable.filter] 27 | */ 28 | fun Flowable>.filterNone(): Flowable = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/main/kotlin/com/gojuno/koptional/rxjava3/Maybe.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Maybe 7 | 8 | private inline fun Maybe<*>.ofType(): Maybe = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by a [Maybe] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return a [Maybe] that emits [Some.value] of those items emitted by the source [Maybe] that are [Some]. 16 | * @see [Maybe.filter] 17 | */ 18 | fun Maybe>.filterSome(): Maybe = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by a [Maybe] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return a [Maybe] that emits [Unit] for each item emitted by the source [Maybe] that is [None]. 26 | * @see [Maybe.filter] 27 | */ 28 | fun Maybe>.filterNone(): Maybe = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/main/kotlin/com/gojuno/koptional/rxjava3/Observable.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Observable 7 | 8 | private inline fun Observable<*>.ofType(): Observable = ofType(R::class.java) 9 | 10 | /** 11 | * Filters items emitted by an [Observable] by only emitting those that are [Some]. 12 | * 13 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 14 | * 15 | * @return an [Observable] that emits [Some.value] of those items emitted by the source [Observable] that are [Some]. 16 | * @see [Observable.filter] 17 | */ 18 | fun Observable>.filterSome(): Observable = ofType>().map { it.value } 19 | 20 | /** 21 | * Filters items emitted by an [Observable] by only emitting those that are [None]. 22 | * 23 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 24 | * 25 | * @return an [Observable] that emits [Unit] for each item emitted by the source [Observable] that is [None]. 26 | * @see [Observable.filter] 27 | */ 28 | fun Observable>.filterNone(): Observable = ofType().map { Unit } 29 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/main/kotlin/com/gojuno/koptional/rxjava3/Single.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Maybe 7 | import io.reactivex.rxjava3.core.Single 8 | 9 | private inline fun Single<*>.ofType(): Maybe = filter { it is R }.cast(R::class.java) 10 | 11 | /** 12 | * Filters items emitted by a [Single] by only emitting those that are [Some]. 13 | * 14 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 15 | * 16 | * @return a [Maybe] that emits [Some.value] of those items emitted by the source [Single] that are [Some]. 17 | * @see [Single.filter] 18 | */ 19 | fun Single>.filterSome(): Maybe = ofType>().map { it.value } 20 | 21 | /** 22 | * Filters items emitted by a [Single] by only emitting those that are [None]. 23 | * 24 | * Does not operate by default on a particular [Scheduler][io.reactivex.Scheduler]. 25 | * 26 | * @return an [Maybe] that emits [Unit] for each item emitted by the source [Single] that is [None]. 27 | * @see [Single.filter] 28 | */ 29 | fun Single>.filterNone(): Maybe = ofType().map { Unit } 30 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/test/kotlin/com/gojuno/koptional/rxjava3/FlowableSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Flowable 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.it 9 | import org.junit.platform.commons.annotation.Testable 10 | 11 | @Testable 12 | class FlowableSpec : Spek({ 13 | 14 | val flowable: Flowable> by memoized { 15 | Flowable.just(Some("a"), None, Some("b"), Some("c"), None) 16 | } 17 | 18 | it("passes Some values on filterSome") { 19 | flowable 20 | .filterSome() 21 | .test() 22 | .assertResult("a", "b", "c") 23 | } 24 | 25 | it("passes None values as Unit on filterNone") { 26 | flowable 27 | .filterNone() 28 | .test() 29 | .assertResult(Unit, Unit) 30 | } 31 | 32 | }) 33 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/test/kotlin/com/gojuno/koptional/rxjava3/MaybeSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Maybe 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.context 9 | import org.jetbrains.spek.api.dsl.it 10 | import org.junit.platform.commons.annotation.Testable 11 | 12 | @Testable 13 | class MaybeSpec : Spek({ 14 | 15 | context("value is Some") { 16 | 17 | val maybe: Maybe> by memoized { 18 | Maybe.just(Some("a")) 19 | } 20 | 21 | it("passes Some value on filterSome") { 22 | maybe 23 | .filterSome() 24 | .test() 25 | .assertResult("a") 26 | } 27 | 28 | it("does not pass value on filterNone") { 29 | maybe 30 | .filterNone() 31 | .test() 32 | .assertNoValues() 33 | .assertComplete() 34 | .assertNoErrors() 35 | } 36 | } 37 | 38 | context("value is None") { 39 | 40 | val maybe: Maybe> by memoized { 41 | Maybe.just(None) 42 | } 43 | 44 | it("does not pass Some value on filterSome") { 45 | maybe 46 | .filterSome() 47 | .test() 48 | .assertNoValues() 49 | .assertComplete() 50 | .assertNoErrors() 51 | } 52 | 53 | it("passes None value as Unit on filterNone") { 54 | maybe 55 | .filterNone() 56 | .test() 57 | .assertResult(Unit) 58 | } 59 | } 60 | 61 | }) 62 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/test/kotlin/com/gojuno/koptional/rxjava3/ObservableSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Observable 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.it 9 | import org.junit.platform.commons.annotation.Testable 10 | 11 | @Testable 12 | class ObservableSpec : Spek({ 13 | 14 | val observable: Observable> by memoized { 15 | Observable.just(Some("a"), None, Some("b"), Some("c"), None) 16 | } 17 | 18 | it("passes Some values on filterSome") { 19 | observable 20 | .filterSome() 21 | .test() 22 | .assertResult("a", "b", "c") 23 | } 24 | 25 | it("passes None values as Unit on filterNone") { 26 | observable 27 | .filterNone() 28 | .test() 29 | .assertResult(Unit, Unit) 30 | } 31 | 32 | }) 33 | -------------------------------------------------------------------------------- /koptional-rxjava3-extensions/src/test/kotlin/com/gojuno/koptional/rxjava3/SingleSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional.rxjava3 2 | 3 | import com.gojuno.koptional.None 4 | import com.gojuno.koptional.Optional 5 | import com.gojuno.koptional.Some 6 | import io.reactivex.rxjava3.core.Single 7 | import org.jetbrains.spek.api.Spek 8 | import org.jetbrains.spek.api.dsl.context 9 | import org.jetbrains.spek.api.dsl.it 10 | import org.junit.platform.commons.annotation.Testable 11 | 12 | @Testable 13 | class SingleSpec : Spek({ 14 | 15 | context("value is Some") { 16 | 17 | val single: Single> by memoized { 18 | Single.just(Some("a")) 19 | } 20 | 21 | it("passes Some value on filterSome") { 22 | single 23 | .filterSome() 24 | .test() 25 | .assertResult("a") 26 | } 27 | 28 | it("does not pass value on filterNone") { 29 | single 30 | .filterNone() 31 | .test() 32 | .assertNoValues() 33 | .assertComplete() 34 | .assertNoErrors() 35 | } 36 | } 37 | 38 | context("value is None") { 39 | 40 | val single: Single> by memoized { 41 | Single.just(None) 42 | } 43 | 44 | it("does not pass Some value on filterSome") { 45 | single 46 | .filterSome() 47 | .test() 48 | .assertNoValues() 49 | .assertComplete() 50 | .assertNoErrors() 51 | } 52 | 53 | it("passes None value as Unit on filterNone") { 54 | single 55 | .filterNone() 56 | .test() 57 | .assertResult(Unit) 58 | } 59 | } 60 | 61 | }) 62 | -------------------------------------------------------------------------------- /koptional/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply from: '../gradle/publishing.gradle' 3 | 4 | dependencies { 5 | compile libraries.kotlinStd 6 | } 7 | 8 | dependencies { 9 | testImplementation(libraries.assertJ) 10 | testImplementation(libraries.junitPlatformCommons) 11 | testImplementation(libraries.junitJupiterApi) 12 | testImplementation(libraries.junitSpekApi) 13 | testImplementation(libraries.kotlinReflect) 14 | } 15 | 16 | dependencies { 17 | testRuntimeOnly(libraries.junitJupiterEngine) 18 | testRuntimeOnly(libraries.junitPlatformEngine) 19 | testRuntimeOnly(libraries.junitSpekEngine) 20 | } 21 | -------------------------------------------------------------------------------- /koptional/gradle.properties: -------------------------------------------------------------------------------- 1 | description=Minimalistic Optional type for Kotlin. 2 | -------------------------------------------------------------------------------- /koptional/src/main/kotlin/com/gojuno/koptional/Optional.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional 2 | 3 | sealed class Optional { 4 | 5 | /** 6 | * Converts [Optional] to either its non-`null` value if it’s [Some] or `null` if it’s [None]. 7 | */ 8 | abstract fun toNullable(): T? 9 | 10 | /** 11 | * Unwraps this [Optional] to either its non-`null` value if it’s [Some] or `null` if it’s [None]. 12 | */ 13 | @JvmSynthetic 14 | abstract operator fun component1(): T? 15 | 16 | companion object { 17 | 18 | /** 19 | * Wraps an instance of `T` (or `null`) into an [Optional]: 20 | * 21 | * ```java 22 | * Optional some = Optional.toOptional("value"); // Some("value") 23 | * Optional none = Optional.toOptional(null); // None 24 | * ``` 25 | * 26 | * This is the preferred method of obtaining an instance of [Optional] in Java. In Kotlin, 27 | * prefer using the [toOptional][com.gojuno.koptional.toOptional] extension function. 28 | */ 29 | @JvmStatic 30 | fun toOptional(value: T?): Optional = if (value == null) None else Some(value) 31 | } 32 | } 33 | 34 | data class Some(val value: T) : Optional() { 35 | override fun toNullable(): T = value 36 | override fun toString() = "Some($value)" 37 | } 38 | 39 | object None : Optional() { 40 | override fun component1(): Nothing? = null 41 | override fun toNullable(): Nothing? = null 42 | override fun toString() = "None" 43 | } 44 | 45 | /** 46 | * Wraps an instance of `T` (or `null`) into an [Optional]: 47 | * 48 | * ```kotlin 49 | * val someValue: String? = "value" 50 | * val noneValue: String? = null 51 | * 52 | * val some = someValue.toOptional() // Some("value") 53 | * val none = noneValue.toOptional() // None 54 | * ``` 55 | * 56 | * This is the preferred method of obtaining an instance of [Optional] in Kotlin. In Java, prefer 57 | * using the static [Optional.toOptional] method. 58 | */ 59 | fun T?.toOptional(): Optional = if (this == null) None else Some(this) 60 | -------------------------------------------------------------------------------- /koptional/src/test/java/com/gojuno/koptional/OptionalTests.java: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | public class OptionalTests { 8 | 9 | @Test 10 | public void toNullableSome() { 11 | assertThat(new Some<>("value").toNullable()).isEqualTo("value"); 12 | } 13 | 14 | @Test 15 | public void toNullableNone() { 16 | assertThat(None.INSTANCE.toNullable()).isNull(); 17 | } 18 | 19 | @Test 20 | public void toOptionalSome() { 21 | assertThat(Optional.toOptional("value")).isEqualTo(new Some<>("value")); 22 | } 23 | 24 | @Test 25 | public void toOptionalSomeCompat() { 26 | assertThat(OptionalKt.toOptional("value")).isEqualTo(new Some<>("value")); 27 | } 28 | 29 | @Test 30 | public void toOptionalNone() { 31 | assertThat(Optional.toOptional(null)).isEqualTo(None.INSTANCE); 32 | } 33 | 34 | @Test 35 | public void toOptionalNoneCompat() { 36 | assertThat(OptionalKt.toOptional(null)).isEqualTo(None.INSTANCE); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt: -------------------------------------------------------------------------------- 1 | package com.gojuno.koptional 2 | 3 | import org.assertj.core.api.Assertions.assertThat 4 | import org.jetbrains.spek.api.Spek 5 | import org.jetbrains.spek.api.dsl.describe 6 | import org.jetbrains.spek.api.dsl.it 7 | import org.junit.platform.commons.annotation.Testable 8 | 9 | @Testable 10 | class OptionalSpec : Spek({ 11 | 12 | describe("toNullable") { 13 | 14 | it("converts Some to value") { 15 | val some: Optional = Some("value") 16 | 17 | assertThat(some.toNullable()).isEqualTo("value") 18 | } 19 | 20 | it("converts None to null") { 21 | val none: Optional = None 22 | 23 | assertThat(none.toNullable()).isNull() 24 | } 25 | } 26 | 27 | describe("toOptional") { 28 | 29 | it("converts value to Some") { 30 | val value = "value" 31 | 32 | assertThat(value.toOptional()).isEqualTo(Some(value)) 33 | } 34 | 35 | it("converts null to None") { 36 | val value: String? = null 37 | 38 | assertThat(value.toOptional()).isEqualTo(None) 39 | } 40 | } 41 | 42 | describe("toString") { 43 | 44 | it("produces Some representation") { 45 | assertThat(Some("value").toString()).isEqualTo("Some(value)") 46 | } 47 | 48 | it("produces None representation") { 49 | assertThat(None.toString()).isEqualTo("None") 50 | } 51 | } 52 | 53 | describe("destructuring") { 54 | 55 | it("destructures Some to value") { 56 | val some: Optional = Some("value") 57 | val (destructure) = some 58 | 59 | assertThat(destructure).isEqualTo("value") 60 | } 61 | 62 | it("destructures None to null") { 63 | val none: Optional = None 64 | val (destructure) = none 65 | 66 | assertThat(destructure).isNull() 67 | } 68 | } 69 | 70 | }) 71 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':koptional' 2 | include ':koptional-rxjava2-extensions' 3 | include ':koptional-rxjava3-extensions' 4 | include ':koptional-reactor-extensions' 5 | --------------------------------------------------------------------------------