/*
 * Copyright 2018 Google Inc.
 *
 * 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.google.template.soy.passes.htmlmatcher;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Optional;
import javax.annotation.Nullable;

public final class HtmlMatcherGraph {

  @Nullable private HtmlMatcherGraphNode rootNode = null;

  /** Pointer to where all new nodes are added in the graph. */
  @Nullable private HtmlMatcherGraphNode graphCursorNode = null;

  public Optional<HtmlMatcherGraphNode> getRootNode() {
    return Optional.fromNullable(rootNode);
  }

  /**
   * Attaches a new HTML tag node to the graph at the current cursor position.
   *
   * @param node the node to add
   */
  public void addNode(HtmlMatcherGraphNode node) {
    checkNotNull(node);
    if (graphCursorNode != null) {
      graphCursorNode.linkActiveEdgeToNode(node);
    }
    setGraphCursorNode(node);
  }

  private void setGraphCursorNode(HtmlMatcherGraphNode node) {
    graphCursorNode = node;
    if (rootNode == null) {
      rootNode = graphCursorNode;
    }
  }
}

@@ -706,16 +706,79 @@
 NotNull;
+%0Aimport static com.google.common.base.Preconditions.checkState;
 %0A%0Aimport
@@ -826,28 +826,23 @@
 java
-x.annotation.Nullabl
+.util.ArrayDequ
 e;%0A%0A
@@ -882,34 +882,33 @@
 ph %7B%0A%0A  
-@Nullable private 
+private Optional%3C
 HtmlMatc
@@ -919,16 +919,17 @@
 raphNode
+%3E
  rootNod
@@ -928,28 +928,41 @@
  rootNode = 
-null
+Optional.absent()
 ;%0A%0A  /** Poi
@@ -1022,26 +1022,25 @@
 /%0A  
-@Nullable private 
+private Optional%3C
 Html
@@ -1055,16 +1055,17 @@
 raphNode
+%3E
  graphCu
@@ -1072,119 +1072,1091 @@
 rsor
-Node = null;%0A%0A  public Optional%3CHtmlMatcherGraphNode%3E getRootNode() %7B%0A    return Optional.fromNullable(rootNode
+ = Optional.absent();%0A%0A  private final ArrayDeque%3COptional%3CHtmlMatcherGraphNode%3E%3E cursorStack = new ArrayDeque%3C%3E();%0A%0A  /** Returns the root node of the graph. */%0A  public Optional%3CHtmlMatcherGraphNode%3E getRootNode() %7B%0A    return rootNode;%0A  %7D%0A%0A  /**%0A   * Returns the %7B@link com.google.template.soy.passes.htmlmatcher.HtmlMatcherGraphNode%7D at the%0A   * cursor.%0A   */%0A  public Optional%3CHtmlMatcherGraphNode%3E getNodeAtCursor() %7B%0A    return graphCursor;%0A  %7D%0A%0A  /**%0A   * Saves the current cursor.%0A   *%0A   * %3Cp%3EDoes not change the value of the cursor. The matching call to %7B@link #restoreCursor()%7D will%0A   * move the cursor back to this position in the graph.%0A   */%0A  public void saveCursor() %7B%0A    cursorStack.addFirst(graphCursor);%0A  %7D%0A%0A  /**%0A   * Pops the cursor stack and moves the cursor to that value.%0A   *%0A   * %3Cp%3EMust be paired with a call to %7B@link #saveCursor()%7D.%0A   */%0A  public void restoreCursor() %7B%0A    checkState(%0A        !cursorStack.isEmpty(),%0A        %22Cursor stack underflow: restoreCursor() without matching saveCursor() call.%22);%0A    graphCursor = cursorStack.removeFirst(
 );%0A 
@@ -2386,20 +2386,20 @@
 rsor
-Node != null
+.isPresent()
 ) %7B%0A
@@ -2415,20 +2415,22 @@
 phCursor
-Node
+.get()
 .linkAct
@@ -2573,19 +2573,28 @@
 rsor
-Node = 
+ = Optional.of(
 node
+)
 ;%0A  
@@ -2599,24 +2599,25 @@
     if (
+!
 rootNode
  == null
@@ -2612,16 +2612,20 @@
 Node
- == null
+.isPresent()
 ) %7B%0A
@@ -2652,20 +2652,16 @@
 phCursor
-Node
 ;%0A    %7D%0A

Add a stack for the graph cursor.