diff --git a/README.md b/README.md index c12ccf5..49188ad 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/main/java/com/github/sviperll/result4j/Result.java b/src/main/java/com/github/sviperll/result4j/Result.java index 184f5ee..fdfab68 100644 --- a/src/main/java/com/github/sviperll/result4j/Result.java +++ b/src/main/java/com/github/sviperll/result4j/Result.java @@ -116,7 +116,7 @@ public sealed interface Result { * Produces function-object that transforms error-values of results. *

* 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, @@ -280,7 +280,7 @@ static Result error(E error) { * @param type of successful result value * @param 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 Result, E> fromOptionalResult( Optional> value @@ -305,7 +305,8 @@ static Result, E> fromOptionalResult( * @param 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 Result fromOptional(Optional optional, E error) { Optional> optionalResult = optional.map(Result::success); @@ -325,7 +326,7 @@ static Result fromOptional(Optional optional, E error) { * the result of this method is exactly the same as provided argument. * * - * @param type of a successful result of a successor + * @param type of successful result of a successor * @param result successor result */ Result andThen(Result result); @@ -342,7 +343,7 @@ static Result fromOptional(Optional optional, E error) { * the result of this method a new result with transformed value. * * - * @param new type of a successful result value + * @param new type of successful result value * @param transformation transformation to be applied to values */ Result map(Function transformation); @@ -350,7 +351,7 @@ static Result fromOptional(Optional optional, E error) { /** * Transforms an error-value of this result, when this is an error. *

- * 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. *

* - * @param new type of an error-value + * @param new type of error-value * @param transformation transformation to be applied to error-values */ Result mapError(Function transformation); @@ -387,21 +388,27 @@ static Result fromOptional(Optional optional, E error) { * throws an exception, by creating exception instance from error-value. * * @param 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 R throwError(Function errorToExceptionConvertion) + default R throwError(Function errorToExceptionConverter) throws X { return switch (this) { case Success success -> success.result; - case Error error -> throw errorToExceptionConvertion.apply(error.error); + case Error error -> throw errorToExceptionConverter.apply(error.error); }; } /** Invokes given consumer for a successful result value. */ void ifSuccess(Consumer consumer); + /** Invokes given consumer for an error-value of this result, and returns the same result. */ + Result peekError(Consumer consumer); + + /** Invokes given consumer for a success-value of this result, and returns the same result. */ + Result peekSuccess(Consumer consumer); + /** * Transforms a value of this result, when this is a successful result. *

@@ -415,7 +422,7 @@ default R throwError(Function errorToExcepti * the result of this method the result of applying transformation to the argument. * * - * @param new type of a successful result value + * @param new type of successful result value * @param transformation transformation to be applied to values */ default Result flatMap( @@ -473,6 +480,17 @@ public R recoverError( public void ifSuccess(Consumer consumer) { consumer.accept(result); } + + @Override + public Result peekError(Consumer consumer) { + return this; + } + + @Override + public Result peekSuccess(Consumer consumer) { + consumer.accept(result); + return this; + } } record Error(E error) implements Result { @@ -520,5 +538,16 @@ public R recoverError( @Override public void ifSuccess(Consumer consumer) { } + + @Override + public Result peekError(Consumer consumer) { + consumer.accept(error); + return this; + } + + @Override + public Result peekSuccess(Consumer consumer) { + return this; + } } }