views:

597

answers:

5

I've got two classes; one interprets commands, and sends these commands to another class which executes them. I then want the class which executes the commands to be able to send the results of this execution back to the interpreter class. The problem is that these results are hierarchal in nature.

Right now the only thing I know of in the Java API that will let me do this is the DOM. I really don't want to futz around with the DOM, creating a new document and then sending a piece of it back to the other class; it just seems like a BIG MESS. Is there anything else I can use?

+1  A: 

Do you really need a specialized tree data structure if you can just create a class consisting of nodes? Just use objects from a class, say Result, for the result, and add fields to Result for the possible child nodes. The fields can have type Result as well, if so desired.

Thomas
I think this is what I need...just wanted to see if it was already available before reinventing the wheel
I don't think this is reinventing the wheel. Every tree has a different structure. What are the possible types of nodes? What types of nodes can be children of what other nodes? Is there an ordering amongst child nodes? How many child nodes can there be at most? At least? The DOM was designed as one very particular tree structure that represents SGML/XML.
Thomas
+1  A: 

You could try some of the data structures available out there, like JDots http://sourceforge.net/projects/jdots/

JDots (Java Dynamic Object Tree Software) is a library/framework to create a dynamic tree of Java objects, for active method communication/propagation

JuanZe
+1  A: 

You could use a simplified DOM abstraction with less boilerplate code like JDOM... or how about just some nested Maps for quick and dirty?

[result:true, childResults:[result:true, childResults:[], result:false, childResults:[]]]

toss some recursion in there to put the thing together... and bob's your uncle.

EDIT:

if you need ordering in the child results, you'll want to toss a list in there... this is probably all a bit messy in Java, but I've been in Groovy land so long that it jumped right out at me.. the syntactic sugar for lists and maps makes it easy (too easy?) to bust out stuff like this... if you have control over the project, you might want to look at a little Groovy :)

danb
A: 

I think what you're looking for is the command pattern and not an API call. I could be wrong. Since the DOM is a xml/html creature, are you talking about a web application? something else?

Jim Barrows
A: 

I would think not about java data structured but rather about correspondent design pattern for your problem.

For hierarchical structure I can suggest Composite pattern.

Roman