/*******************************************************************************
* (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
*******************************************************************************/
package io.cloudslang.lang.runtime.env;

import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

/**
 * @author moradi
 * @since 06/11/2014
 * @version $Id$
 */
public class ExecutionPath implements Serializable {

	public static final String PATH_SEPARATOR = "/";
	private static final long serialVersionUID = 5536588094244112461L;

	private Deque<Integer> parentPositions;
	private int position;

	public ExecutionPath() {
		parentPositions = new ArrayDeque<>();
	}

	public int forward() {
		return position++;
	}

	public int down() {
		parentPositions.push(position);
		position = 0;
		return position;
	}

	public int up() {
		position = parentPositions.pop();
		return position;
	}

	public int getDepth() {
		return parentPositions.size();
	}

	public String getCurrentPath() {
        return getCurrentPath(position);
	}

    public String getCurrentPathPeekForward() {
        return getCurrentPath(position + 1);
    }

    private String getCurrentPath(int position) {
        StringBuilder result = new StringBuilder();
        for(Iterator<Integer> iterator = parentPositions.descendingIterator(); iterator.hasNext();) {
            result.append(iterator.next()).append(PATH_SEPARATOR);
        }
        result.append(position);
        return result.toString();
    }

}

@@ -74,16 +74,17 @@
 *******%0A
+ 
 * (c) Co
@@ -138,16 +138,17 @@
 y, L.P.%0A
+ 
 * All ri
@@ -206,16 +206,17 @@
 terials%0A
+ 
 * are ma
@@ -306,18 +306,20 @@
 bution.%0A
+ 
 *%0A
+ 
 * The Ap
@@ -347,16 +347,17 @@
 able at%0A
+ 
 * http:/
@@ -393,18 +393,20 @@
 NSE-2.0%0A
+ 
 *%0A
+ 
 ********
@@ -519,16 +519,62 @@
 e.env;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 import j
@@ -650,35 +650,8 @@
 que;
-%0Aimport java.util.Iterator;
 %0A%0A/*
@@ -695,25 +695,8 @@
 014%0A
- * @version $Id$%0A
  */%0A
@@ -749,17 +749,20 @@
 able %7B%0A%0A
-%09
+    
 public s
@@ -806,78 +806,13 @@
 /%22;%0A
-%09private static final long serialVersionUID = 5536588094244112461L;%0A%0A%09
+%0A    
 priv
@@ -847,17 +847,20 @@
 itions;%0A
-%09
+    
 private 
@@ -874,17 +874,20 @@
 ition;%0A%0A
-%09
+    
 public E
@@ -899,26 +899,32 @@
 ionPath() %7B%0A
-%09%09
+        
 parentPositi
@@ -945,29 +945,35 @@
 yDeque%3C%3E();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public int f
@@ -979,26 +979,32 @@
 forward() %7B%0A
-%09%09
+        
 return posit
@@ -1010,21 +1010,27 @@
 tion++;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public i
@@ -1041,18 +1041,24 @@
 own() %7B%0A
-%09%09
+        
 parentPo
@@ -1081,18 +1081,24 @@
 ition);%0A
-%09%09
+        
 position
@@ -1103,18 +1103,24 @@
 on = 0;%0A
-%09%09
+        
 return p
@@ -1120,37 +1120,43 @@
 eturn position;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public int up() 
@@ -1157,18 +1157,24 @@
  up() %7B%0A
-%09%09
+        
 position
@@ -1199,18 +1199,24 @@
 .pop();%0A
-%09%09
+        
 return p
@@ -1224,21 +1224,27 @@
 sition;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public i
@@ -1263,10 +1263,16 @@
 ) %7B%0A
-%09%09
+        
 retu
@@ -1302,13 +1302,19 @@
 ();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 publ
@@ -1387,9 +1387,12 @@
 n);%0A
-%09
+    
 %7D%0A%0A 
@@ -1558,290 +1558,188 @@
 ring
-Builder result = new StringBuilder();%0A        for(Iterator%3CInteger%3E iterator = parentPositions.descendingIterator(); iterator.hasNext();) %7B%0A            result.append(iterator.next()).append(PATH_SEPARATOR);%0A        %7D%0A        result.append(position);%0A        return result.toString()
+ parents = StringUtils.join(parentPositions.descendingIterator(), PATH_SEPARATOR);%0A        return StringUtils.isEmpty(parents) ? position + %22%22 : parents + PATH_SEPARATOR + position
 ;%0A  

reformat execution path