/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package net.jodah.failsafe;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Contextual execution information.
 *
 * @author Jonathan Halterman
 */
public class ExecutionContext {
  volatile Duration startTime = Duration.ZERO;
  volatile Duration attemptStartTime = Duration.ZERO;
  /** Number of execution attempts */
  AtomicInteger attempts = new AtomicInteger();

  // Internally mutable state
  volatile boolean cancelled;
  volatile Object lastResult;
  volatile Throwable lastFailure;

  ExecutionContext() {
  }

  private ExecutionContext(ExecutionContext context) {
    this.startTime = context.startTime;
    this.attemptStartTime = context.attemptStartTime;
    this.attempts = context.attempts;
    this.lastResult = context.lastResult;
    this.lastFailure = context.lastFailure;
  }

  /**
   * Returns the elapsed time since initial execution began.
   */
  public Duration getElapsedTime() {
    return Duration.ofNanos(System.nanoTime() - startTime.toNanos());
  }

  /**
   * Returns the elapsed time since the last execution attempt began.
   */
  public Duration getElapsedAttemptTime() {
    return Duration.ofNanos(System.nanoTime() - attemptStartTime.toNanos());
  }

  /**
   * Gets the number of completed execution attempts so far.
   */
  public int getAttemptCount() {
    return attempts.get();
  }

  /**
   * Returns the last failure that was recorded else {@code null}.
   */
  @SuppressWarnings("unchecked")
  public <T extends Throwable> T getLastFailure() {
    return (T) lastFailure;
  }

  /**
   * Returns the last result that was recorded else {@code null}.
   */
  @SuppressWarnings("unchecked")
  public <T> T getLastResult() {
    return (T) lastResult;
  }

  /**
   * Returns the last result that was recorded else the {@code defaultValue}.
   */
  @SuppressWarnings("unchecked")
  public <T> T getLastResult(T defaultValue) {
    return lastResult != null ? (T) lastResult : defaultValue;
  }

  /**
   * Returns the time that the initial execution started.
   */
  public Duration getStartTime() {
    return startTime;
  }

  /**
   * Returns whether the execution has ben cancelled. In this case the implementor shuold attempt to stop execution.
   */
  public boolean isCancelled() {
    return cancelled;
  }

  public ExecutionContext copy() {
    return new ExecutionContext(this);
  }

  static ExecutionContext ofResult(Object result) {
    ExecutionContext context = new ExecutionContext();
    context.lastResult = result;
    return context;
  }

  static ExecutionContext ofFailure(Throwable failure) {
    ExecutionContext context = new ExecutionContext();
    context.lastFailure = failure;
    return context;
  }

  @Override
  public String toString() {
    return "ExecutionContext[" + "attempts=" + attempts + ", lastResult=" + lastResult + ", lastFailure=" + lastFailure
      + ']';
  }
}

@@ -1898,16 +1898,82 @@
  so far.
+ Will return %7B@code 0%7D when the first attempt is in%0A   * progress.
 %0A   */%0A 
@@ -2968,16 +2968,407 @@
 d;%0A  %7D%0A%0A
+  /**%0A   * Returns %7B@code true%7D when %7B@link #getAttemptCount()%7D is %7B@code 0%7D meaning this is the first execution attempt.%0A   */%0A  public boolean isFirstAttempt() %7B%0A    return attempts.get() == 0;%0A  %7D%0A%0A  /**%0A   * Returns %7B@code true%7D when %7B@link #getAttemptCount()%7D is %7B@code %3E 0%7D meaning the execution is being retried.%0A   */%0A  public boolean isRetry() %7B%0A    return attempts.get() %3E 0;%0A  %7D%0A%0A
   public

Add some convenience methods to ExecutionContext.