Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peekError and peekSuccess methods #3

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Result-type for Java
====================

The project provides Result-type similar to Result-type in Rust that
allows to return either successfull result or otherwise some kind of error.
allows to return either successful result or otherwise some kind of error.

In Java, the native way of reporting errors are exceptions, either checked or unchecked.
You do not need Result-type most of the time in Java-code, where
Expand All @@ -13,7 +13,7 @@ Handling exception in such situations can be cumbersome and require a lot of boi
Result-type and associated helper-classes help with exception handling and
allow to write idiomatic functional code that can interact with methods that throw exceptions.

Result-type provides a way to pass error enformation as a first-class value through
Result-type provides a way to pass error information as a first-class value through
the code written in functional style.
Routines are provided for interoperability of normal code that uses exception and
functional code that uses Result-type, so that exceptions can be caught and propagated as
Expand Down
51 changes: 40 additions & 11 deletions src/main/java/com/github/sviperll/result4j/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public sealed interface Result<R, E> {
* Produces function-object that transforms error-values of results.
* <p>
* Similarly to {@link Result#mapError(Function)},
* this method allows to apply a transformation to a error-value, associated with a result,
* this method allows to apply a transformation to an error-value, associated with a result,
* to get a new result.
* In contrast to {@link Result#mapError(Function)},
* instead of applying the transformation to some particular result,
Expand Down Expand Up @@ -280,7 +280,7 @@ static <R, E> Result<R, E> error(E error) {
* @param <R> type of successful result value
* @param <E> type representing error-value
* @param value {@code Optional}-value containing {@code Result}-value
* @return
* @return {@code Result}-value with {@code Optional} successful result value.
*/
static <R, E> Result<Optional<R>, E> fromOptionalResult(
Optional<Result<R, E>> value
Expand All @@ -305,7 +305,8 @@ static <R, E> Result<Optional<R>, E> fromOptionalResult(
* @param <E> type representing error-value
* @param optional {@code Optional}-value
* @param error error value
* @return
* @return {@code Result}-value with {@code Optional} successful result value,
* or given error value.
*/
static <R, E> Result<R, E> fromOptional(Optional<R> optional, E error) {
Optional<Result<R, E>> optionalResult = optional.map(Result::success);
Expand All @@ -325,7 +326,7 @@ static <R, E> Result<R, E> fromOptional(Optional<R> optional, E error) {
* the result of this method is exactly the same as provided argument.
* </ul>
*
* @param <U> type of a successful result of a successor
* @param <U> type of successful result of a successor
* @param result successor result
*/
<U> Result<U, E> andThen(Result<U, E> result);
Expand All @@ -342,15 +343,15 @@ static <R, E> Result<R, E> fromOptional(Optional<R> optional, E error) {
* the result of this method a new result with transformed value.
* </ul>
*
* @param <U> new type of a successful result value
* @param <U> new type of successful result value
* @param transformation transformation to be applied to values
*/
<U> Result<U, E> map(Function<? super R, ? extends U> transformation);

/**
* Transforms an error-value of this result, when this is an error.
* <p>
* this method allows to apply a transformation to a error-value,
* this method allows to apply a transformation to an error-value,
* associated with a result, to get a new result.
* <ul>
* <li>When this result is an error, then
Expand All @@ -359,7 +360,7 @@ static <R, E> Result<R, E> fromOptional(Optional<R> optional, E error) {
* the result of this method the same successful result with the same value.
* </ul>
*
* @param <E1> new type of an error-value
* @param <E1> new type of error-value
* @param transformation transformation to be applied to error-values
*/
<E1> Result<R, E1> mapError(Function<? super E, ? extends E1> transformation);
Expand Down Expand Up @@ -387,21 +388,27 @@ static <R, E> Result<R, E> fromOptional(Optional<R> optional, E error) {
* throws an exception, by creating exception instance from error-value.
*
* @param <X> type of thrown exception
* @param errorToExceptionConvertion function to convert error-value to an exception
* @param errorToExceptionConverter function to convert error-value to an exception
* @return the value of this result, when it is a successful result
* @throws X when this is an error result
*/
default <X extends Exception> R throwError(Function<? super E, X> errorToExceptionConvertion)
default <X extends Exception> R throwError(Function<? super E, X> errorToExceptionConverter)
throws X {
return switch (this) {
case Success<R, E> success -> success.result;
case Error<R, E> error -> throw errorToExceptionConvertion.apply(error.error);
case Error<R, E> error -> throw errorToExceptionConverter.apply(error.error);
};
}

/** Invokes given consumer for a successful result value. */
void ifSuccess(Consumer<R> consumer);

/** Invokes given consumer for an error-value of this result, and returns the same result. */
Result<R, E> peekError(Consumer<E> consumer);

/** Invokes given consumer for a success-value of this result, and returns the same result. */
Result<R, E> peekSuccess(Consumer<R> consumer);

/**
* Transforms a value of this result, when this is a successful result.
* <p>
Expand All @@ -415,7 +422,7 @@ default <X extends Exception> R throwError(Function<? super E, X> errorToExcepti
* the result of this method the result of applying transformation to the argument.
* </ul>
*
* @param <U> new type of a successful result value
* @param <U> new type of successful result value
* @param transformation transformation to be applied to values
*/
default <U> Result<U, E> flatMap(
Expand Down Expand Up @@ -473,6 +480,17 @@ public R recoverError(
public void ifSuccess(Consumer<R> consumer) {
consumer.accept(result);
}

@Override
public Result<R, E> peekError(Consumer<E> consumer) {
return this;
}

@Override
public Result<R, E> peekSuccess(Consumer<R> consumer) {
consumer.accept(result);
return this;
}
}

record Error<R, E>(E error) implements Result<R, E> {
Expand Down Expand Up @@ -520,5 +538,16 @@ public R recoverError(
@Override
public void ifSuccess(Consumer<R> consumer) {
}

@Override
public Result<R, E> peekError(Consumer<E> consumer) {
consumer.accept(error);
return this;
}

@Override
public Result<R, E> peekSuccess(Consumer<R> consumer) {
return this;
}
}
}