Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface CheckedConsumer<T>
* @throws Exception if the consume operation fails.
*/

void accept(T t) throws Exception;
T accept(T t) throws Exception;

}
15 changes: 11 additions & 4 deletions endeavour/src/main/java/org/saltations/endeavour/Failure.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,24 @@ public <U> Result<U> flatMap(CheckedFunction<T, Result<U>> mapping)
}

@Override
public Result<T> ifSuccess(CheckedConsumer<Success<T>> action) throws Exception
public Result<T> ifSuccess(CheckedConsumer<Success<T>> action)
{
return this;
}

@Override
public Result<T> ifFailure(CheckedConsumer<Failure<T>> action) throws Exception
public Result<T> ifFailure(CheckedConsumer<Failure<T>> action)
{
Objects.requireNonNull(action, "Action cannot be null");
action.accept(this);
return this;

try {
return action.accept(this);
} catch (Exception ex) {
return new Failure<>(FailureDescription.of()
.type(FailureDescription.GenericFailureType.GENERIC_EXCEPTION)
.cause(ex)
.build());
}
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions endeavour/src/main/java/org/saltations/endeavour/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public sealed interface Result<T> permits Failure, Success
* }
*/

Result<T> ifSuccess(@NonNull CheckedConsumer<Success<T>> action) throws Exception;
Result<T> ifSuccess(@NonNull CheckedConsumer<Success<T>> action);

/**
* Executes action if this outcome is a failure, takes no action otherwise.
Expand All @@ -150,7 +150,7 @@ public sealed interface Result<T> permits Failure, Success
* }
*/

Result<T> ifFailure(@NonNull CheckedConsumer<Failure<T>> action) throws Exception;
Result<T> ifFailure(@NonNull CheckedConsumer<Failure<T>> action);

/**
* Returns the alternate result if this outcome is a failure, otherwise returns the existing outcome.
Expand Down
16 changes: 12 additions & 4 deletions endeavour/src/main/java/org/saltations/endeavour/Success.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
public sealed interface Success<T> extends Result<T> permits QuantSuccess, QualSuccess {


default Result<T> ifSuccess(CheckedConsumer<Success<T>> action) throws Exception
default Result<T> ifSuccess(CheckedConsumer<Success<T>> action)
{
Objects.requireNonNull(action, "Action cannot be null");
action.accept(this);
return this;


try {
return action.accept(this);
} catch (Exception ex) {
return new Failure<>(FailureDescription.of()
.type(FailureDescription.GenericFailureType.GENERIC_EXCEPTION)
.cause(ex)
.build());
}
}

default Result<T> ifFailure(CheckedConsumer<Failure<T>> action) throws Exception
default Result<T> ifFailure(CheckedConsumer<Failure<T>> action)
{
Objects.requireNonNull(action, "Action cannot be null");
return this;
Expand Down
27 changes: 19 additions & 8 deletions endeavour/src/test/java/org/saltations/endeavour/FailureTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.saltations.endeavour;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.ClassOrderer;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -13,7 +12,7 @@
import org.saltations.endeavour.fixture.ReplaceBDDCamelCase;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.saltations.endeavour.fixture.ResultAssert.assertThat;
Expand Down Expand Up @@ -90,17 +89,29 @@ void whenReducingFailureThenCallsFailureFunction()
@Test
@Order(60)
void whenIfSuccessThenDoesNotTakeAction() throws Exception {
final AtomicBoolean applied = new AtomicBoolean(false);
failure.ifSuccess(x -> applied.getAndSet(true));
assertFalse(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = failure.ifSuccess(x -> {
resultBuilder.append("Processed: ").append(x.get());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("", resultBuilder.toString(), "Action not taken");
assertSame(failure, result, "Should return same result");
}

@Test
@Order(61)
void whenIfFailureThenTakesAction() throws Exception {
final AtomicBoolean applied = new AtomicBoolean(false);
failure.ifFailure(x -> applied.getAndSet(true));
assertTrue(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = failure.ifFailure(x -> {
resultBuilder.append("Processed: ").append(x.getTitle());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertTrue(resultBuilder.length() > 0, "Action taken");
assertSame(failure, result, "Should return same result");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,31 @@ void whenReducingThenReturnsSuccessValue()
@Order(60)
void whenTakingActionIfSuccessThenTakesAction() throws Exception
{
final AtomicBoolean applied = new AtomicBoolean(false);

qualSuccess.ifSuccess(x -> applied.getAndSet(true));
assertTrue(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = qualSuccess.ifSuccess(x -> {
resultBuilder.append("Processed: ").append(x.hasPayload() ? "with payload" : "no payload");
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("Processed: no payload", resultBuilder.toString(), "Action taken");
assertSame(qualSuccess, result, "Should return same result");
}


@Test
@Order(61)
void whenTakingActionIfFailureThenTakesNoAction() throws Exception
{
final AtomicBoolean applied = new AtomicBoolean(false);

qualSuccess.ifFailure(x -> applied.getAndSet(true));
assertFalse(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = qualSuccess.ifFailure(x -> {
resultBuilder.append("Processed: ").append(x.getTitle());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("", resultBuilder.toString(), "Action not taken");
assertSame(qualSuccess, result, "Should return same result");
}


Expand All @@ -141,10 +151,15 @@ void whenOrElseThenReturnsSuppliedResult() throws Throwable
@Order(70)
void whenTakingActionOnFailureThenTakesNoAction() throws Exception
{
final AtomicBoolean applied = new AtomicBoolean(false);

qualSuccess.ifFailure(x -> applied.getAndSet(true));
assertFalse(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = qualSuccess.ifFailure(x -> {
resultBuilder.append("Processed: ").append(x.getTitle());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("", resultBuilder.toString(), "Action not taken");
assertSame(qualSuccess, result, "Should return same result");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ void whenMappingPayloadOnSuccessToNullThenReturnsNoValue()
@Order(40)
void whenTakingActionOnSuccessThenTakesAction() throws Exception
{
final AtomicBoolean applied = new AtomicBoolean(false);

value.ifSuccess(x -> applied.getAndSet(true));
assertTrue(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = value.ifSuccess(x -> {
resultBuilder.append("Processed: ").append(x.get());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("Processed: 1111", resultBuilder.toString(), "Action taken");
assertSame(value, result, "Should return same result");
}

@Test
Expand All @@ -155,10 +160,15 @@ void whenTransformingResultOnFailureThenReturnsExistingSuccess()
@Order(70)
void whenTakingActionOnFailureThenTakesNoAction() throws Exception
{
final AtomicBoolean applied = new AtomicBoolean(false);

value.ifFailure(x -> applied.getAndSet(true));
assertFalse(applied.get(), "Action taken");
final StringBuilder resultBuilder = new StringBuilder();

var result = value.ifFailure(x -> {
resultBuilder.append("Processed: ").append(x.getTitle());
return x; // Return the value to satisfy CheckedConsumer contract
});

assertEquals("", resultBuilder.toString(), "Action not taken");
assertSame(value, result, "Should return same result");
}

@Test
Expand Down