Home | History | Annotate | Download | only in node
      1 // Copyright 2008 Google Inc. All Rights Reserved.
      2 
      3 package com.google.clearsilver.jsilver.syntax.node;
      4 
      5 import java.util.LinkedList;
      6 
      7 /**
      8  * Replacement for SableCC generated AMultipleCommand. Iterates much faster. Important because this
      9  * iteration is called a lot.
     10  *
     11  * NOTE: Because the SableCC generated code contains methods of package visibility that need to be
     12  * overriden, this class needs to reside in the same package.
     13  *
     14  * @see com.google.clearsilver.jsilver.syntax.SyntaxTreeOptimizer
     15  */
     16 public class AOptimizedMultipleCommand extends PCommand {
     17 
     18   private final PCommand[] commands;
     19 
     20   public AOptimizedMultipleCommand(AMultipleCommand originalNode) {
     21     LinkedList<PCommand> originalChildCommands = originalNode.getCommand();
     22     commands = new PCommand[originalChildCommands.size()];
     23     originalChildCommands.toArray(commands);
     24     for (int i = 0; i < commands.length; i++) {
     25       commands[i].parent(this); // set parent.
     26     }
     27   }
     28 
     29   @Override
     30   public Object clone() {
     31     return this; // Immutable object. Clone not necessary.
     32   }
     33 
     34   @Override
     35   void removeChild(Node child) {
     36     throw new UnsupportedOperationException();
     37   }
     38 
     39   @Override
     40   void replaceChild(Node oldChild, Node newChild) {
     41     if (newChild == null) {
     42       throw new IllegalArgumentException("newChild cannot be null.");
     43     }
     44     // Replace child
     45     for (int i = 0; i < commands.length; i++) {
     46       if (commands[i] == oldChild) {
     47         commands[i] = (PCommand) newChild;
     48         newChild.parent(this);
     49         oldChild.parent(null);
     50         return;
     51       }
     52     }
     53     throw new RuntimeException("Not a child.");
     54   }
     55 
     56   @Override
     57   public void apply(Switch sw) {
     58     for (int i = 0; i < commands.length; i++) {
     59       commands[i].apply(sw);
     60     }
     61   }
     62 }
     63