Skip to content

Commit

Permalink
Added isOk/isErr/isNone/isLeft/isRight methods
Browse files Browse the repository at this point in the history
Added tests for above
No changes to Try pending new design considerations
  • Loading branch information
xyzsd committed Dec 4, 2023
1 parent 299054c commit c2c65b6
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/main/java/net/xyzsd/dichotomy/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ static <L, R> Either<L, R> 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}.
Expand Down Expand Up @@ -607,6 +618,22 @@ record Left<L, R>(@NotNull L value) implements Either<L, R> {
public L get() {return value;}


/**
* {@inheritDoc}
*/
@Override
public boolean isLeft() {
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isRight() {
return false;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -943,6 +970,23 @@ record Right<L, R>(@NotNull R value) implements Either<L, R> {
public R get() {return value;}


/**
* {@inheritDoc}
*/
@Override
public boolean isLeft() {
return false;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isRight() {
return true;
}


/**
* {@inheritDoc}
*/
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/net/xyzsd/dichotomy/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static <T> Maybe<T> 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}.
* <p>
Expand Down Expand Up @@ -161,12 +162,20 @@ static <T> Maybe<T> 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 <i>some</i>thing)
*
* @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.
*
Expand Down Expand Up @@ -247,6 +256,7 @@ record Some<T>(@NotNull T value) implements Maybe<T> {
requireNonNull( value );
}


@Override
public @NotNull Maybe<T> biMatch(@NotNull Consumer<? super T> someConsumer, @NotNull Runnable noneRunner) {
requireNonNull( someConsumer );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -380,6 +395,8 @@ private static <U> None<U> empty() {
}




@Override
public @NotNull Maybe<T> biMatch(@NotNull Consumer<? super T> someConsumer, @NotNull Runnable noneRunner) {
requireNonNull( someConsumer );
Expand Down Expand Up @@ -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 );
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/net/xyzsd/dichotomy/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ static <V, E> Result<V, E> 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}.
*
Expand Down Expand Up @@ -437,7 +452,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
* This is equivalent to:
* {@snippet :
* // given:
* Result<String,Integer> result = Result<>.of(5);
* Result<String, Integer> result = Result<>.of(5);
* Function<Integer,Double> mapper = (i) -> 10.0d * i;
*
* // newResult : 50.0d
Expand Down Expand Up @@ -715,6 +730,15 @@ record OK<V, E>(@NotNull V value) implements Result<V, E> {
return value;
}

@Override
public boolean isOK() {
return true;
}

@Override
public boolean isErr() {
return false;
}

@Override
public @NotNull Optional<V> ok() {
Expand Down Expand Up @@ -958,6 +982,16 @@ record Err<V, E>(@NotNull E error) implements Result<V, E> {
}


@Override
public boolean isOK() {
return false;
}

@Override
public boolean isErr() {
return true;
}

@Override
public @NotNull Optional<V> ok() {
return Optional.empty();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/net/xyzsd/dichotomy/EitherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/net/xyzsd/dichotomy/MaybeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/net/xyzsd/dichotomy/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c2c65b6

Please sign in to comment.