From d6d17a66825d1ace733608ac0e54a30c9c31725d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 22 Jun 2018 23:52:29 +0100 Subject: [PATCH] Split Maybe interface into MaybeOptional and MaybeStream Also deprecated Maybe.fromOptional(Optional) --- CHANGELOG | 1 + .../java/net/kemitix/mon/maybe/Maybe.java | 69 ++---------------- .../net/kemitix/mon/maybe/MaybeOptional.java | 71 +++++++++++++++++++ .../net/kemitix/mon/maybe/MaybeStream.java | 62 ++++++++++++++++ src/test/java/net/kemitix/mon/MaybeTest.java | 4 ++ 5 files changed, 143 insertions(+), 64 deletions(-) create mode 100644 src/main/java/net/kemitix/mon/maybe/MaybeOptional.java create mode 100644 src/main/java/net/kemitix/mon/maybe/MaybeStream.java diff --git a/CHANGELOG b/CHANGELOG index dfa1644..354d099 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,7 @@ CHANGELOG * Add `Result` * Moved `Maybe` to `net.kemitix.mon.maybe.Maybe` * Add `Maybe.stream()` +* Deprecate `Maybe.fromOptional(Optional)` 0.6.0 ----- diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index a3fe6eb..25180ba 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -25,10 +25,6 @@ import lombok.NonNull; import net.kemitix.mon.Functor; import java.util.Optional; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.stream.Stream; /** * A value that may or may not be present. @@ -37,7 +33,7 @@ import java.util.stream.Stream; * * @author Paul Campbell (pcampbell@kemitix.net) */ -public interface Maybe extends Functor> { +public interface Maybe extends Functor>, MaybeStream, MaybeOptional { /** * Create a Maybe for the value that is present. @@ -85,69 +81,14 @@ public interface Maybe extends Functor> { * @param the type of the Optional * * @return a Maybe + * @deprecated need to find a better way of converting an Optional to a Maybe, but + * without having to pass the Optional as a parameter + * Try: Optional.of(1).map(Maybe::just).orElseGet(Maybe::nothing) */ + @Deprecated static Maybe fromOptional(final Optional optional) { return optional.map(Maybe::maybe) .orElse(nothing()); } - /** - * Provide a value to use when Maybe is Nothing. - * - * @param supplier supplier for an alternate value - * - * @return a Maybe - */ - T orElseGet(Supplier supplier); - - /** - * A value to use when Maybe is Nothing. - * - * @param otherValue an alternate value - * - * @return a Maybe - */ - T orElse(T otherValue); - - /** - * Filter a Maybe by the predicate, replacing with Nothing when it fails. - * - * @param predicate the test - * - * @return the Maybe, or Nothing if the test returns false - */ - Maybe filter(Predicate predicate); - - /** - * Convert the Maybe to an {@link Optional}. - * - * @return an Optional containing a value for a Just, or empty for a Nothing - */ - Optional toOptional(); - - /** - * Provide the value within the Maybe, if it exists, to the Supplier, and returns the Maybe. - * - * @param consumer the Consumer to the value if present - * - * @return the Maybe - */ - Maybe peek(Consumer consumer); - - /** - * Throw the exception if the Maybe is a Nothing. - * - * @param e the exception to throw - * - * @throws Exception if the Maybe is a Nothing - */ - @SuppressWarnings("illegalthrows") - void orElseThrow(Supplier e) throws Exception; - - /** - * Converts the Maybe into either a single value stream or and empty stream. - * - * @return a Stream containing the value or nothing. - */ - Stream stream(); } diff --git a/src/main/java/net/kemitix/mon/maybe/MaybeOptional.java b/src/main/java/net/kemitix/mon/maybe/MaybeOptional.java new file mode 100644 index 0000000..44f8700 --- /dev/null +++ b/src/main/java/net/kemitix/mon/maybe/MaybeOptional.java @@ -0,0 +1,71 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon.maybe; + +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Enables emulation parts of and conversion to the Java Optional class for Maybe. + * + * @param the type of the content of the Just + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +interface MaybeOptional { + + /** + * Provide a value to use when Maybe is Nothing. + * + * @param supplier supplier for an alternate value + * + * @return a Maybe + */ + T orElseGet(Supplier supplier); + + /** + * A value to use when Maybe is Nothing. + * + * @param otherValue an alternate value + * + * @return a Maybe + */ + T orElse(T otherValue); + + /** + * Convert the Maybe to an {@link Optional}. + * + * @return an Optional containing a value for a Just, or empty for a Nothing + */ + Optional toOptional(); + + /** + * Throw the exception if the Maybe is a Nothing. + * + * @param e the exception to throw + * + * @throws Exception if the Maybe is a Nothing + */ + @SuppressWarnings("illegalthrows") + void orElseThrow(Supplier e) throws Exception; + +} diff --git a/src/main/java/net/kemitix/mon/maybe/MaybeStream.java b/src/main/java/net/kemitix/mon/maybe/MaybeStream.java new file mode 100644 index 0000000..4036ee8 --- /dev/null +++ b/src/main/java/net/kemitix/mon/maybe/MaybeStream.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon.maybe; + +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.stream.Stream; + +/** + * Enables emulation parts of and conversion to the Java Stream class for Maybe. + * + * @param the type of the content of the Just + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +interface MaybeStream { + + /** + * Converts the Maybe into either a single value stream or and empty stream. + * + * @return a Stream containing the value or nothing. + */ + Stream stream(); + + /** + * Filter a Maybe by the predicate, replacing with Nothing when it fails. + * + * @param predicate the test + * + * @return the Maybe, or Nothing if the test returns false + */ + Maybe filter(Predicate predicate); + + /** + * Provide the value within the Maybe, if it exists, to the Supplier, and returns the Maybe. + * + * @param consumer the Consumer to the value if present + * + * @return the Maybe + */ + Maybe peek(Consumer consumer); + +} diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java index 90ae989..4f3b31b 100644 --- a/src/test/java/net/kemitix/mon/MaybeTest.java +++ b/src/test/java/net/kemitix/mon/MaybeTest.java @@ -85,8 +85,12 @@ public class MaybeTest implements WithAssertions { @Test public void fromOptional() { + // deprecated methods assertThat(Maybe.fromOptional(Optional.of(1))).isEqualTo(just(1)); assertThat(Maybe.fromOptional(Optional.empty())).isEqualTo(nothing()); + // recommended alternative + assertThat(Optional.of(1).map(Maybe::just).orElseGet(Maybe::nothing)).isEqualTo(just(1)); + assertThat(Optional.empty().map(Maybe::just).orElseGet(Maybe::nothing)).isEqualTo(nothing()); }