Folks,
I've been stuck to boogery for the last couple of hours... I can't fathom what to do about this compiler error... Never mind the screwy logic (yet)... I'm just trying to get the sooker to compile.
Compiling 3 source files to C:\Java\home\src\nb-projects\denismurphy2008\build\classes
C:\Java\home\src\nb-projects\denismurphy2008\src\tree\FileTree.java:48: an enclosing instance that contains tree.Tree.Node is required
recurse(new Tree.Node(dir), file);
1 error
Google finds just about diddly... plenty of links, with very little useful content.
Does anyone know what that cryptic message means exactly? and/or what to do about it.
I get the impression from "fiddling" with the code to try to get
something to compile that the message means "you can't access the Tree.Node class from an instance of the Tree class"... rendering it effectively useless for my purposed because I can't create an instance of Node outside of the Tree class, and I need it to build my tree.
Do you think I should make Node a static inner class? What ramifications does that have? Can you point me at a good article?
Or maybe I should pull Node out of the Tree, and make it a public class in it's own right?
Any suggestions welcome... Once I get over this compiler error I'll get to work on the screwy logic... I know it's assAbout at the moment.
Cheers. Keith.
tree/FileTree.java
package tree;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Iterator;
/**
* Each FileTree object describes a hierarchical collection of files and
* folders, in which a folder may contain any number of folders and files.
* Within a given directory, all items have a distinct name.
*/
public class FileTree {
// The directory hierarchy is represented by a Tree of File's.
private final Tree<File> tree;
/**
* Construct a new FileTree containing everything in or under the given 'path'.
* @param path String
* @throws java.io.FileNotFoundException if the given path is not found.
*/
public FileTree(String path) throws java.io.FileNotFoundException {
File root = new File(path);
if ( !root.exists() ) {
throw new FileNotFoundException("root path '"+root.getAbsolutePath()+"' does not exist!");
} else if ( !root.isDirectory() ) {
throw new FileNotFoundException("root path '"+root.getAbsolutePath()+"' is not a directory!");
}
this.tree = new Tree<File>();
this.recurse(tree.root(), root);
}
public void print(PrintStream out) {
for ( Iterator<Tree<File>.Node>it=tree.iterator(); it.hasNext(); ) {
Tree<File>.Node node = it.next();
System.out.println("DEBUG: node="+node);
File file = (File) node.element();
out.println(file.getAbsolutePath());
}
}
private void recurse(Tree<File>.Node node, File dir) {
for ( File file : dir.listFiles() ) {
if ( file.isDirectory() ) {
recurse(new Tree<File>.Node(dir), file); // <<<< THIS LINE >>>>
} else {
tree.add(node, file);
}
}
}
}
tree/Tree.java
package tree;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
import java.util.PriorityQueue;
public class Tree<E extends Comparable<E>> implements Iterable<Tree<E>.Node>
{
private Node root;
public Tree() {
}
public void add(Node parent, E element) {
//parent.addChild(new Node(parent, element));
parent.addChild(new Node(element));
}
public Node root() {
return this.root;
}
@Override
public Iterator<Tree<E>.Node> iterator() {
System.out.println("DEBUG: Tree.iterator("+root+")");
return root.iterator();
}
@Override
public String toString() {
return "Tree(root="+root+")";
}
//////////////////////////////////////////////////////////
// Tree.Node
//////////////////////////////////////////////////////////
public class Node implements Iterable<Node>
{
private final E element;
//private final Node parent;
private final List<Node> children;
//public Node(Node parentNode, Object element) {
// this.parent = parentNode;
// this.element = element;
// this.children = new LinkedList<Node>();
//}
public Node(E element) {
this.element = element;
this.children = new LinkedList<Node>();
}
public E element() {
return this.element;
}
public void addChild(Node child) {
children.add(child);
System.out.println("DEBUG: Node.add("+child+")");
}
@Override
public String toString() {
return element.toString(); //+ children.toString();
}
@Override
public Iterator<Node> iterator() {
System.out.println("DEBUG: Node.iterator(children="+children+")");
return new OrderedIterator();
}
public class OrderedIterator implements Iterator<Node> {
private final Queue<Node> kids;
public OrderedIterator() {
System.out.println("DEBUG: new OrderedIterator(children="+children+")");
kids = new PriorityQueue<Node>(children);
}
@Override
public boolean hasNext() { return !kids.isEmpty(); }
@Override
public Node next() { return kids.poll(); }
@Override
public void remove() { throw new UnsupportedOperationException(); }
}
}
}
tree/test/FileTreeTest.java
package tree.test;
import tree.*;
class FileTreeTest
{
public FileTreeTest() {
}
public static void main(String[] args) {
try {
FileTree tree = new FileTree("c:/java/home/src/forums/denismurphy2008");
tree.print(System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
PS: The observant people might notice the similarities to [this thread|http://forums.sun.com/thread.jspa?threadID=5347909]... It's where I started from... Having not written a tree data structure in java before I thought I'd have a crack at it for the sake of the exercise.
Cheers. Keith.