/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 Sri Harsha Chilakapati
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.utils;

import com.shc.silenceengine.core.SilenceException;
import com.shc.silenceengine.utils.functional.Provider;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @param <T> Any typed parameter.
 *
 * @author Sri Harsha Chilakapati
 */
public final class ReusableStack<T>
{
    private Deque<T> stack;
    private List<T>  list;

    private Provider<T> objectProvider;

    public ReusableStack(Provider<T> objectProvider)
    {
        stack = new LinkedList<>();
        list = new ArrayList<>();
        this.objectProvider = objectProvider;
    }

    public T pop()
    {
        if (stack.size() == 0)
            try
            {
                T object = objectProvider.provide();

                list.add(object);
                stack.push(object);
            }
            catch (Exception e)
            {
                SilenceException.reThrow(e);
            }

        return stack.pop();
    }

    public void push(T value)
    {
        stack.push(value);
    }

    public List<T> getAsList()
    {
        return list;
    }
}

@@ -1524,16 +1524,63 @@
 ck%3CT%3E%0A%7B%0A
+    private final Object lock = new Object();%0A%0A
     priv
@@ -1867,32 +1867,74 @@
 c T pop()%0A    %7B%0A
+        synchronized (lock)%0A        %7B%0A    
         if (stac
@@ -1960,16 +1960,20 @@
         
+    
 try%0A    
@@ -1972,34 +1972,42 @@
 try%0A            
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -2056,24 +2056,28 @@
             
+  
+  
 list.add(obj
@@ -2074,32 +2074,36 @@
 st.add(object);%0A
+    
                 
@@ -2134,18 +2134,26 @@
         
+   
+ 
 %7D%0A
+    
         
@@ -2176,16 +2176,20 @@
 tion e)%0A
+    
         
@@ -2210,16 +2210,20 @@
         
+    
 SilenceE
@@ -2251,24 +2251,28 @@
             
+    
 %7D%0A%0A        r
@@ -2266,24 +2266,28 @@
  %7D%0A%0A        
+  
+  
 return stack
@@ -2290,24 +2290,34 @@
 tack.pop();%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2339,32 +2339,74 @@
 (T value)%0A    %7B%0A
+        synchronized (lock)%0A        %7B%0A    
         stack.pu
@@ -2412,24 +2412,34 @@
 ush(value);%0A
+        %7D%0A
     %7D%0A%0A    p

Synchronize ReusableStack