pnuts.lang
Class Package

java.lang.Object
  extended by pnuts.lang.Package
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Property
Direct Known Subclasses:
CompositePackage, SecurePackage

public class Package
extends java.lang.Object
implements Property, java.io.Serializable, java.lang.Cloneable

This class represents a Pnuts' package (not Java's).

See Also:
Serialized Form

Field Summary
protected  pnuts.lang.SymbolTable autoloadTable
           
protected  pnuts.lang.SymbolTable exportedSymbols
           
protected  boolean exports
           
static Package globalPackage
          The package with name "".
protected  boolean initialized
           
protected  java.lang.String name
          The name of the package.
protected  java.util.Hashtable packages
          All packages with a non-null name
protected  Package parent
           
protected  java.util.Vector providedModuleNames
           
protected  java.util.Vector requiredModuleNames
           
protected  Package root
           
protected  boolean usedAsModule
           
 
Constructor Summary
  Package()
          Creates a package that is not visible from other packages.
  Package(java.lang.String name)
          Creates a package and register it in a static hashtable.
  Package(java.lang.String name, Package parent)
          Creates a package and register it in a static hashtable.
protected Package(java.lang.String name, Package parent, Package root)
          Creates a package and register it in a static hashtable.
 
Method Summary
protected  void addPackage(Package pkg, Context context)
           
 java.util.Map asMap()
          Returns a Map object that wraps this package
 void autoload(java.lang.String name, AutoloadHook hook)
          Registers an AutoloadHook for the name.
 void autoload(java.lang.String name, java.lang.String file, Context context)
          Registers an autoload script for the name.
 java.util.Enumeration bindings()
          Returns an enumeration of the NamedValues in the symbol table.
 void clear()
          Deletes all name-value bindings.
 void clear(java.lang.String symbol, Context context)
          Deletes a symbol from the package.
 java.lang.Object clone()
          Returns a clone package.
 boolean defined(java.lang.String name, Context context)
          Checks if the specified name is already defined in this package.
 java.util.Enumeration elements()
          Deprecated.  
 void export(java.lang.String name)
          Exports a symbol of the module
static Package find(java.lang.String pkg)
          Deprecated. replaced by find(String, Context)
static Package find(java.lang.String pkg, Context context)
          Find a named package.
 java.lang.Object get(java.lang.String interned)
          Gets the value of a variable.
 java.lang.Object get(java.lang.String symbol, Context context)
          Get the value of a symbol in the package.
static Package getGlobalPackage()
           
 java.lang.String getName()
           
static Package getPackage(java.lang.String pkg)
          If package "pkg" exists returns the package, otherwise creates and returns it.
static Package getPackage(java.lang.String pkg, Context context)
          If package "pkg" exists returns the package, otherwise creates and returns it.
 Package getParent()
          Returns the parent package.
protected  void init(Context context)
          This method is called when the package become the current package with package() function.
protected  void initializeModule()
          Starts using this package as a module.
 java.util.Enumeration keys()
          Returns an enumeration of the keys in the symbol table.
 NamedValue lookup(java.lang.String interned)
          Looks for a name-value binding in the symbol table chain.
 NamedValue lookup(java.lang.String symbol, Context context)
          Looks up a symbol in this package.
protected  NamedValue lookupRecursively(java.lang.String symbol, Context context)
          Lookup the symbol in the package.
 Package newInstance(java.lang.String name)
          Creates an uninitialized instance of a Package subclass When a sub-package is created, this method is called.
static void remove(java.lang.String name)
          Deprecated. replaced by remove(String, Context)
static void remove(java.lang.String name, Context context)
          Removes the specified package.
protected  void removePackage(Package pkg, Context context)
           
 void set(java.lang.String interned, java.lang.Object value)
          Defines a name-value binding in the symbol table.
 void set(java.lang.String symbol, java.lang.Object obj, Context context)
          Set a value of a symbol in the package.
 void setConstant(java.lang.String interned, java.lang.Object value)
          Defines a constant in the symbol table.
 int size()
           
 java.lang.String toString()
           
 java.util.Enumeration values()
          Returns an enumeration of the values in the symbol table.
static Package wrap(java.util.Map map)
          Returns a Package object that wrap the specified Map
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

packages

protected transient java.util.Hashtable packages
All packages with a non-null name


globalPackage

public static final Package globalPackage
The package with name "".


parent

protected transient Package parent

root

protected transient Package root

autoloadTable

protected pnuts.lang.SymbolTable autoloadTable

exportedSymbols

protected pnuts.lang.SymbolTable exportedSymbols

requiredModuleNames

protected java.util.Vector requiredModuleNames

providedModuleNames

protected java.util.Vector providedModuleNames

exports

protected boolean exports

usedAsModule

protected boolean usedAsModule

initialized

protected boolean initialized

name

protected java.lang.String name
The name of the package.

Constructor Detail

Package

public Package()
Creates a package that is not visible from other packages.


Package

public Package(java.lang.String name)
Creates a package and register it in a static hashtable.

Parameters:
name - the name of the package

Package

public Package(java.lang.String name,
               Package parent)
Creates a package and register it in a static hashtable. The method get() tries to find a symbol in this package and then consult the parent package. Other instance methods, such as set(), defined(), operates on this package only. Other constructors implicitly specify the global package as the parent package.

Parameters:
name - the name of the package
parent - the parent package.

Package

protected Package(java.lang.String name,
                  Package parent,
                  Package root)
Creates a package and register it in a static hashtable. The method get() tries to find a symbol in this package and then consult the parent package. Other instance methods, such as set(), defined(), operates on this package only. Other constructors implicitly specify the global package as the parent package.

