package com.bbn.bue.common.parameters.exceptions;

import java.io.File;
import java.util.Stack;

public class ErrorInIncludedParameterFileException extends ParameterFileException {
	private static final long serialVersionUID = 1L;
	
	private ErrorInIncludedParameterFileException(Stack<ErrorInIncludedParameterFileException.ErrorLocation> includeStack, 
			Exception rootCause)
	{
		super(makeMessage(includeStack, rootCause));
		this.includeStack = includeStack;
		this.rootCause = rootCause;
	}
	
	public static ErrorInIncludedParameterFileException fromException(final File filename, final int lineNumber, 
		Exception rootCause) 
	{
		Stack<ErrorInIncludedParameterFileException.ErrorLocation> includeStack = new Stack<ErrorInIncludedParameterFileException.ErrorLocation>();
		includeStack.push(new ErrorLocation(filename, lineNumber));
		return new ErrorInIncludedParameterFileException(includeStack, rootCause);
	}
	
	public static ErrorInIncludedParameterFileException fromNextLevel(final File filename, final int lineNumber,
		ErrorInIncludedParameterFileException nextLevel) 
	{
		final Stack<ErrorInIncludedParameterFileException.ErrorLocation> includeStack = new Stack<ErrorInIncludedParameterFileException.ErrorLocation>();
		includeStack.addAll(nextLevel.includeStack);
		includeStack.push(new ErrorLocation(filename, lineNumber));
		return new ErrorInIncludedParameterFileException(includeStack, nextLevel.rootCause);
	}
	
	private static String makeMessage(Stack<ErrorInIncludedParameterFileException.ErrorLocation> includeStack, 
			Exception rootCause) 
	{
		final StringBuilder ret = new StringBuilder();
		
		while(!includeStack.empty()) {
			final ErrorInIncludedParameterFileException.ErrorLocation errorLocation = includeStack.pop();
			ret.append(String.format("Included from %s:%d\n", 
				errorLocation.file.getAbsolutePath(), errorLocation.lineNumber));
		}
		ret.append("Root cause: ").append(rootCause.toString());
		
		return ret.toString();
	}
	
	private static final class ErrorLocation {
		public ErrorLocation(File file, int lineNumber) {
			this.file = file;
			this.lineNumber = lineNumber;
		}
		
		public final File file;
		public final int lineNumber;
	}
	
	final Exception rootCause;
	final Stack<ErrorInIncludedParameterFileException.ErrorLocation> includeStack;
}
@@ -65,16 +65,69 @@
 o.File;%0A
+import java.util.ArrayDeque;%0Aimport java.util.Deque;%0A
 import j
@@ -142,16 +142,16 @@
 .Stack;%0A
-
 %0Apublic 
@@ -330,52 +330,14 @@
 ion(
-Stack%3CErrorInIncludedParameterFileException.
+Deque%3C
 Erro
@@ -352,33 +352,32 @@
 n%3E includeStack,
- 
 %0A%09%09%09Exception ro
@@ -646,21 +646,27 @@
 ) %0A%09%7B%0A%09%09
-Stack
+final Deque
 %3CErrorIn
@@ -734,52 +734,19 @@
 new 
-Stack%3CErrorInIncludedParameterFileException.
+ArrayDeque%3C
 Erro
@@ -1072,29 +1072,29 @@
 %0A%09%7B%0A%09%09final 
-Stack
+Deque
 %3CErrorInIncl
@@ -1158,52 +1158,19 @@
 new 
-Stack%3CErrorInIncludedParameterFileException.
+ArrayDeque%3C
 Erro
@@ -1423,52 +1423,14 @@
 age(
-Stack%3CErrorInIncludedParameterFileException.
+Deque%3C
 Erro
@@ -1453,17 +1453,16 @@
 deStack,
- 
 %0A%09%09%09Exce
@@ -1556,17 +1556,19 @@
 deStack.
-e
+isE
 mpty()) 
@@ -2144,52 +2144,14 @@
 nal 
-Stack%3CErrorInIncludedParameterFileException.
+Deque%3C
 Erro

Remove use of deprecated Stack class