org.xlattice
Class Context

java.lang.Object
  extended by org.xlattice.Context

public class Context
extends java.lang.Object

A naming context consisting of a possibly nested set of name-to-object bindings. If there is a parent context and a key cannot be resolved in this context, an attempt will be made to resolve it in the parent, recursively.

Names added to the context must not be null.

This implementation is intended to be thread-safe.

Author:
Jim Dixon

Constructor Summary
Context()
          Create a context without a parent.
Context(Context parent)
          Create a context with a parent Context.
 
Method Summary
 Context bind(java.lang.String name, java.lang.Object o)
          Bind a name to an Object at this Context level.
 Context getParent()
           
 java.lang.Object lookup(java.lang.String name)
          Looks up a name recursively.
 Context setParent(Context newParent)
          Change the parent Context.
 int size()
           
 void unbind(java.lang.String name)
          Remove a binding from the Context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

public Context()
Create a context without a parent.


Context

public Context(Context parent)
Create a context with a parent Context.

Method Detail

bind

public Context bind(java.lang.String name,
                    java.lang.Object o)
Bind a name to an Object at this Context level. Neither name nor object may be null.

If this context has a parent, the binding at this level will mask any bindings in the parent and above.

Parameters:
name - the name being bound
o - the Object it is bound to
Throws:
java.lang.IllegalArgumentException - if either is null.

lookup

public java.lang.Object lookup(java.lang.String name)
Looks up a name recursively. If the name is bound at this level, the object it is bound to is returned. Otherwise, if there is a parent Context, the value returned by a lookup in the parent Context is returned. If there is no parent and no match, returns null.

Parameters:
name - the name we are attempting to match
Returns:
the value the name is bound to at this or a higher level or null if there is no such value

unbind

public void unbind(java.lang.String name)
Remove a binding from the Context. If there is no such binding, silently ignore the request. Any binding at a higher level, in the parent Context or above, is unaffected by this operation.

Parameters:
name - Name to be unbound.

size

public int size()
Returns:
the number of bindings at this level

getParent

public Context getParent()
Returns:
a reference to the parent Context or null if there is none

setParent

public Context setParent(Context newParent)
Change the parent Context. This method returns a reference to this instance, to allow method calls to be chained.

Parameters:
newParent - New parent Context, possibly null.
Returns:
a reference to the current Context