package com.tinkerpop.blueprints.util;

import com.tinkerpop.blueprints.TransactionalGraph;

/**
 * Creates a TransactionGraph "holder" which allows execution of a TransactionWork instance inside of a
 * TransactionRetryStrategy implementation.
 *
 * @param <T> The return value of the work.
 */
public class TransactionRetryHelper<T> {
    private final TransactionalGraph graph;
    private final TransactionWork<T> work;

    private TransactionRetryHelper(final Builder<T> builder) {
        this.graph = builder.getGraph();
        this.work = builder.getWork();
    }

    /**
     * Executes the work committing if possible and rolling back on failure.  On failure, not exception is reported.
     */
    public T fireAndForget() {
        return tryIt(new TransactionRetryStrategy.FireAndForget<T>());
    }

    /**
     * Executes the work committing if possible and rolling back on failure.  On failure an exception is reported.
     */
    public T oneAndDone() {
        return tryIt(new TransactionRetryStrategy.OneAndDone<T>());
    }

    /**
     * Executes the work with a default number of retries with a default number of milliseconds delay between
     * each try.
     */
    public T retry() {
        return tryIt(new TransactionRetryStrategy.DelayedRetry<T>());
    }

    /**
     * Executes the work with a specified number of retries with a default number of milliseconds delay between
     * each try.
     */
    public T retry(final int retries) {
        return tryIt(new TransactionRetryStrategy.DelayedRetry<T>(retries, TransactionRetryStrategy.DelayedRetry.DEFAULT_DELAY_MS));
    }

    /**
     * Executes the work with a specified number of retries with a specified number of milliseconds delay between
     * each try.
     */
    public T retry(final int retries, final long delayBetweenRetries) {
        return tryIt(new TransactionRetryStrategy.DelayedRetry<T>(retries, delayBetweenRetries));
    }

    /**
     * Executes the work with a default number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     */
    public T exponentialBackoff() {
        return tryIt(new TransactionRetryStrategy.ExponentialBackoff<T>());
    }

    /**
     * Executes the work with a specified number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     */
    public T exponentialBackoff(final int retries) {
        return tryIt(new TransactionRetryStrategy.ExponentialBackoff<T>(
                retries, TransactionRetryStrategy.ExponentialBackoff.DEFAULT_TRIES));
    }

    /**
     * Executes the work with a specified TransactionRetryStrategy.
     */
    public T tryIt(final TransactionRetryStrategy<T> strategy) {
        return strategy.execute(this.graph, this.work);
    }

    public static class Builder<T> {
        private final TransactionalGraph graph;
        private TransactionWork<T> work;

        public Builder(final TransactionalGraph graph) {
            this.graph = graph;
        }

        public Builder<T> perform(final TransactionWork<T> work) {
            this.work = work;
            return this;
        }

        public TransactionRetryHelper<T> build() {
            return new TransactionRetryHelper<T>(this);
        }

        public TransactionalGraph getGraph() {
            return graph;
        }

        public TransactionWork<T> getWork() {
            return work;
        }
    }
}

@@ -739,37 +739,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -974,37 +974,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -1213,37 +1213,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -1473,37 +1473,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -1830,37 +1830,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -2125,37 +2125,35 @@
 %0A        return 
-tryIt
+use
 (new Transaction
@@ -2425,21 +2425,19 @@
  return 
-tryIt
+use
 (new Tra
@@ -2675,13 +2675,11 @@
 c T 
-tryIt
+use
 (fin
@@ -2780,24 +2780,86 @@
 rk);%0A    %7D%0A%0A
+    /**%0A     * Constructs a TransactionRetryStrategy.%0A     */%0A
     public s

Refactor naming of TransactionRetryHelper methods.