Reputation: 97
I've created a class to mimic a file structure for a program (a text-based game) I'm working on. This is a simplified version:
public class Dir {
public Dir(String name, Dir[] subdirs) {
this.name = name;
this.subdirs = subdirs;
}
public String name; //directory name
public Dir[] subdirs; //Sub-directories
}
The structure would be created using something like this (only much, much bigger):
private Dir root = new Dir("root",new Dir[]{
new Dir("first",new Dir[]{
new Dir("child1",null),
new Dir("child2",null),
new Dir("child3",new Dir[]{
new Dir("child3-1",null)
})
}),
new Dir("second",null),
});
And finally, the current directory is tracked in the variable currentDir, and will change arbitrarily based on user input:
Dir currentDir = root.subdir[0].subdir[3].subdir[0];
I want to be able to find the parent object of a given object. In this case, currentDir has a parent named "child3", which has a parent named "first", which has a parent named "root", which doesn't have a parent. How best to go about that? Also, any tips on a better way to do this are appreciated - I've plenty of programming experience, just not a lot in Java.
Edit:
I ended up creating a recursive subroutine, to be run once the directories have been set up:
private void setParent(Dir thisDir) {
//Loop through every subdir
for(Dir tmp : thisDir.subdirs) {
//set this as the parent on each sub-dir
tmp.parent = thisDir;
//then call setParent on each sub-dir
setParent(tmp);
}
}
I still have to track any changes to the parent if a directory is moved, but this works, for now at least.
Upvotes: 2
Views: 1021
Reputation: 421130
You could have a parentDir
reference in each Dir
object.
In the constructor of Dir
you'd do something like
for (Dir subdir : subdirs)
subdir.parent = this;
I realize that it introduces some redundancy and annoying invariants in your code. I guess the alternative is to have a simple function that recursively finds the parent of a dir-object by searching from the root. Could be done by something like this:
Dir findParent(Dir root, Dir d) {
if (Arrays.asList(subdirs).contains(d))
return this;
for (Dir subdir : subdirs) {
Dir parent = findParent(subdir, d);
if (parent != null)
return parent;
}
return null;
}
A side-note: I strongly suggest you use empty arrays instead of null
for directories that don't have any children. That avoids a lot of conditional code (if-statements).
Upvotes: 3
Reputation: 2295
Either store the parentDir for each Dir, just as aioobe suggested, search for the parent every time you want it (very bad idea), or use an existing library, like classpath-explorer. That might just do what you need.
JDK 7 also has some more advanced file handling stuff, so if your Dir() structure reflects the actual file system, you might not even need to implement it yourself.
Upvotes: 0
Reputation: 24630
Add a parent reference to your Dir class and climb-up the tree:
public class Dir {
public Dir(String name, Dir parent, Dir[] subdirs) {
this.name = name;
this.subdirs = subdirs;
this.parent = parent;
}
public String name; //directory name
public Dir[] subdirs; //Sub-directories
public Dir parent;
}
But your one liner instantiation won't work ...
Upvotes: 0