问题描述
我们有一个OO代码库,在很多情况下 hashcode()
和 equals()
根本不工作,主要是由于以下原因:
We've got an OO codebase where in quite a lot of cases hashcode()
and equals()
simply don't work, mostly for the following reason:
这是Joshua Bloch撰写的Effective Java的引用,在这篇文章中有更多关于该主题的文章:
That's a quote from "Effective Java" by Joshua Bloch and there's more on that subject in a great Artima article here:
我们完全没问题,这不是这个问题的关键。
And we're perfectly fine with that, this is not what this question is about.
这个问题是:看来这是一个事实,在某些情况下你不能满足 equals()
合同,什么是一个干净的自动方式atically make hashcode()
和 equals()
抛出UnsupportedOperationException?
The question is: seen that it is a fact that in some case you cannot satisfy the equals()
contract, what would be a clean way to automatically make hashcode()
and equals()
throw an UnsupportedOperationException?
注释是否有效?我正在考虑像 @NotNull
这样的事情:每个 @NotNull
合同违规都会自动抛出异常而你什么都没有除了用 @NotNull
注释你的参数/返回值之外还要做。
Would an annotation work? I'm thinking about something like @NotNull
: every @NotNull
contract violation does throw an exception automatically and you have nothing else to do besides annotating your parameters/return value with @NotNull
.
这很方便,因为它是8个字符( @NotNull)而不是不断重复相同的验证/抛出异常代码。
It's convenient, because it's 8 characters ("@NotNull") instead of constantly repeating the same verification/throw exception code.
在我关注的情况下,在每个<$ c $的实现中c> hashCode()/ equals()毫无意义,我们总是重复同样的事情:
In the case that I'm concerned about, in every implementation where hashCode()/equals()
makes no sense, we're always repeating the same thing:
@Override
public int hashCode() {
throw new UnsupportedOperationException( "contract violation: calling hashCode() on such an object makes no sense" );
}
@Override
public boolean equals( Object o ) {
throw new UnsupportedOperationException( "contract violation: calling equals() on such an object makes no sense" );
}
然而这很容易出错:我们可能会错误地忘记剪切/粘贴这个并且它可能导致用户滥用这些对象(例如通过尝试将它们放入默认的Java集合中)。
However this is error prone: we may by mistake forget to cut/paste this and it may results in users misusing such objects (say by trying to put them in the default Java collections).
或者如果无法进行注释以创建此行为,AOP会起作用吗?
Or if annotation can't be made to create this behavior, would AOP work?
有趣的是真正的问题是它存在 hashCode()
和在Java层次结构顶部的equals()
在某些情况下根本没有意义。但是,我们如何干净地处理这个问题?
Interestingly the real issue it the very presence of hashCode()
and equals()
at the top of the Java hierarchy which simply makes no sense in quite some cases. But then how do we deal with this problem cleanly?
推荐答案
为什么不让你的IDE(Eclipse / NetBeans / IntelliJ) )为您生成 hashCode()
和 equals()
方法。他们在这方面做得很好。
Why don't you let your IDE (Eclipse/NetBeans/IntelliJ) generate the hashCode()
and equals()
methods for you. They are doing quite a good job at it.
当然,AOP会起作用,但这很复杂。这意味着你将无法使用几乎没有集合或实用程序的这些对象。
AOP will work, of course, but it's quite a complication. And this will mean you won't be able to use these objects with almost no collection or utility.
另一个逻辑解决方案是删除那些方法的实现它们不起作用,thsus实际上只留下 Object
中的实现。
The other logical solution is to just remove the implementations of those methods where they do not work, thsus effectively leaving only the implementations in Object
.
这篇关于Java:调用hashCode()和equals()时自动抛出UnsupportedOperationException的干净方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!