今天我的目标非常简单,我正在尝试找出正确的方法来为我的类(扩展了 DefaultMutableTreeNode)实现compareTo(或Comparable)接口。
问题是这样的:假设我有一个代表时代的完美类。我已经编写了一个非常好的compareTo方法(它按照我的意愿工作),我已经用Arrays.sort()对其进行了测试,结果非常好。
现在假设我有一个包含一堆不同对象的 JTree,如下所示:
new SpecialNode("Zomg a string!"); // add this group of nodes right here
new SpecialNode(new Time("8:55 PM"));
new SpecialNode(new SomeTypeYouveNeverHeardOf());
因此,作为一名专业程序员,我立即开始编码,无需任何深思熟虑。这是我的 SpecialNode 类:
class SpecialNode extends DefaultMutableTreeNode implements Comparator<SpecialNode>
{
public int compareTo(SpecialNode sn)
{
// Not only does this not work correctly (read: at all)
// But, it is sub-par, how do I get the type information out of the userObject
// So I can cast it correctly and call the correct compareTo method!!
return this.getUserObject().toString().compareTo(sn.getUserObject().toString());
}
}
好的,如果您没有阅读评论(承认,您没有阅读);我的问题是,在 SpecialNode 的 CompareTo 方法中,我只能访问 userObject。遗憾的是,我不知道 userObject 曾经是什么,因此,我无法正确地转换它以调用正确的compareTo 方法!
这确实很痛苦,因为我已经在所有将添加到我的树中的类中编写了几个非常好的compareTo 方法。那么有人可以帮助一个人并给我一个提示吗?
tl;dr - 如何从 DefaultMutableTreeNode 存储的通用对象中获取类型信息?如果这是不可能的,当我什至不知道它们可能包含什么时,我应该如何比较 SpecialNode 的两个实例!
提前致谢。
My goal is very simple today, I am trying to work out the proper way to implement compareTo (or the Comparable) interface for my class which extends DefaultMutableTreeNode.
The problem is this: Say I have a perfectly good class that represents times. I've already written a perfectly good compareTo method (which works as I desire) that I've tested out with Arrays.sort() with marvelous results.
Now lets say I have a JTree with bunches of different objects, like this:
new SpecialNode("Zomg a string!"); // add this group of nodes right here
new SpecialNode(new Time("8:55 PM"));
new SpecialNode(new SomeTypeYouveNeverHeardOf());
So, being a professional programmer, I immediately start coding without any forethought whatsoever. Here is my SpecialNode class:
class SpecialNode extends DefaultMutableTreeNode implements Comparator<SpecialNode>
{
public int compareTo(SpecialNode sn)
{
// Not only does this not work correctly (read: at all)
// But, it is sub-par, how do I get the type information out of the userObject
// So I can cast it correctly and call the correct compareTo method!!
return this.getUserObject().toString().compareTo(sn.getUserObject().toString());
}
}
Ok, so if you didn't read the comments (which, admit, you didn't); My problem is that within the compareTo method of SpecialNode, I only have access to the userObject. Sadly, I do not know what the userObject used to be, and as such, I cannot properly cast it to call the correct compareTo method!
This is really a pain, since I've already written several perfectly good compareTo methods in all of the classes which will be added to my tree. So can someone help a guy out and drop me a hint?
tl;dr - How do I get type information out of an generic object that DefaultMutableTreeNode stores? If that is not possible, how should I go about comparing two instances of SpecialNode when I don't even know what they may contain!
Thanks in advance.
发布评论
评论(1)
我假设您不能为每种类型的数据都有一个 TreeNode。如果类型不同,在您的用例中进行比较是否有意义?
想法:
SimpleNode 可以知道所有可能的类型并执行instanceof 并强制转换为Comparable 的正确类型吗?这是我几年前处理它的方式。
您对未经检查的警告有何看法?在使用 JList 之前我也遇到过类似的问题,并且无法让编译器满意(我放弃了 swing 默认模型以使我的生活更轻松)。也许其他人可以改进这个答案?
编辑:如果您知道它们应该是相同的类型 - 消除检查
如果您不希望节点本身中的代码(我不认为我会),您可以创建一个单独的类
实现比较器>
I'm assuming you can't have a TreeNode for each type of data. Does it make sense in your use case to do comparisons if the type is different?
Ideas:
Can SimpleNode know all of the possible types and do instanceof and casts to the correct type for Comparable? This is the way I would have handled it a few years ago.
How do you feel about unchecked warnings? I had a similar problem before using a JList and couldn't quite make the compiler happy (I gave up the swing default model to make my life easier). Maybe someone else could improve on this answer?
Edit: If you know they should be the same type - eliminates check
If you don't want the code in the node itself (I don't think I would), you might create a separate class that
implements Comparator<SpecialNode<T>>