From 2211182c7d050cf84bec1df19b266ee372cab289 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 25 Jun 2018 22:04:23 +0100 Subject: [PATCH] Collapse Maybe{Optional,Stream} into Maybe --- .../java/net/kemitix/mon/maybe/Maybe.java | 63 +++++++++++++++- .../net/kemitix/mon/maybe/MaybeOptional.java | 71 ------------------- .../net/kemitix/mon/maybe/MaybeStream.java | 62 ---------------- 3 files changed, 62 insertions(+), 134 deletions(-) delete mode 100644 src/main/java/net/kemitix/mon/maybe/MaybeOptional.java delete mode 100644 src/main/java/net/kemitix/mon/maybe/MaybeStream.java diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index 744068c..bcbf64a 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -24,7 +24,12 @@ package net.kemitix.mon.maybe; import lombok.NonNull; import net.kemitix.mon.Functor; +import java.util.Optional; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.stream.Stream; /** * A value that may or may not be present. @@ -32,7 +37,8 @@ import java.util.function.Function; * @param the type of the content of the Just * @author Paul Campbell (pcampbell@kemitix.net) */ -public interface Maybe extends Functor>, MaybeStream, MaybeOptional { +@SuppressWarnings("methodcount") +public interface Maybe extends Functor> { /** * Create a Maybe for the value that may or may not be present. @@ -72,4 +78,59 @@ public interface Maybe extends Functor>, MaybeStream, MaybeOpt @Override Maybe map(Function f); + + /** + * 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; + + /** + * 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/main/java/net/kemitix/mon/maybe/MaybeOptional.java b/src/main/java/net/kemitix/mon/maybe/MaybeOptional.java deleted file mode 100644 index 44f8700..0000000 --- a/src/main/java/net/kemitix/mon/maybe/MaybeOptional.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * 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 deleted file mode 100644 index 4036ee8..0000000 --- a/src/main/java/net/kemitix/mon/maybe/MaybeStream.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * 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); - -}