pnuts.lang
Class Package

java.lang.Object
  |
  +--pnuts.lang.Package
Direct Known Subclasses:
ProtectedPackage, ScriptPackage

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
 pnuts.lang.SymbolTable autoloadTable
          This represents a set of names which are registered as autoloaded.
protected  java.util.Vector children
           
static Package globalPackage
          The package with name "".
protected  java.lang.String name
          The name of the package.
protected static java.util.Hashtable packages
          All packages with a non-null name
protected  Package parent
           
protected  pnuts.lang.SymbolTable requireTable
          This represents a set of loaded script names.
protected  pnuts.lang.SymbolTable table
           
protected  pnuts.lang.SymbolTable unitTable
          This represents a set of registered QuantityFactory.
 
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.
 
Method Summary
 void clear(java.lang.String symbol)
          Deprecated.  
 void clear(java.lang.String symbol, Context context)
          Deletes a symbol from the package.
 java.lang.Object clone()
           
 boolean defined(java.lang.String name)
          Deprecated.  
 boolean defined(java.lang.String name, Context context)
           
 java.util.Enumeration elements()
          Enumerates sub-packages.
static Package find(java.lang.String pkg)
           
 java.lang.Object get(java.lang.String symbol)
          Deprecated.  
 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.
 Package getParent()
          Returns the parent package.
protected  void init()
          This method is called by the constructors.
protected  void init(Context context)
          This method is called when the package become the current package with package() function.
 java.util.Enumeration keys()
          Returns an enumeration of the names in the package.
protected  Value lookup(java.lang.String symbol, Context context)
          Lookup the symbol in the package.
static void registerQuantityFactory(java.lang.String unit, QuantityFactory fac)
           
static void remove(java.lang.String name)
          Removes the specified package.
static void reset()
          Remove all packages
 void save(java.io.OutputStream s)
          Saves the package to an outputStream.
 void set(java.lang.String symbol, java.lang.Object obj, Context context)
          Set a value of a symbol in the package.
static void setPackageFactory(PackageFactory factory)
          When an instance of PackageFactory is registered by this method, the package() builtin function calls its PackageFactory.createPackage() method.
 int size()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

packages

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

globalPackage

public static Package globalPackage
The package with name "".

table

protected transient pnuts.lang.SymbolTable table

children

protected transient java.util.Vector children

parent

protected transient Package parent

name

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

autoloadTable

public pnuts.lang.SymbolTable autoloadTable
This represents a set of names which are registered as autoloaded. This is only for global package.

unitTable

protected pnuts.lang.SymbolTable unitTable
This represents a set of registered QuantityFactory. This is only for global package.

requireTable

protected pnuts.lang.SymbolTable requireTable
This represents a set of loaded script names. This is only for global 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.
Method Detail

getGlobalPackage

public static Package getGlobalPackage()
Returns:
the global package

setPackageFactory

public static void setPackageFactory(PackageFactory factory)
When an instance of PackageFactory is registered by this method, the package() builtin function calls its PackageFactory.createPackage() method.

getPackage

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

defined

public boolean defined(java.lang.String name)
Deprecated.  

Returns:
true if name is defined in the package.

defined

public boolean defined(java.lang.String name,
                       Context context)
Returns:
true if name is defined in the package.

get

public java.lang.Object get(java.lang.String symbol)
Deprecated.  

Returns:
the value of specified variable 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 and this.parent is not null, 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.
Specified by:
set in interface Property
Parameters:
symbol - an interned name of variable
obj - the value of the variable

clear

public void clear(java.lang.String symbol)
Deprecated.  

Deletes a symbol from the package.
Parameters:
symbol - a name of variable to be deleted

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)
Removes the specified package.

find

public static Package find(java.lang.String pkg)
Parameters:
pkg - a name of package to look.
Returns:
a package with name "pkg" if it exits.

reset

public static void reset()
Remove all packages

init

protected void init()
This method is called by the constructors.

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.

keys

public java.util.Enumeration keys()
Returns an enumeration of the names in the package.

getParent

public Package getParent()
Returns the parent package.

size

public int size()
Returns:
the number of symbols.

lookup

protected Value lookup(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.
Parameters:
symbol - intern'ed string

save

public void save(java.io.OutputStream s)
Saves the package to an outputStream. If a value of a symbol can not be serialized, the symbol is ignored.

elements

public java.util.Enumeration elements()
Enumerates sub-packages.

registerQuantityFactory

public static void registerQuantityFactory(java.lang.String unit,
                                           QuantityFactory fac)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

toString

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