Why are TreePath constructed by Object[] instead of TreeNode[]? - java

From the Java API, it seems that nodes of a JTree are instances of TreeNode. In addition, all TreePaths returned seems to be instances of TreeNode. So, why are TreePaths represented by Object[] instead of TreeNode[]? This give raise to inconvenience when using these classes.
Thanks.

See this explanation from the Java tutorial:
Interestingly, the TreeModel interface accepts any kind of object as a tree node. It does not require that nodes be represented by DefaultMutableTreeNode objects, or even that nodes implement the TreeNode interface. Thus, if the TreeNode interface is not suitable for your tree model, feel free to devise your own representation for tree nodes. For example, if you have a pre-existing hierarchical data structure, you do not need to duplicate it or force it into the TreeNode mold. You just need to implement your tree model so that it uses the information in the existing data structure.

Related

Increasing elements in a data structure that follows the Composite pattern

I have often read/heard that the composite pattern is a good solution to represent hierarchical data structures like binary trees, which it is great to explain this pattern because internal nodes are composite objects and leaves are leaf objects. I can appreciate that using this pattern, it is easy to visit every element in an uniform way.
However, I am not so sure if it is the best example if you are considering that a tree is filling on demand (every time an insert method is executed) because we have to convert a leaf to composite object many times (e.g. when leaf has to add a child). To convert a leaf object, I imagine a tricky way like (inspired by become: from Smalltalk I guess):
aComposite = aLeaf.becomeComposite();
aComposite.addChild(newElement);
//destroy aLeaf (bad time performance)
To sum up, Is a good example the use of a tree-like structure to illustrate the composite pattern if this kind of structure is born commonly empty and then you have to add/insert elements?
GoF states the Intent of Composite as follows:
"Compose objects into tree structures to represent part-whole hierarchies. ..... treat individual object and compositions of objects uniformly"
So a tree is not so much a structure to illustrate Composite, rather a tree is the structure by which composite is defined and operates. Its also worth remembering that for the purposes of Composite, a tree can be a binary tree (2 children), a linked list (one child) or can be composed of nodes with a variable number of children .
Its quite normal to build a tree from nothing. Consider an arithmetic expression parser, building a composite "parse" tree. The parser will start from nothing and create leaf nodes for terminal symbols (like + - * / braces, numbers) and composite nodes to combine the terminals perform the calculations. The parser constructs the tree such that invoking evaluate() on the head node will cause a traversal to evaluate the expression.
I use this example to show that a tree can be built bottom up, never having to "convert a leaf to composite object".
If your application builds the tree top down, or progressively in stages, its hard to see that matters, because the build process will consist of creating appropriate nodes and inserting them in a way that makes sense for the application.
If converting leaf nodes to composite nodes is problematic in any specific application, then for sure you make to look at ways to minimise the overhead in that situation. but its only a valid Composite structure when the tree is built!

Tree data set migration to CQ/AEM

I have a data set(excel spreadsheet) which needs to be migrated to CQ/AEM.
eg:
http://imgur.com/lbYIExf
Each entry in the excel cell should correspond to a node in CRX content repository. My initial thought is to use apache POI to parse the excel and construct a data structure containing the tree elements. Now, How do i construct this tree data structure before iterating persisting to CRX? Map containing map containing map(depth of 5) will be very complex to manage.
If there is any efficient design idea that you can share, please let me know.
To construct tree element you can create a class Node.
Class Node will have reference of its parent, reference of list of child nodes and actual Node data (corresponding to jcr node)
By using this class you can create a tree structure.
Use DFS/BFS to traverse the tree and then convert those node to jcr Nodes.

Save to XML DefaultMutableTreeNode - org.w3c.dom.Node

I am writing a simple configuration helper program to hold variables-value combinations and have the ability to save and load to XML files.
The basic structure is built around the DefaultMutableTreeNode that I extended with a few methods. For example the 'get(String variable)' method recursively searches the children of the node to find the value of the specified variable.
String user = conf.get("Username");
After investigating saving to XML using the DOM I realized it uses a similar hierarchical node structure, rather than double handling I considered using the 'org.w3c.dom.Node' for everything.
As I can't extend the Node interface in the same way (only implement it), what class actually implements Node? I can see here that IOMetadataNode, SOAPPart implement the Node interface and there are a heap of subinterfaces.
Is a certain amount of double handling fine?
I have been able to do away with the double handling using MOXy
Following the example about JAXBCustomizations I used the below syntax to match the XML elements with the variables in my class. So long as there are setter/getter methods with matching names (For example var needs getVar and setVar methods) it's trivial to save to and load from XML.
#XmlRootElement
#XmlType(propOrder={"var", "value", "childNodes"})
The children nodes are instances of the same class as the parent and they are correctly nested underneath the parent.

Add tree as a sub tree to different type of tree

I have tree, T, structure in an Eclipse plugin which has a different type of parent or node. Now I want to add a tree, T1, to it which has a node of a different type. So what would be the clear and robust approach to do this task as it is assumed that it would get a lots of load so approach should be very visionary?
My approach: As I am very new in this, in my opinion I have to just manage (anyhow) the getParent() method for T1 of same type to T, as this would be the down most sub tree of tree T.
(I want to ask this question for a discussion point of view. I want to just clear my views and approach.)
Make/use a tree interface or abstract class that all the other trees implement/extend, then create a list of that interface or abstract class. E.g:
ArrayList<TreeInterface> treeArray = new ArrayList<TreeInterface>();
This interface/abstract class should have all of the methods, getters and setters you will need to access the data you want later on.
If you can't do that, I think you can only make an array of Object to satisfy this...

Queue/LinkedList serialization in Java

I have an implementation of a queue (Queue class, which is basically a linked list) - it consists of linked list of objects of Element class (with Previous/Next references) and a header for first and last element, which is also an object of Element class. I would like to serialize the Queue. The question is - how to serialize a bidirectional linked list?
There is a lot of ways to do that. You can implement Serializable and take care about persisting all the objects. You can save all the data and relations in some structured XML. You can go with JSON.
The only problem can be if your Queue is generic (like the one in the standard library), you are going to have hard time saving an object of unknown type.

Categories