Split Maybe interface into MaybeOptional and MaybeStream
Also deprecated Maybe.fromOptional(Optional)
This commit is contained in:
parent
24a12cbf5b
commit
d6d17a6682
5 changed files with 143 additions and 64 deletions
|
@ -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
|
||||
-----
|
||||
|
|
|
@ -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<T> extends Functor<T, Maybe<?>> {
|
||||
public interface Maybe<T> extends Functor<T, Maybe<?>>, MaybeStream<T>, MaybeOptional<T> {
|
||||
|
||||
/**
|
||||
* Create a Maybe for the value that is present.
|
||||
|
@ -85,69 +81,14 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
|
|||
* @param <T> 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 <T> Maybe<T> fromOptional(final Optional<T> 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<T> 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<T> filter(Predicate<T> predicate);
|
||||
|
||||
/**
|
||||
* Convert the Maybe to an {@link Optional}.
|
||||
*
|
||||
* @return an Optional containing a value for a Just, or empty for a Nothing
|
||||
*/
|
||||
Optional<T> 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<T> peek(Consumer<T> 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<Exception> 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<T> stream();
|
||||
}
|
||||
|
|
71
src/main/java/net/kemitix/mon/maybe/MaybeOptional.java
Normal file
71
src/main/java/net/kemitix/mon/maybe/MaybeOptional.java
Normal file
|
@ -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 <T> the type of the content of the Just
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
interface MaybeOptional<T> {
|
||||
|
||||
/**
|
||||
* Provide a value to use when Maybe is Nothing.
|
||||
*
|
||||
* @param supplier supplier for an alternate value
|
||||
*
|
||||
* @return a Maybe
|
||||
*/
|
||||
T orElseGet(Supplier<T> 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<T> 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<Exception> e) throws Exception;
|
||||
|
||||
}
|
62
src/main/java/net/kemitix/mon/maybe/MaybeStream.java
Normal file
62
src/main/java/net/kemitix/mon/maybe/MaybeStream.java
Normal file
|
@ -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 <T> the type of the content of the Just
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
interface MaybeStream<T> {
|
||||
|
||||
/**
|
||||
* Converts the Maybe into either a single value stream or and empty stream.
|
||||
*
|
||||
* @return a Stream containing the value or nothing.
|
||||
*/
|
||||
Stream<T> 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<T> filter(Predicate<T> 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<T> peek(Consumer<T> consumer);
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue