Skip to content

Commit

Permalink
More tests. Minor Fixes. JDK 20 target complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzsd committed Dec 23, 2023
1 parent 815fbbc commit fd67497
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 151 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
languageVersion.set(JavaLanguageVersion.of(20))
vendor.set(JvmVendorSpec.ADOPTIUM)
}

Expand All @@ -41,6 +41,7 @@ tasks.getByName<Test>("test") {

tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("-Xlint:preview")
options.compilerArgs.add("-Xlint:unchecked")
}


Expand Down Expand Up @@ -120,7 +121,7 @@ tasks.javadoc {
(options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
}
val javadocOptions = options as CoreJavadocOptions
javadocOptions.addStringOption("source", "21")
javadocOptions.addStringOption("source", "20")
}

// for reproducible builds
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/xyzsd/dichotomy/Conversion.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package net.xyzsd.dichotomy;

import net.xyzsd.dichotomy.Either;
import net.xyzsd.dichotomy.Result;
import net.xyzsd.dichotomy.trying.Try;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;


/**
* Conversion utilities.
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/xyzsd/dichotomy/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public sealed interface Either<L, R> {
/**
* Create a Left {@link Either}.
* <p>
* By convention, the left-sided value is the abnormal / unexpected / error value.
* By convention, the left-sided value is the abnormal / unexpected / value value.
* </p>
*
* @param value left value
Expand Down Expand Up @@ -218,7 +218,7 @@ static <L, R> Either<L, R> ofRight(@NotNull R value) {


/**
* Recover from an error; ignore the {@link Left} value if present,
* Recover from an value; ignore the {@link Left} value if present,
* and apply the mapping function to get a {@link Right}.
* <p>
* If this is a {@link Right}, return it without applying the mapping function.
Expand Down Expand Up @@ -569,7 +569,7 @@ default void consume(@NotNull Consumer<? super R> rightConsumer) {
* </p>
* <p>
* This method is particularly useful to convert Exception types, or to create Exceptions with
* error messages dependent upon the {@link Left} value.
* value messages dependent upon the {@link Left} value.
* </p>
* <p>
* For example:
Expand All @@ -589,7 +589,7 @@ default void consume(@NotNull Consumer<? super R> rightConsumer) {
*
* // regarding getOrThrow(ArithmeticException::new)
* // this works because there is an ArithmeticException(String) constructor;
* // otherwise a compile-time error would occur (no appropriate constructor to match)
* // otherwise a compile-time value would occur (no appropriate constructor to match)
*
* Either<String,Integer> myEitherBad = anExampleMethod(-37234);
* int badValue = myEitherGood.getOrThrow(ArithmeticException::new);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/xyzsd/dichotomy/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static <T> Maybe<T> ofNullable(@Nullable T value) {
/**
* Perform an action depending on whether the {@link Maybe} is a {@link Some} or a {@link None}.
* <p>
* This is analagous to {@link java.util.Optional#ifPresentOrElse(Consumer, Runnable)}
* This is analogous to {@link java.util.Optional#ifPresentOrElse(Consumer, Runnable)}
*
* @param someConsumer action performed if {@link Some}
* @param noneRunner action performed if {@link None}
Expand Down Expand Up @@ -389,7 +389,7 @@ public boolean isNone() {
/**
* The empty {@link Maybe}.
* <p>
* This None has a type (unliked net.xyzsd.dichotomy.None), but the type is always coerced to the needed type.
* This None has a type which s always coerced to the needed type.
*
* @param <T> value type
*/
Expand Down
80 changes: 40 additions & 40 deletions src/main/java/net/xyzsd/dichotomy/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* {@link Result}, and can wrap methods producing checked or unchecked {@link Exception}s.
*
* @param <V> The Success type.
* @param <E> THe Failure (error) type. Does not have to be an Exception.
* @param <E> THe Failure (value) type. Does not have to be an Exception.
*/
public sealed interface Result<V, E> {

Expand Down Expand Up @@ -113,7 +113,7 @@ static <E> Result<Empty, E> ofOK() {
* non-{@code null} values are failure) use {@link #swap()}, as follows:
* <pre>
* {@code
* // Lets say RuntimeException is the error type
* // Lets say RuntimeException is the value type
* Result<None, RuntimeException> result = Result.ofNullable(myValue).swap();
* }
* </pre>
Expand All @@ -139,7 +139,7 @@ static <V> Result<V, Empty> ofNullable(@Nullable V value) {
* If the opposite behavior is desired ({@code null} is successful, and
* non-{@code null} values are failure) use {@link #swap()}, as follows:
* {@snippet :
* // Lets say RuntimeException is the error type
* // Lets say RuntimeException is the value type
* Optional<RuntimeException> myValue = someFunction();
* Result<None, RuntimeException> result = Result.ofOK(myValue).swap();
* }
Expand Down Expand Up @@ -177,7 +177,7 @@ static <V, E> Result<V, E> ofErr(@NotNull E error) {

/**
* If this is an {@link Err}, return {@code true}.
* @return true if this is an error result.
* @return true if this is an value result.
*/
boolean isErr();

Expand Down Expand Up @@ -226,7 +226,7 @@ static <V, E> Result<V, E> ofErr(@NotNull E error) {
*
* @param okMapper the mapping function for {@link OK} values.
* @param errMapper the mapping function for {@link Err} values.
* @param <E2> type of the error, which can be different from the original type
* @param <E2> type of the value, which can be different from the original type
* @param <V2> type of the value, which can be different from the original type
* @return the {@link Result} produced from {@code okMapper} or {@code errMapper}
* @throws NullPointerException if the called function returns {@code null}.
Expand All @@ -243,7 +243,7 @@ static <V, E> Result<V, E> ofErr(@NotNull E error) {
*
* @param fnOK the mapping function for {@link Err} values.
* @param fnErr the mapping function for {@link OK} values.
* @param <E2> type of the error, which can be different from the original type
* @param <E2> type of the value, which can be different from the original type
* @param <V2> type of the value, which can be different from the original type
* @return the {@link Result} produced from {@code fnOK} or {@code fnErr}
* @throws NullPointerException if the called function returns {@code null}.
Expand Down Expand Up @@ -417,7 +417,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
@NotNull V orElse(@NotNull Supplier<? extends V> okSupplier);

/**
* Recover from an error; ignore the {@link Err} value if present,
* Recover from an value; ignore the {@link Err} value if present,
* and apply the mapping function to get an {@link OK}.
* <p>
* If this is an {@link OK}, return it without applying the mapping function.
Expand Down Expand Up @@ -481,7 +481,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
*}
*
* @param errMapper the mapping function producing a new {@link Err} value.
* @param <E2> type of the error, which can be different from the original type
* @param <E2> type of the value, which can be different from the original type
* @return a new {@link Err} produced by the mapping function if this is {@link Err};
* otherwise, returns an {@link OK}.
* @throws NullPointerException if any argument is null, or if the called action returns {@code null}.
Expand All @@ -502,7 +502,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
* </p>
*
* @param errMapper the mapping function that produces a new {@link Result}
* @param <E2> type of the error, which can be different from the original type
* @param <E2> type of the value, which can be different from the original type
* @return a new {@link Err} produced by the mapping function if this is {@link Err};
* otherwise, returns an {@link OK}.
* @throws NullPointerException if any argument is null, or if the called action returns {@code null}.
Expand Down Expand Up @@ -615,7 +615,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
* The next {@link Result} can have a different parameterized {@link Err} type.
*
* @param nextResult The {@link Result} to return.
* @param <E2> type of the error,
* @param <E2> type of the value,
* which can be different from the original type
* @see #or(Supplier)
* @see #and(Result)
Expand All @@ -630,7 +630,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
* The next {@link Result} can have a different parameterized {@link Err} type.
*
* @param nextResultSupplier The supplier of a {@link Result} to return; only called if {@code this} is {@link Err}.
* @param <E2> type of the error,
* @param <E2> type of the value,
* which can be different from the original type
* @throws NullPointerException if the supplier is called and returns {@code null}.
* @see #or(Result)
Expand Down Expand Up @@ -697,7 +697,7 @@ default void consume(@NotNull Consumer<? super V> okConsumer) {
* }
* </pre>
* <p>
* If the exception type does not wrap a given error type:
* If the exception type does not wrap a given value type:
* {@code
* fileResult.orThrow((err) -> new MyException());
* }
Expand Down Expand Up @@ -947,9 +947,9 @@ public boolean containsErr(@Nullable E errValue) {
}


// For types where the error type is unchanged and exists, but the generic type of the value differs
// For types where the value type is unchanged and exists, but the generic type of the value differs
// just cast and return. Types are erased so there is no need to create a new object.
// The value stays the same, only the empty error signature changes
// The value stays the same, only the empty value signature changes
@SuppressWarnings("unchecked")
@NotNull
private <E2> Result<V, E2> coerce() {
Expand All @@ -960,19 +960,19 @@ private <E2> Result<V, E2> coerce() {
/**
* Error Result.
*
* @param error Error value
* @param value Error value
* @param <V> OK type
* @param <E> Error type
*/
record Err<V, E>(@NotNull E error) implements Result<V, E> {
record Err<V, E>(@NotNull E value) implements Result<V, E> {

/**
* Create an OK with the given non-null value.
*
* @param error OK Value
* @param value OK Value
*/
public Err {
requireNonNull( error, "Err: error cannot be null!" );
requireNonNull( value, "Err: value cannot be null!" );
}


Expand All @@ -982,7 +982,7 @@ record Err<V, E>(@NotNull E error) implements Result<V, E> {
* @return unwrapped Err E
*/
public @NotNull E get() {
return error;
return value;
}


Expand All @@ -1003,42 +1003,42 @@ public boolean isErr() {

@Override
public @NotNull Optional<E> err() {
return Optional.of( error );
return Optional.of( value );
}

@Override
public @NotNull Result<E, V> swap() {
return new OK<>( error );
return new OK<>( value );
}

@Override
public @NotNull Result<V, E> biMatch(@NotNull Consumer<? super V> okConsumer, @NotNull Consumer<? super E> errConsumer) {
requireNonNull( okConsumer );
requireNonNull( errConsumer );
errConsumer.accept( error );
errConsumer.accept( value );
return this;
}

@Override
public @NotNull <V2, E2> Result<V2, E2> biMap(@NotNull Function<? super V, ? extends V2> okMapper, @NotNull Function<? super E, ? extends E2> errMapper) {
requireNonNull( okMapper );
requireNonNull( errMapper );
return new Err<>( errMapper.apply( error ) );
return new Err<>( errMapper.apply( value ) );
}

@SuppressWarnings("unchecked")
@Override
public @NotNull <V2, E2> Result<V2, E2> biFlatMap(@NotNull Function<? super V, ? extends Result<? extends V2, ? extends E2>> okMapper, @NotNull Function<? super E, ? extends Result<? extends V2, ? extends E2>> errMapper) {
requireNonNull( okMapper );
requireNonNull( errMapper );
return (Result<V2, E2>) requireNonNull( errMapper.apply( error ) );
return (Result<V2, E2>) requireNonNull( errMapper.apply( value ) );
}

@Override
public <T> @NotNull T fold(@NotNull Function<? super V, ? extends T> fnOK, @NotNull Function<? super E, ? extends T> fnErr) {
requireNonNull( fnOK );
requireNonNull( fnErr );
return requireNonNull( fnErr.apply( error ) );
return requireNonNull( fnErr.apply( value ) );
}

@Override
Expand Down Expand Up @@ -1098,62 +1098,62 @@ public boolean contains(@Nullable V okValue) {
@Override
public @NotNull V recover(@NotNull Function<? super E, ? extends V> fnE2V) {
requireNonNull( fnE2V );
return requireNonNull( fnE2V.apply( error ) );
return requireNonNull( fnE2V.apply( value ) );
}

@Override
public @NotNull Stream<E> streamErr() {
return Stream.of( error );
return Stream.of( value );
}

@Override
public @NotNull Result<V, E> matchErr(@NotNull Consumer<? super E> errConsumer) {
requireNonNull( errConsumer );
errConsumer.accept( error );
errConsumer.accept( value );
return this;
}

@Override
public @NotNull <E2> Result<V, E2> mapErr(@NotNull Function<? super E, ? extends E2> errMapper) {
requireNonNull( errMapper );
return new Err<>( errMapper.apply( error ) );
return new Err<>( errMapper.apply( value ) );
}

@SuppressWarnings("unchecked")
@Override
public @NotNull <E2> Result<V, E2> flatMapErr(@NotNull Function<? super E, ? extends Result<? extends V, ? extends E2>> errMapper) {
requireNonNull( errMapper );
return (Result<V, E2>) requireNonNull( errMapper.apply( error ) );
return (Result<V, E2>) requireNonNull( errMapper.apply( value ) );
}

@Override
public boolean ifPredicateErr(@NotNull Predicate<E> errPredicate) {
requireNonNull( errPredicate );
return errPredicate.test( error );
return errPredicate.test( value );
}

@Override
public boolean containsErr(@Nullable E errValue) {
requireNonNull( errValue );
return Objects.equals( error, errValue );
return Objects.equals( value, errValue );
}

@Override
public @NotNull E orElseErr(@NotNull E errAlternate) {
requireNonNull( errAlternate );
return error;
return value;
}

@Override
public @NotNull E orElseErr(@NotNull Supplier<? extends E> errSupplier) {
requireNonNull( errSupplier );
return error;
return value;
}

@Override
public @NotNull E forfeit(@NotNull Function<? super V, ? extends E> fnV2E) {
requireNonNull( fnV2E );
return error;
return value;
}

@Override
Expand Down Expand Up @@ -1183,22 +1183,22 @@ public boolean containsErr(@Nullable E errValue) {
@Override
public @NotNull V expect() throws RuntimeException {
// TODO: when pattern-switch is out of preview, convert this code
if (error instanceof RuntimeException e) {
if (value instanceof RuntimeException e) {
throw e;
} else if (error instanceof Throwable t) {
} else if (value instanceof Throwable t) {
throw new NoSuchElementException( t );
} else {
throw new NoSuchElementException( String.valueOf( error ) );
throw new NoSuchElementException( String.valueOf( value ) );
}
}

@Override
public <X extends Exception> @NotNull V getOrThrow(@NotNull Function<E, X> exFn) throws X {
requireNonNull( exFn );
throw requireNonNull( exFn.apply( error ) );
throw requireNonNull( exFn.apply( value ) );
}

// For types where the error type is unchanged and exists, but the generic type of the value differs
// For types where the value type is unchanged and exists, but the generic type of the value differs
// just cast and return. Types are erased so there is no need to create a new object.
// The Error stays the same; only the empty value signature changes
@SuppressWarnings("unchecked")
Expand Down
Loading

0 comments on commit fd67497

Please sign in to comment.