Class ScopeChain


  • public class ScopeChain
    extends java.lang.Object
    A stack data structure used to represent the scope of variables that are currently accessible. Pushing a new scope will allow the template to add variables with names of pre-existing variables without overriding the originals; to access the original variables you would pop the scope again.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.LinkedList<Scope> stack
      The stack of scopes
    • Constructor Summary

      Constructors 
      Constructor Description
      ScopeChain()
      Constructs an empty scope chain without any known scopes.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsKey​(java.lang.String key)
      This method checks if the given key does exists within the scope chain.
      boolean currentScopeContainsVariable​(java.lang.String variableName)
      Checks if the current scope contains a variable without then looking up the scope chain.
      ScopeChain deepCopy()
      Creates a deep copy of the ScopeChain.
      java.lang.Object get​(java.lang.String key)
      Retrieves a variable from the scope chain, starting at the current scope and working it's way up all visible scopes.
      java.util.List<Scope> getGlobalScopes()  
      void popScope()
      Pops the most recent scope from the scope chain.
      void pushLocalScope()
      Adds a new local scope to the scope chain
      void pushScope()
      Adds an empty non-local scope to the scope chain
      void pushScope​(java.util.Map<java.lang.String,​java.lang.Object> map)
      Adds a new non-local scope to the scope chain
      void put​(java.lang.String key, java.lang.Object value)
      Adds a variable to the current scope.
      void set​(java.lang.String key, java.lang.Object value)
      Sets the value of a variable in the first scope in the chain that already contains the variable; adds a variable to the current scope if an existing variable is not found.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • stack

        private java.util.LinkedList<Scope> stack
        The stack of scopes
    • Constructor Detail

      • ScopeChain

        public ScopeChain()
        Constructs an empty scope chain without any known scopes.
    • Method Detail

      • deepCopy

        public ScopeChain deepCopy()
        Creates a deep copy of the ScopeChain. This is used for the parallel tag because every new thread should have a "snapshot" of the scopes, i.e. if one thread adds a new object to a scope, it should not be available to the other threads.

        This will construct a new scope chain and new scopes but it will continue to have references to the original user-provided variables. This is why it is important for the user to only provide thread-safe variables when using the "parallel" tag.

        Returns:
        A copy of the scope chain
      • pushScope

        public void pushScope()
        Adds an empty non-local scope to the scope chain
      • pushScope

        public void pushScope​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Adds a new non-local scope to the scope chain
        Parameters:
        map - The known variables of this scope.
      • pushLocalScope

        public void pushLocalScope()
        Adds a new local scope to the scope chain
      • popScope

        public void popScope()
        Pops the most recent scope from the scope chain.
      • put

        public void put​(java.lang.String key,
                        java.lang.Object value)
        Adds a variable to the current scope.
        Parameters:
        key - The name of the variable
        value - The value of the variable
      • get

        public java.lang.Object get​(java.lang.String key)
        Retrieves a variable from the scope chain, starting at the current scope and working it's way up all visible scopes.
        Parameters:
        key - The name of the variable
        Returns:
        The value of the variable
      • containsKey

        public boolean containsKey​(java.lang.String key)
        This method checks if the given key does exists within the scope chain.
        Parameters:
        key - the for which the the check should be executed for.
        Returns:
        true when the key does exists or false when the given key does not exists.
      • currentScopeContainsVariable

        public boolean currentScopeContainsVariable​(java.lang.String variableName)
        Checks if the current scope contains a variable without then looking up the scope chain.
        Parameters:
        variableName - The name of the variable
        Returns:
        Whether or not the variable exists in the current scope
      • set

        public void set​(java.lang.String key,
                        java.lang.Object value)
        Sets the value of a variable in the first scope in the chain that already contains the variable; adds a variable to the current scope if an existing variable is not found.
        Parameters:
        key - The name of the variable
        value - The value of the variable
      • getGlobalScopes

        public java.util.List<Scope> getGlobalScopes()