content: /*
   * Copyright 2020 Alibaba Group Holding Limited.
   *
   * 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 com.alibaba.graphscope.gremlin.plugin.strategy;
  
  import com.alibaba.graphscope.gremlin.Utils;
  
  import org.apache.tinkerpop.gremlin.process.traversal.Step;
  import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
  import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
  import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep;
  import org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep;
  import org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep;
  import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
  
  import java.util.List;
  
  public class RemoveUselessStepStrategy
          extends AbstractTraversalStrategy<TraversalStrategy.ProviderOptimizationStrategy>
          implements TraversalStrategy.ProviderOptimizationStrategy {
      private static final RemoveUselessStepStrategy INSTANCE = new RemoveUselessStepStrategy();
  
      private RemoveUselessStepStrategy() {}
  
      public static RemoveUselessStepStrategy instance() {
          return INSTANCE;
      }
  
      @Override
      public void apply(Traversal.Admin<?, ?> traversal) {
          List<Step> steps = traversal.getSteps();
          for (int i = 0; i < steps.size(); ++i) {
              Step step = steps.get(i);
              if (Utils.equalClass(step, ComputerAwareStep.EndStep.class)
                      || Utils.equalClass(step, NoOpBarrierStep.class)) {
                  traversal.removeStep(step);
              } else if (Utils.equalClass(step, WhereTraversalStep.WhereStartStep.class)
                      && ((WhereTraversalStep.WhereStartStep) step).getScopeKeys().isEmpty()) {
                  traversal.removeStep(step);
              }
          }
      }
  }
  