Package org.apache.sling.scripting.sightly.compiler.util

## Class VariableTracker<T>

- java.lang.Object
- - org.apache.sling.scripting.sightly.compiler.util.VariableTracker<T>

    public final class VariableTracker<T>
    extends Object

Tracks information related to HTL variables, at different levels of the compiler.

### Constructor Summary

Constructors
`VariableTracker()`

### Method Summary

All Methods Instance Methods Concrete Methods
`T` `get​(String name)` Get the data associated with the given variable.
`List<T>` `getAll​(String name)` Get an immutable view of all the data items associated with the specified variable.
`int` `getOccurrenceCount​(String name)` Get how many times a variable was declared in the current scope.
`boolean` `isDeclared​(String name)` Checks if the variable identified by `name` is tracked by this tracker or not.
`boolean` `isEmpty()` Checks if the declaration stack is empty.
`boolean` `isInScope​(String name)` Check whether a variable was declared and is visible in the current scope.
`Map.Entry<String,​T>` `peek()` Peeks at the top of the declaration stack.
`String` `popVariable()` Pops a variable from this tracker.
`void` `pushVariable​(String name, T data)` Pushes a variable to this tracker.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### VariableTracker

    public VariableTracker()

### Method Detail

#### isDeclared

    public boolean isDeclared​(String name)

Checks if the variable identified by `name` is tracked by this tracker or not.

Parameters:
  `name` - the name of the variable
Returns:
  `true` if the variable is declared, `false` otherwise

#### pushVariable

    public void pushVariable​(String name,
                             T data)

Pushes a variable to this tracker. Call this method whenever a variable is declared.

Parameters:
  `name` - the name of the variable
  `data` - the data associated with the variable

#### popVariable

    public String popVariable()

Pops a variable from this tracker. Call this method whenever a variable goes out of scope.

Returns:
  the name of the popped variable
Throws:
  `NoSuchElementException` - if there are no declared variables in the scope

#### peek

    public Map.Entry<String,​T> peek()

Peeks at the top of the declaration stack.

Returns:
  the most recently declared variable and its associated data
Throws:
  `NoSuchElementException` - if there are no variables in scope

#### isEmpty

    public boolean isEmpty()

Checks if the declaration stack is empty.

Returns:
  `true` if the stack is empty, `false` otherwise

#### get

    public T get​(String name)

Get the data associated with the given variable.

Parameters:
  `name` - the name of the variable
Returns:
  the associated data or `null` if that variable is not in scope

#### isInScope

    public boolean isInScope​(String name)

Check whether a variable was declared and is visible in the current scope.

Parameters:
  `name` - the name of the variable
Returns:
  `true` if the variable is visible in the current scope, `false` otherwise

#### getAll

    public List<T> getAll​(String name)

Get an immutable view of all the data items associated with the specified variable.

Parameters:
  `name` - the name of the variable
Returns:
  a list of all the data associated with this variable name. If the variable is not declared in the current scope then that list will be empty. Otherwise it will contain all the data associated for the current scope starting with the data associated at the topmost scope and ending with the most recently associated data

#### getOccurrenceCount

    public int getOccurrenceCount​(String name)

Get how many times a variable was declared in the current scope.

Parameters:
  `name` - the name of the variable
Returns:
  the number of declarations for a variable in the current scope