cds.util
Class TreeHashMap

java.lang.Object
  extended bycds.util.TreeHashMap

public class TreeHashMap
extends java.lang.Object

TreeHashMap: a special HashMap managing hierarchical entries.

A normal HashMap contains pairs of key/value entries. If a value can itself be a HashMap, the it constitutes a node from which a new set of key/values can be defined. Keys in different branches can be identicals.

A TreeHashMap is a tree where every node can have many nodes an the next level. Every node, including leaves have a path. Only leaves contain an actual value. Keys are considered to be only Strings. From the root, a key can be defined by its path "A.B.C" : A and B have HashMaps as values, C is a normal key, which returns its value. If C is still a hashmap, it returns a subtree, also of type TreeHashMap.
It is possible to specify a current node, and add items by specifying the key from this node on (relative path) or specify an absolute path for a node. Extending keys to other objects would require an array of objects to specify a path, instead of the simple syntax A.B.C

Special iterators allow to browse the whole tree, or only one branch.

Version:
1.0 April 2003. Created to manage the Parameters objects.
Author:
Marc Wenger

Nested Class Summary
protected  class TreeHashMap.TreeIterator
          Leaf Iterator: returns only the leaf elements from a given node
 
Field Summary
private static char defaultPathSeparator
           
private  java.util.HashMap hm
           
private  java.lang.String nodeName
           
private static char pathSeparator
           
 
Constructor Summary
  TreeHashMap()
          Constructor.
private TreeHashMap(java.lang.String name)
          Constructor.
 
Method Summary
 boolean createNode(java.lang.String key)
          Create a node.
 java.lang.Object get(java.lang.String key)
          returns the object corresponding to the key
 java.util.HashMap getHashMap()
          Returns the internal HashMap used for this node
private  java.lang.String getKey(java.lang.String path, int pos)
          Returns the key item defined by a former splitKey method
 java.lang.String getName()
          Returns the name of a node.
private  java.lang.String getPath(java.lang.String path, int pos)
          Returns the remainder path defined by a former splitKey method
 char getPathSeparator()
          Returns the current path separator
static void main(java.lang.String[] args)
           
 boolean put(java.lang.String key, java.lang.Object value)
          Add an item to the tree.
 boolean put(java.lang.String key, java.lang.Object value, boolean create)
          Add an item to the tree.
 char setPathSeparator(char sep)
          Defines a new path separator
private  int splitKey(java.lang.String path)
          Split a key.
 TreeHashMap.TreeIterator treeIterator()
          Accessor to a Leaf Iterator
 TreeHashMap.TreeIterator treeIterator(int type)
           
 TreeHashMap.TreeIterator treeIterator(int type, int level)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultPathSeparator

private static final char defaultPathSeparator
See Also:
Constant Field Values

pathSeparator

private static char pathSeparator

hm

private java.util.HashMap hm

nodeName

private java.lang.String nodeName
Constructor Detail

TreeHashMap

public TreeHashMap()
Constructor. Defines also the object as root and defines this root as current node.


TreeHashMap

private TreeHashMap(java.lang.String name)
Constructor. Defines a node in a hierarchie. Its name is its keyname in its father.

Parameters:
name - name for this non root node.
Method Detail

getHashMap

public java.util.HashMap getHashMap()
Returns the internal HashMap used for this node

Returns:
the hashMap of the node

setPathSeparator

public char setPathSeparator(char sep)
Defines a new path separator

Parameters:
sep - new path separator. If it is null, resets the default separator. The default separator is defined as '/'.
Returns:
the previous separator

getPathSeparator

public char getPathSeparator()
Returns the current path separator

Returns:
the path separator character

getName

public java.lang.String getName()
Returns the name of a node. This name is its key in its parent node

Returns:
the node name used to build the path up

createNode

public boolean createNode(java.lang.String key)
Create a node. All elements in the path that do not yet exist are created.

Parameters:
key - path which is created after this call. Intermediate level may exist or not. They will be created.
Returns:
true if the path is created or already exists. false if the path contains an already existing leaf

put

public boolean put(java.lang.String key,
                   java.lang.Object value)
            throws ExistenceException
Add an item to the tree. If the path contains an unknown term, the item is not added. If the path exists, the value is added, whether the leaf name exists (value replaced) or not (value created).

Parameters:
key - key path
value - value to add
Returns:
true if the item has been added, false otherwise.
Throws:
ExistenceException

put

public boolean put(java.lang.String key,
                   java.lang.Object value,
                   boolean create)
            throws ExistenceException
Add an item to the tree. If the path contains un unknow term, it will be added only if the create parameter is set to true

Parameters:
key - path defining the key.It can be relative or absolute
value - object to write as value at the given key. It can be any kind of object including a TreeHashMap, which will then be considered as a new node in the tree.
Throws:
ExistenceException

get

public java.lang.Object get(java.lang.String key)
returns the object corresponding to the key

Parameters:
key - path to the value
Returns:
the value corresponding to the path, or null if it does not exist. The returned object can be either an actual value, or a TreeHashMap object, if the key points to a node.

splitKey

private int splitKey(java.lang.String path)
Split a key. Returns the position of the next separator

Parameters:
path - key path to split
Returns:
the position of the next separator, or -1 if there is none

getKey

private java.lang.String getKey(java.lang.String path,
                                int pos)
Returns the key item defined by a former splitKey method

Parameters:
path - key path fromwhich the first key name must be extracted
pos - value returned by splitKey applied on the same path
Returns:
the extracted key name

getPath

private java.lang.String getPath(java.lang.String path,
                                 int pos)
Returns the remainder path defined by a former splitKey method

Parameters:
path - key path fromwhich the first key name must be extracted
pos - value returned by splitKey applied on the same path
Returns:
the remained path (or null if none)

treeIterator

public TreeHashMap.TreeIterator treeIterator()
Accessor to a Leaf Iterator


treeIterator

public TreeHashMap.TreeIterator treeIterator(int type)

treeIterator

public TreeHashMap.TreeIterator treeIterator(int type,
                                             int level)

main

public static void main(java.lang.String[] args)