001    /*
002    @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import java.util.Collections;
007    import java.util.HashMap;
008    import java.util.LinkedList;
009    import java.util.List;
010    import java.util.Map;
011    
012    /**
013     * Simple class to build tree structures
014     * @author Pavel Vlasov 
015     * @version $Revision: 1.4 $
016     */
017    public class TreeNode extends VisitableBase implements Attributable {
018            private TreeNode parent;
019    
020            public TreeNode() {
021                    super();
022            }
023    
024            /* (non-Javadoc)
025             * @see biz.hammurapi.util.Visitable#accept(biz.hammurapi.util.Visitor)
026             */
027            protected void acceptChildren(Visitor visitor) {
028                    childrenVisitable.accept(visitor);
029            }
030    
031            private Map attributes=new HashMap();
032            
033            public void setAttribute(Object key, Object value) {
034                    if (value==null) {
035                            attributes.remove(key);
036                    } else {
037                            attributes.put(key, value);
038                    }
039            }
040            
041            public Object getAttribute(Object key) {
042                    return attributes.get(key);
043            }
044            
045            private List children=new LinkedList();
046            private Visitable childrenVisitable=new CollectionVisitable(children, false);
047            
048            public List getChildren() {
049                    return Collections.unmodifiableList(children);          
050            }
051            
052            public void addChild(Object child) {
053                    children.add(child);
054                    if (child instanceof TreeNode) {
055                            ((TreeNode) child).parent=this;
056                    }
057            }
058            
059            public boolean hasAttribute(String name) {
060                    return attributes.containsKey(name);
061            }
062            /**
063             * @return Returns the parent.
064             */
065            public TreeNode getParent() {
066                    return parent;
067            }
068    
069            public Object removeAttribute(Object key) {
070                    return attributes.remove(key);
071            }
072    
073    }