This is the sort of tree I was asking about (below) to represent the memory file system. I want to avoid reusing DefaultTreeModel for a data structure since it is Swing-oriented (look at all the listeners!).

I'd expect to specialize the generic thus:

public class RamTree extends Tree<String, RamFileObject> {
    public final Node root = new Node("");
}

The rest is encapsulated in the Node class.

An aside: where would a class like Tree belong? It is awfully general to be in VFS. Should I be mentioning this to the collections folk?


Cheers, --binkley


public class Tree <Path, Leaf> {
/**
* Creates a new root node for a tree with the given <var>path</var>. For example, if the type of <var>Path</var> is [EMAIL PROTECTED] String}, a common choice for root path would be the empty string.
*
* @param path the root node name
*
* @return the new root node
*/
public Node createRootNode(final Path path) {
return new Node(path);
}


    public class Node {
        private final Path path;
        private final Node parent;
        private final Map<Path, Node> children = new HashMap<Path, Node>();

        private Leaf content;

/**
* Constructs a new root [EMAIL PROTECTED] Node} with the given <var>path</var>.
* The root is itself its own parent node.
*
* @param path the root node name
*
* @see #Node(Path, Node)
*/
protected Node(final Path path) {
if (null == path) throw new NullPointerException();


            this.parent = this;
            this.path = path;
        }

        /**
         * Constructs a new non-root [EMAIL PROTECTED] Node} with the given
         * <var>path</var> and <var>parent</var> node.  The parent node
         * <strong>must</strong> be distinct from this node.
         *
         * @param path the child node name
         * @param parent the parent node
         *
         * @see #Node(Path)
         */
        protected Node(final Path path, final Node parent) {
            if (null == parent) throw new NullPointerException();
            if (this == parent) throw new IllegalArgumentException();
            if (null == path) throw new NullPointerException();

            this.parent = parent;
            this.path = path;
        }

        public boolean isRoot() {
            return this == parent;
        }

        public Node getParent() {
            return parent;
        }

        public Path getPath() {
            return path;
        }

        public boolean hasContent() {
            return null != content;
        }

        public Leaf getContent() {
            if (null == content) throw new NullPointerException();

            return content;
        }

        public void setContent(final Leaf content) {
            if (null == content) throw new NullPointerException();

            this.content = content;
        }

        public void removeContent() {
            if (null != content) throw new IllegalStateException();

            this.content = null;
        }

        public boolean hasChild(final Path path) {
            if (null == path) throw new NullPointerException();

            return children.containsKey(path);
        }

        public Node getChild(final Path path) {
            if (null == path) throw new NullPointerException();

            return children.get(path);
        }

        public Node addChild(final Path path) {
            if (null == path) throw new NullPointerException();
            if (hasChild(path)) throw new IllegalArgumentException();

            final Node child = new Node(path, this);

            children.put(path, child);

            return child;
        }

        public void removeChild(final Path path) {
            if (null == path) throw new NullPointerException();
            if (!hasChild(path)) throw new IllegalArgumentException();

            children.remove(path);

            // Do not return the removed node; it has no valid parent
        }

        public Collection<Node> getChildren() {
            return Collections.unmodifiableCollection(children.values());
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to