• Post Reply Bookmark Topic Watch Topic
  • New Topic

MutableTreeNode that does not allow duplicate Objects  RSS feed

 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I need is a MutableTreeNode that does not allow two references to the same object.
What I am thinking is make a subclass of DefaultMutableTreeNode and override the add method.

add(MutableTreeNode newchild){
// checks if the object referenced by newchild is referenced by any node in this node
// and if not referenced
super.add(newchild);
}

Would this work? Is it how such a thing is usually done?

Also, to check if two objects are the same or not, the JVM calls the equals method, I believe.
But the argument to equals() is an object of the same class, isn't it? For a String it is
boolean equals(String otherString){}
and for an ArrayList,
boolean equals(ArrayList otherArrayList){}

or are the equals methods all for Objects?
boolean equals(Object newObject){}

What if you have a class Dog and you want the program to consider two Dogs to be the same Dog if they have the same name? Then you would get an exception by trying to pass a Cat object to the equals method because the equals method asks for the name, which does not exist in the Cat object. But I don't want an exception. I want a return of false because a Cat is not a Dog!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, overriding the add method doesn't sound like a bad idea to me. As far as your concern over your equals method, here is an example of how you can avoid that:

 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About overriding the add method, there is one thing you may want to consider: what if you want to add an entire (sub)tree? Do you ignore it completely?

I've written a SetTreeNode (from scratch) which is performs more or less the same (but sorted), but I've given it a completely new add method that merges the two nodes:
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the advice. Actually, I have an idea. I'll make a subclass of DefaultMutableTreeNode, maybe calling it DogNode, or something like that, and override the constructor so that an object can only be created by giving it a Dog object. Then I'll override the add(MutableTreeNode newChild) method so that it only takes nodes that are type DogNode, have no children, and reference a Dog which has a different name from all the other Dogs in the tree.

That brings me to the next question. In DefaultMutableTreeNode, the add method looks like this:
void add(MutableTreeNode newChild)
If I write a method that looks like this
void add(DogNode newPuppy)
would that be an override of the original add method, or an overloading of it? If someone tries to call the add method with another MutableTreeNode besides DogNode, would it work?
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be overloading. You would need to check if the argument is an instance of DogNode, and throw an IllegalArgumentException if it isn't.
Also, do the same for setUserObject - if you don't then changing the object will be possible. Perhaps you want to make sure the object is only set once:

I would really include that check against getUserObject(), because this method may be called from the constructor in a future version (it isn't now). Without the null check you could cause the constructor to throw an exception when you don't want it.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!