Skip to content

Commit

Permalink
Switch ExecutionResult to use accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Jan 18, 2019
1 parent 2b32bd8 commit 8c2c465
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/jodah/failsafe/AbstractExecution.java
Expand Up @@ -75,8 +75,8 @@ private void buildPolicyExecutor(Policy policy) {
void record(ExecutionResult result) {
Assert.state(!completed, "Execution has already been completed");
attempts++;
lastResult = result.result;
lastFailure = result.failure;
lastResult = result.getResult();
lastFailure = result.getFailure();
}

void preExecute() {
Expand All @@ -92,8 +92,8 @@ synchronized boolean postExecute(ExecutionResult result) {
for (PolicyExecutor<Policy<Object>> policyExecutor : policyExecutors)
result = policyExecutor.postExecute(result);

waitNanos = result.waitNanos;
completed = result.completed;
waitNanos = result.getWaitNanos();
completed = result.isComplete();
return completed;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/jodah/failsafe/AbstractPolicy.java
Expand Up @@ -123,7 +123,7 @@ public S handleResultIf(Predicate<R> resultPredicate) {
* @see #handleResultIf(Predicate)
*/
boolean isFailure(ExecutionResult result) {
return failureConditions.isEmpty() ? result.failure != null : isFailure((R) result.result, result.failure);
return failureConditions.isEmpty() ? result.getFailure() != null : isFailure((R) result.getResult(), result.getFailure());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/jodah/failsafe/AsyncExecution.java
Expand Up @@ -194,7 +194,7 @@ private void complete(ExecutionResult result, Throwable error) {
completed = true;
if (!future.isDone() && !future.isCancelled()) {
if (result != null) {
future.complete(result.result, result.failure);
future.complete(result.getResult(), result.getFailure());
executor.handleComplete(result, this);
} else
future.complete(null, error);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/jodah/failsafe/Execution.java
Expand Up @@ -116,7 +116,7 @@ ExecutionResult executeSync(Supplier<ExecutionResult> supplier) {
supplier = policyExecutor.supplySync(supplier);

ExecutionResult result = supplier.get();
completed = result.completed;
completed = result.isComplete();
executor.handleComplete(result, this);
return result;
}
Expand Down
47 changes: 35 additions & 12 deletions src/main/java/net/jodah/failsafe/ExecutionResult.java
Expand Up @@ -7,23 +7,22 @@
*
* @author Jonathan Halterman
*/
@SuppressWarnings("WeakerAccess")
public class ExecutionResult {
/** An execution that was completed with a non-result */
static final ExecutionResult NONE = new ExecutionResult(null, null, true, 0, true, true, true);

/** The execution result, if any */
public final Object result;
private final Object result;
/** The execution failure, if any */
public final Throwable failure;
private final Throwable failure;
/** Whether the result represents a non result rather than a {@code null} result */
public final boolean nonResult;
private final boolean nonResult;
/** The amount of time to wait prior to the next execution, according to the policy */
public final long waitNanos;
private final long waitNanos;
/** Whether a policy has completed handling of the execution */
public final boolean completed;
private final boolean complete;
/** Whether a policy determined the execution to be a success */
public final boolean success;
private final boolean success;
/** Whether all policies determined the execution to be a success */
private final Boolean successAll;

Expand All @@ -34,13 +33,13 @@ public ExecutionResult(Object result, Throwable failure) {
this(result, failure, false, 0, false, failure == null, null);
}

private ExecutionResult(Object result, Throwable failure, boolean nonResult, long waitNanos, boolean completed,
private ExecutionResult(Object result, Throwable failure, boolean nonResult, long waitNanos, boolean complete,
boolean success, Boolean successAll) {
this.nonResult = nonResult;
this.result = result;
this.failure = failure;
this.waitNanos = waitNanos;
this.completed = completed;
this.complete = complete;
this.success = success;
this.successAll = successAll;
}
Expand All @@ -57,6 +56,30 @@ public static ExecutionResult failure(Throwable failure) {
return new ExecutionResult(null, failure, false, 0, true, false, false);
}

public Object getResult() {
return result;
}

public Throwable getFailure() {
return failure;
}

public long getWaitNanos() {
return waitNanos;
}

public boolean isComplete() {
return complete;
}

public boolean isNonResult() {
return nonResult;
}

public boolean isSuccess() {
return success;
}

/**
* Returns a copy of the ExecutionResult with the {@code result} value, and completed and success set to true.
*/
Expand All @@ -67,8 +90,8 @@ public ExecutionResult withResult(Object result) {
/**
* Returns a copy of the ExecutionResult with the value set to true, else this if nothing has changed.
*/
public ExecutionResult withCompleted() {
return this.completed ?
public ExecutionResult withComplete() {
return this.complete ?
this :
new ExecutionResult(result, failure, nonResult, waitNanos, true, success, successAll);
}
Expand Down Expand Up @@ -96,7 +119,7 @@ boolean getSuccessAll() {
@Override
public String toString() {
return "ExecutionResult[" + "result=" + result + ", failure=" + failure + ", nonResult=" + nonResult
+ ", waitNanos=" + waitNanos + ", completed=" + completed + ", success=" + success + ", successAll="
+ ", waitNanos=" + waitNanos + ", complete=" + complete + ", success=" + success + ", successAll="
+ successAll + ']';
}
}
10 changes: 5 additions & 5 deletions src/main/java/net/jodah/failsafe/FailsafeExecutor.java
Expand Up @@ -349,11 +349,11 @@ private <T> T call(Function<Execution, CheckedSupplier<?>> supplierFn) {
Supplier<ExecutionResult> supplier = Functions.resultSupplierOf(supplierFn.apply(execution), execution);

ExecutionResult result = execution.executeSync(supplier);
if (result.failure != null)
throw result.failure instanceof RuntimeException ?
(RuntimeException) result.failure :
new FailsafeException(result.failure);
return (T) result.result;
if (result.getFailure() != null)
throw result.getFailure() instanceof RuntimeException ?
(RuntimeException) result.getFailure() :
new FailsafeException(result.getFailure());
return (T) result.getResult();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/jodah/failsafe/PolicyExecutor.java
Expand Up @@ -68,7 +68,7 @@ protected ExecutionResult postExecute(ExecutionResult result) {
if (isFailure(result)) {
result = onFailure(result.with(false, false));

if (result.completed && policy instanceof AbstractPolicy) {
if (result.isComplete() && policy instanceof AbstractPolicy) {
AbstractPolicy abstractPolicy = (AbstractPolicy) policy;
if (abstractPolicy.failureListener != null)
abstractPolicy.failureListener.handle(result, execution);
Expand All @@ -77,7 +77,7 @@ protected ExecutionResult postExecute(ExecutionResult result) {
result = result.with(true, true);
onSuccess(result);

if (result.completed && policy instanceof AbstractPolicy) {
if (result.isComplete() && policy instanceof AbstractPolicy) {
AbstractPolicy abstractPolicy = (AbstractPolicy) policy;
if (abstractPolicy.successListener != null)
abstractPolicy.successListener.handle(result, execution);
Expand All @@ -92,12 +92,12 @@ protected ExecutionResult postExecute(ExecutionResult result) {
* not a failure.
*/
protected boolean isFailure(ExecutionResult result) {
if (result.nonResult)
if (result.isNonResult())
return false;
else if (policy instanceof AbstractPolicy)
return ((AbstractPolicy) policy).isFailure(result);
else
return result.failure != null;
return result.getFailure() != null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/jodah/failsafe/PolicyListeners.java
Expand Up @@ -40,7 +40,7 @@ static <R> EventListener ofAttempt(CheckedConsumer<? extends ExecutionAttemptedE
}

default void handle(ExecutionResult result, ExecutionContext context) {
handle(result.result, result.failure, context);
handle(result.getResult(), result.getFailure(), context);
}
}

Expand Down
Expand Up @@ -57,6 +57,6 @@ protected void onSuccess(ExecutionResult result) {
@Override
protected ExecutionResult onFailure(ExecutionResult result) {
policy.recordFailure();
return result.withCompleted();
return result.withComplete();
}
}
Expand Up @@ -31,7 +31,7 @@ public FallbackExecutor(Fallback fallback) {
@SuppressWarnings("unchecked")
protected ExecutionResult onFailure(ExecutionResult result) {
try {
return result.withResult(policy.apply(result.result, result.failure));
return result.withResult(policy.apply(result.getResult(), result.getFailure()));
} catch (Exception e) {
return ExecutionResult.failure(e);
}
Expand Down
Expand Up @@ -68,11 +68,11 @@ protected Supplier<ExecutionResult> supplySync(Supplier<ExecutionResult> supplie
return result;

result = postExecute(result);
if (result.completed)
if (result.isComplete())
return result;

try {
Thread.sleep(TimeUnit.NANOSECONDS.toMillis(result.waitNanos));
Thread.sleep(TimeUnit.NANOSECONDS.toMillis(result.getWaitNanos()));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return ExecutionResult.failure(new FailsafeException(e));
Expand Down Expand Up @@ -103,12 +103,12 @@ public Object call() {
if (result != null) {
if (!retriesExceeded)
result = postExecute(result);
if (retriesExceeded || result.completed)
if (retriesExceeded || result.isComplete())
promise.complete(result);
else if (!future.isDone() && !future.isCancelled()) {
try {
previousResult = result;
future.inject((Future<Object>) scheduler.schedule(this, result.waitNanos, TimeUnit.NANOSECONDS));
future.inject((Future<Object>) scheduler.schedule(this, result.getWaitNanos(), TimeUnit.NANOSECONDS));
} catch (Exception e) {
promise.completeExceptionally(e);
}
Expand Down Expand Up @@ -139,8 +139,8 @@ protected ExecutionResult onFailure(ExecutionResult result) {
// Determine the computed delay
long computedDelayNanos = -1;
DelayFunction<Object, Throwable> delayFunction = (DelayFunction<Object, Throwable>) policy.getDelayFn();
if (delayFunction != null && policy.canApplyDelayFn(result.result, result.failure)) {
Duration computedDelay = delayFunction.computeDelay(result.result, result.failure, execution);
if (delayFunction != null && policy.canApplyDelayFn(result.getResult(), result.getFailure())) {
Duration computedDelay = delayFunction.computeDelay(result.getResult(), result.getFailure(), execution);
if (computedDelay != null && computedDelay.toNanos() >= 0)
computedDelayNanos = computedDelay.toNanos();
}
Expand Down Expand Up @@ -182,10 +182,10 @@ else if (policy.getJitterFactor() > 0.0)
boolean maxRetriesExceeded = policy.getMaxRetries() != -1 && failedAttempts > policy.getMaxRetries();
boolean maxDurationExceeded = policy.getMaxDuration() != null && elapsedNanos > policy.getMaxDuration().toNanos();
retriesExceeded = maxRetriesExceeded || maxDurationExceeded;
boolean isAbortable = policy.isAbortable(result.result, result.failure);
boolean shouldRetry = !result.success && !isAbortable && !retriesExceeded && policy.allowsRetries();
boolean isAbortable = policy.isAbortable(result.getResult(), result.getFailure());
boolean shouldRetry = !result.isSuccess() && !isAbortable && !retriesExceeded && policy.allowsRetries();
boolean completed = isAbortable || !shouldRetry;
boolean success = completed && result.success && !isAbortable;
boolean success = completed && result.isSuccess() && !isAbortable;

// Call attempt listeners
if (failedAttemptListener != null && !success)
Expand Down

0 comments on commit 8c2c465

Please sign in to comment.