From c2c65b65d3cfcf7e23759b71635821de7f547b99 Mon Sep 17 00:00:00 2001 From: "z.s.d." Date: Mon, 4 Dec 2023 11:04:07 -0500 Subject: [PATCH] Added isOk/isErr/isNone/isLeft/isRight methods Added tests for above No changes to Try pending new design considerations --- src/main/java/net/xyzsd/dichotomy/Either.java | 44 +++++++++++++++++++ src/main/java/net/xyzsd/dichotomy/Maybe.java | 24 +++++++++- src/main/java/net/xyzsd/dichotomy/Result.java | 36 ++++++++++++++- .../java/net/xyzsd/dichotomy/EitherTest.java | 13 ++++++ .../java/net/xyzsd/dichotomy/MaybeTest.java | 6 +++ .../java/net/xyzsd/dichotomy/ResultTest.java | 12 +++++ 6 files changed, 133 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/xyzsd/dichotomy/Either.java b/src/main/java/net/xyzsd/dichotomy/Either.java index 62c7df2..db7cc82 100644 --- a/src/main/java/net/xyzsd/dichotomy/Either.java +++ b/src/main/java/net/xyzsd/dichotomy/Either.java @@ -67,6 +67,17 @@ static Either ofRight(@NotNull R value) { } + /** + * Returns {@code true} is this is a {@link Left} {@link Either}. + */ + boolean isLeft(); + + /** + * Returns {@code true} is this is a {@link Right} {@link Either}. + */ + boolean isRight(); + + /** * If the {@link Either} is {@link Left}, return the value as an {@link Optional}. * Otherwise, return an empty {@link Optional}. @@ -607,6 +618,22 @@ record Left(@NotNull L value) implements Either { public L get() {return value;} + /** + * {@inheritDoc} + */ + @Override + public boolean isLeft() { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRight() { + return false; + } + /** * {@inheritDoc} */ @@ -943,6 +970,23 @@ record Right(@NotNull R value) implements Either { public R get() {return value;} + /** + * {@inheritDoc} + */ + @Override + public boolean isLeft() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRight() { + return true; + } + + /** * {@inheritDoc} */ diff --git a/src/main/java/net/xyzsd/dichotomy/Maybe.java b/src/main/java/net/xyzsd/dichotomy/Maybe.java index 27230fe..9d3ed06 100644 --- a/src/main/java/net/xyzsd/dichotomy/Maybe.java +++ b/src/main/java/net/xyzsd/dichotomy/Maybe.java @@ -58,6 +58,7 @@ static Maybe ofNullable(@Nullable T value) { return (value == null) ? Maybe.ofNone() : Maybe.of( value ); } + /** * Perform an action depending on whether the {@link Maybe} is a {@link Some} or a {@link None}. *

@@ -161,12 +162,20 @@ static Maybe ofNullable(@Nullable T value) { boolean contains(@Nullable final T value); /** - * True if this is a {@link Some}. + * True if this is a {@link Some} (contains something) * * @return {@code true} if this is a {@link Some} type. */ boolean hasSome(); + + /** + * True if this is a {@link None}. (contains nothing). + * + * @return {@code true} if this is a {@link None} type. + */ + boolean isNone(); + /** * If this is a {@link Some}, return it. Otherwise, use the provided alternate value. * @@ -247,6 +256,7 @@ record Some(@NotNull T value) implements Maybe { requireNonNull( value ); } + @Override public @NotNull Maybe biMatch(@NotNull Consumer someConsumer, @NotNull Runnable noneRunner) { requireNonNull( someConsumer ); @@ -315,6 +325,11 @@ public boolean hasSome() { return true; } + @Override + public boolean isNone() { + return false; + } + @Override public @NotNull T orElse(@NotNull T alternate) { requireNonNull( value ); @@ -380,6 +395,8 @@ private static None empty() { } + + @Override public @NotNull Maybe biMatch(@NotNull Consumer someConsumer, @NotNull Runnable noneRunner) { requireNonNull( someConsumer ); @@ -446,6 +463,11 @@ public boolean hasSome() { return false; } + @Override + public boolean isNone() { + return true; + } + @Override public @NotNull T orElse(@NotNull T alternate) { requireNonNull( alternate ); diff --git a/src/main/java/net/xyzsd/dichotomy/Result.java b/src/main/java/net/xyzsd/dichotomy/Result.java index 6078e14..9b62895 100644 --- a/src/main/java/net/xyzsd/dichotomy/Result.java +++ b/src/main/java/net/xyzsd/dichotomy/Result.java @@ -168,6 +168,21 @@ static Result ofErr(@NotNull E error) { return new Err<>( error ); } + + + + /** + * If this is an {@link OK}, return {@code true}. + */ + boolean isOK(); + + /** + * If this is an {@link Err}, return {@code true}. + */ + boolean isErr(); + + + /** * Get the {@link OK} value V as an {@link Optional}. * @@ -437,7 +452,7 @@ default void consume(@NotNull Consumer okConsumer) { * This is equivalent to: * {@snippet : * // given: - * Result result = Result<>.of(5); + * Result result = Result<>.of(5); * Function mapper = (i) -> 10.0d * i; * * // newResult : 50.0d @@ -715,6 +730,15 @@ record OK(@NotNull V value) implements Result { return value; } + @Override + public boolean isOK() { + return true; + } + + @Override + public boolean isErr() { + return false; + } @Override public @NotNull Optional ok() { @@ -958,6 +982,16 @@ record Err(@NotNull E error) implements Result { } + @Override + public boolean isOK() { + return false; + } + + @Override + public boolean isErr() { + return true; + } + @Override public @NotNull Optional ok() { return Optional.empty(); diff --git a/src/test/java/net/xyzsd/dichotomy/EitherTest.java b/src/test/java/net/xyzsd/dichotomy/EitherTest.java index 46ddeb9..382f2c5 100644 --- a/src/test/java/net/xyzsd/dichotomy/EitherTest.java +++ b/src/test/java/net/xyzsd/dichotomy/EitherTest.java @@ -37,6 +37,19 @@ void fromResult() { assertEquals( LEFT, Conversion.toEither( Result.ofErr( LVAL ) ) ); } + + @Test + void isLeft() { + assertTrue( LEFT.isLeft() ); + assertFalse( RIGHT.isLeft() ); + } + + @Test + void isRight() { + assertFalse( LEFT.isRight() ); + assertTrue( RIGHT.isRight() ); + } + @Test void left() { assertEquals( Optional.of( LVAL ), LEFT.left() ); diff --git a/src/test/java/net/xyzsd/dichotomy/MaybeTest.java b/src/test/java/net/xyzsd/dichotomy/MaybeTest.java index 9791f77..49f6ffa 100644 --- a/src/test/java/net/xyzsd/dichotomy/MaybeTest.java +++ b/src/test/java/net/xyzsd/dichotomy/MaybeTest.java @@ -183,6 +183,12 @@ void hasSome() { assertFalse( MAYBE_NOT.hasSome() ); } + @Test + void isNone() { + assertFalse( MAYBE_YES.isNone() ); + assertTrue( MAYBE_NOT.isNone() ); + } + @Test void orElse() { // assertThrows( NullPointerException.class, () -> MAYBE_YES.orElse( (String)null ) ); diff --git a/src/test/java/net/xyzsd/dichotomy/ResultTest.java b/src/test/java/net/xyzsd/dichotomy/ResultTest.java index 25d1253..023788b 100644 --- a/src/test/java/net/xyzsd/dichotomy/ResultTest.java +++ b/src/test/java/net/xyzsd/dichotomy/ResultTest.java @@ -89,6 +89,18 @@ void ofErr() { assertThrows( NullPointerException.class, () -> {Result.ofErr( null );} ); } + @Test + void isOK() { + assertTrue( OK.isOK() ); + assertFalse( ERR.isOK() ); + } + + @Test + void isErr() { + assertFalse( OK.isErr() ); + assertTrue( ERR.isErr() ); + } + @Test void ok() {