Collapse Maybe{Optional,Stream} into Maybe
This commit is contained in:
parent
7a150066a9
commit
2211182c7d
3 changed files with 62 additions and 134 deletions
|
@ -24,7 +24,12 @@ package net.kemitix.mon.maybe;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import net.kemitix.mon.Functor;
|
import net.kemitix.mon.Functor;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
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.
|
* A value that may or may not be present.
|
||||||
|
@ -32,7 +37,8 @@ import java.util.function.Function;
|
||||||
* @param <T> the type of the content of the Just
|
* @param <T> the type of the content of the Just
|
||||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
public interface Maybe<T> extends Functor<T, Maybe<?>>, MaybeStream<T>, MaybeOptional<T> {
|
@SuppressWarnings("methodcount")
|
||||||
|
public interface Maybe<T> extends Functor<T, Maybe<?>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Maybe for the value that may or may not be present.
|
* Create a Maybe for the value that may or may not be present.
|
||||||
|
@ -72,4 +78,59 @@ public interface Maybe<T> extends Functor<T, Maybe<?>>, MaybeStream<T>, MaybeOpt
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
<R> Maybe<R> map(Function<T, R> f);
|
<R> Maybe<R> map(Function<T, R> f);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 <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;
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 <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);
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue