package com.tinkerpop.gremlin.process.graph.strategy;

import com.tinkerpop.gremlin.process.Step;
import com.tinkerpop.gremlin.process.Traversal;
import com.tinkerpop.gremlin.process.TraversalEngine;
import com.tinkerpop.gremlin.process.graph.step.sideEffect.ProfileStep;
import com.tinkerpop.gremlin.process.util.TraversalHelper;

import java.util.List;

/**
 * @author Bob Briody (http://bobbriody.com)
 */
public class ProfileStrategy extends AbstractTraversalStrategy {

    private static final ProfileStrategy INSTANCE = new ProfileStrategy();

    private ProfileStrategy() {
    }

    @Override
    public void apply(final Traversal<?, ?> traversal, final TraversalEngine engine) {
        if (!TraversalHelper.hasStepOfClass(ProfileStep.class, traversal))
            return;

        // Remove user-specified .profile() steps
        List<ProfileStep> profileSteps = TraversalHelper.getStepsOfClass(ProfileStep.class, traversal);
        for (ProfileStep step : profileSteps) {
            TraversalHelper.removeStep(step, traversal);
        }

        // Add .profile() step after every pre-existing step.
        final List<Step> steps = traversal.asAdmin().getSteps();
        for (int ii = 0; ii < steps.size(); ii++) {
            TraversalHelper.insertStep(new ProfileStep(traversal, steps.get(ii)), ii + 1, traversal);
            ii++;
        }
    }

    public static ProfileStrategy instance() {
        return INSTANCE;
    }
}

@@ -193,16 +193,72 @@
 Engine;%0A
+import com.tinkerpop.gremlin.process.TraversalStrategy;%0A
 import c
@@ -317,24 +317,24 @@
 rofileStep;%0A
-
 import com.t
@@ -398,19 +398,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -572,16 +572,16 @@
 tegy %7B%0A%0A
-
     priv
@@ -646,16 +646,232 @@
 ategy();
+%0A    private static final Set%3CClass%3C? extends TraversalStrategy%3E%3E PRIORS = new HashSet%3C%3E();%0A%0A    static %7B%0A        // Ensure that this strategy is applied last.%0A        PRIORS.add(TraverserSourceStrategy.class);%0A    %7D
 %0A%0A    pr
@@ -1682,24 +1682,134 @@
    %7D%0A    %7D%0A%0A
+    @Override%0A    public Set%3CClass%3C? extends TraversalStrategy%3E%3E applyPrior() %7B%0A        return PRIORS;%0A    %7D%0A%0A
     public s

Add applyPrior strategy ordering to ProfileStrategy.