Parameters:
name - the name of the package
parent - the parent package.
root - the root package.
Method Detail

getGlobalPackage

public static Package getGlobalPackage()
Returns:
the global package

wrap

public static Package wrap(java.util.Map map)
Returns a Package object that wrap the specified Map


asMap

public java.util.Map asMap()
Returns a Map object that wraps this package


newInstance

public Package newInstance(java.lang.String name)
Creates an uninitialized instance of a Package subclass When a sub-package is created, this method is called.

Parameters:
name - the package name
Returns:
a Package object

addPackage

protected void addPackage(Package pkg,
                          Context context)

removePackage

protected void removePackage(Package pkg,
                             Context context)

getPackage

public static Package getPackage(java.lang.String pkg)
If package "pkg" exists returns the package, otherwise creates and returns it.


getPackage

public static Package getPackage(java.lang.String pkg,
                                 Context context)
If package "pkg" exists returns the package, otherwise creates and returns it.


defined

public boolean defined(java.lang.String name,
                       Context context)
Checks if the specified name is already defined in this package.

Returns:
true if name is defined in the package.

get

public java.lang.Object get(java.lang.String symbol,
                            Context context)
Get the value of a symbol in the package. When the symbol is not defined in the package, first, the associated autoloading hook is invoked if any, second, get the value in the parent package.

Specified by:
get in interface Property
Parameters:
symbol - an interned name in the package
context - the context in which the symbol is referenced. null means "not specified".
Returns:
the value of specified variable in the package.

set

public void set(java.lang.String symbol,
                java.lang.Object obj,
                Context context)
Set a value of a symbol in the package. If this package is "used" in the specified context, and if the target object is either a Class object or a function whose name matches the symbol, then the symbol is imported to the context.

Specified by:
set in interface Property
Parameters:
symbol - an interned name of variable
obj - the value of the variable
context - the context in which the expression is evaluated.

export

public void export(java.lang.String name)
Exports a symbol of the module

Parameters:
name - the symbol
Throws:
java.lang.IllegalStateException - when the package is not used as a module.

clear

public void clear(java.lang.String symbol,
                  Context context)
Deletes a symbol from the package.

Parameters:
symbol - a name of variable to be deleted

remove

public static void remove(java.lang.String name)
Deprecated. replaced by remove(String, Context)

Removes the specified package.


remove

public static void remove(java.lang.String name,
                          Context context)
Removes the specified package.


find

public static Package find(java.lang.String pkg)
Deprecated. replaced by find(String, Context)

Find a named package.

Parameters:
pkg - a name of package to look.
Returns:
a package with name "pkg" if it exits.

find

public static Package find(java.lang.String pkg,
                           Context context)
Find a named package.

Parameters:
pkg - a name of package to look.
Returns:
a package with name "pkg" if it exits.

init

protected void init(Context context)
This method is called when the package become the current package with package() function. This method in a subclass must call super.init(context) first.


getName

public java.lang.String getName()
Returns:
the name of the package.

getParent

public Package getParent()
Returns the parent package.


lookup

public NamedValue lookup(java.lang.String symbol,
                         Context context)
Looks up a symbol in this package. If not defined, the associated autoloading hook is invoked if any.

Parameters:
symbol - an interned String
context - the context
Returns:
a NamedValue

lookupRecursively

protected NamedValue lookupRecursively(java.lang.String symbol,
                                       Context context)
Lookup the symbol in the package. When the symbol is not defined in the package and this.parent is not null, lookup the symbol in the parent package. Resulting value is an instance of NamedValue.

Parameters:
symbol - intern'ed string

initializeModule

protected void initializeModule()
Starts using this package as a module.


elements

public java.util.Enumeration elements()
Deprecated. 

Enumerates sub-packages


clone

public java.lang.Object clone()
Returns a clone package.


autoload

public void autoload(java.lang.String name,
                     java.lang.String file,
                     Context context)
Registers an autoload script for the name. If name is not defined when accessed, the registerred file is loaded.

Parameters:
name - variable name
file - the file
context - the context

autoload

public void autoload(java.lang.String name,
                     AutoloadHook hook)
Registers an AutoloadHook for the name. If name is not defined when accessed, the registerred AutoloadHook is executed.

Parameters:
name - variable name
hook - the AutoloadHook

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

get

public java.lang.Object get(java.lang.String interned)
Gets the value of a variable.

Parameters:
interned - the name of the variable, which must be intern'd
Returns:
the value

lookup

public NamedValue lookup(java.lang.String interned)
Looks for a name-value binding in the symbol table chain.

Parameters:
interned - the name of the variable, which must be intern'd
Returns:
a NamedValue

set

public void set(java.lang.String interned,
                java.lang.Object value)
Defines a name-value binding in the symbol table.

Parameters:
interned - the name of the variable, which must be intern'd
value - the new value
Throws:
java.lang.IllegalStateException - thrown when the specified symbol has been defined as a constant.

setConstant

public void setConstant(java.lang.String interned,
                        java.lang.Object value)
Defines a constant in the symbol table.

Parameters:
interned - the name of the variable, which must be intern'd
value - the constant value
Throws:
java.lang.IllegalStateException - thrown when the specified symbol has been defined as a constant

clear

public void clear()
Deletes all name-value bindings.


size

public int size()

bindings

public java.util.Enumeration bindings()
Returns an enumeration of the NamedValues in the symbol table.

Returns:
an enumeration of the NamedValues
See Also:
NamedValue

keys

public java.util.Enumeration keys()
Returns an enumeration of the keys in the symbol table.

Returns:
an enumeration of the keys

values

public java.util.Enumeration values()
Returns an enumeration of the values in the symbol table.

Returns:
an enumeration of the values