Interface Namespace


public interface Namespace
A Namespace offers storage of Variable and Command by name. Namespaces exist in a hierarchy. Variables and Commands added to this namespace usually shadow those from parent namespaces, and are usually visible to child namespaces.

A Namespace is passed in from the script executor to Command.createStackFrame(org.praxislive.script.Namespace, java.util.List).

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add a Command with the given ID to this Namespace.
    void
    Add a Variable with the given ID to this Namespace.
    Create a child Namespace of this Namespace.
    default Variable
    Create a constant in this namespace with the initial value given.
    default Variable
    Create a variable in this namespace with the initial value given.
    Get the Command with the given ID, or null if it doesn't exist in this or a parent namespace.
    default Variable
    getOrCreateVariable(String id, Value defaultValue)
    Get the variable with the given ID from this namespace or a parent namespace, creating and initializing a variable with the provided default value if none exists.
    Get the Variable with the given ID, or null if it doesn't exist in this or a parent namespace.
  • Method Details

    • getVariable

      Variable getVariable(String id)
      Get the Variable with the given ID, or null if it doesn't exist in this or a parent namespace.
      Parameters:
      id - variable ID
      Returns:
      named variable, or null if none exists
    • addVariable

      void addVariable(String id, Variable var)
      Add a Variable with the given ID to this Namespace.
      Parameters:
      id - variable ID
      var - variable to add
      Throws:
      IllegalArgumentException - if a variable with that ID already exists in this namespace
    • getCommand

      Command getCommand(String id)
      Get the Command with the given ID, or null if it doesn't exist in this or a parent namespace.
      Parameters:
      id - command ID
      Returns:
      named command, or null if none exists
    • addCommand

      void addCommand(String id, Command cmd)
      Add a Command with the given ID to this Namespace.
      Parameters:
      id - command ID
      cmd - command to add
      Throws:
      IllegalArgumentException - if a command with that ID already exists in this namespace
    • createChild

      Namespace createChild()
      Create a child Namespace of this Namespace.
      Returns:
      child namespace
    • createVariable

      default Variable createVariable(String id, Value value)
      Create a variable in this namespace with the initial value given.

      The default implementation of this method creates a new instance of a variable implementation, and calls addVariable(java.lang.String, org.praxislive.script.Variable) to register it.

      Parameters:
      id - variable ID
      value - initial value
      Returns:
      created variable
      Throws:
      IllegalArgumentException - if a variable with that name already exists in this namespace
    • getOrCreateVariable

      default Variable getOrCreateVariable(String id, Value defaultValue)
      Get the variable with the given ID from this namespace or a parent namespace, creating and initializing a variable with the provided default value if none exists.

      The default implementation of this method calls getVariable(java.lang.String) to find a registered variable, and if that method returns

      invalid @link
      {@link null
      } delegates to createVariable(java.lang.String, org.praxislive.core.Value).
      Parameters:
      id - variable ID
      defaultValue - default initial value
      Returns:
      created variable
    • createConstant

      default Variable createConstant(String id, Value value)
      Create a constant in this namespace with the initial value given. The constant is guaranteed to always return value from Variable.getValue(), and to always throw UnsupportedOperationException on any call to Variable.setValue(org.praxislive.core.Value).

      The default implementation of this method creates a new instance of a constant variable implementation, and calls addVariable(java.lang.String, org.praxislive.script.Variable) to register it.

      Parameters:
      id - constant name
      value - constant value
      Returns:
      created constant
      Throws:
      IllegalArgumentException - if a variable with that name already exists in this namespace