jtree - comparing two TreeNode (or DefaultMutableTreeNode) objects in Java Comparator -


my goal simple today, trying work out proper way implement compareto (or comparable) interface class extends defaultmutabletreenode.

the problem this: have class represents times. i've written compareto method (which works desire) i've tested out arrays.sort() marvelous results.

now lets have jtree bunches of different objects, this:

    new specialnode("zomg string!"); // add group of nodes right here     new specialnode(new time("8:55 pm"));     new specialnode(new sometypeyouveneverheardof()); 

so, being professional programmer, start coding without forethought whatsoever. here specialnode class:

class specialnode extends defaultmutabletreenode implements comparator<specialnode> {     public int compareto(specialnode sn)     {         // not not work correctly (read: @ all)         // but, sub-par, how type information out of userobject         // can cast correctly , call correct compareto method!!         return this.getuserobject().tostring().compareto(sn.getuserobject().tostring());     } } 

ok, if didn't read comments (which, admit, didn't); problem within compareto method of specialnode, have access userobject. sadly, not know userobject used be, , such, cannot cast call correct compareto method!

this pain, since i've written several compareto methods in of classes added tree. can guy out , drop me hint?

tl;dr - how type information out of generic object defaultmutabletreenode stores? if not possible, how should go comparing 2 instances of specialnode when don't know may contain!

thanks in advance.

i'm assuming can't have treenode each type of data. make sense in use case comparisons if type different?

ideas:

  1. can simplenode know of possible types , instanceof , casts correct type comparable? way have handled few years ago.

  2. how feel unchecked warnings? had similar problem before using jlist , couldn't quite make compiler happy (i gave swing default model make life easier). maybe else improve on answer?

    class specialnode<t extends comparable<t>> extends defaultmutabletreenode                                            implements comparable<specialnode> {   t typeduserobject;   specialnode(t t)   {      this.typeduserobject = t;      setuserobject(t);   }     public int compareto(specialnode node)   {       if(typeduserobject.getclass().isinstance(node.typeduserobject))       {           t otherobj = (t) node.typeduserobject;           return typeduserobject.compareto(otherobj);       }       else       {           //what going if they're not same type?           return -1;       }   }   } 

edit: if know should same type - eliminates check

class specialnode<t extends comparable<t>> extends defaultmutabletreenode                                           implements comparable<specialnode<t>> {   t typeduserobject;   specialnode(t t)   {      this.typeduserobject = t;      setuserobject(t);   }     public int compareto(specialnode<t> node)   {      return typeduserobject.compareto(node.typeduserobject);   }     } 

if don't want code in node (i don't think would), might create separate class implements comparator<specialnode<t>>


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -