50 lines
922 B
Plaintext
50 lines
922 B
Plaintext
Perhaps counter-intuitively, a ``++compareTo++`` method is _expected_ to throw a ``++NullPointerException++`` if passed a null argument, and a ``++ClassCastException++`` if the argument is of the wrong type. So there's no need to null-test or type-test the argument.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
public int compareTo(Object obj) {
|
|
|
|
if (obj == null) { // Noncompliant
|
|
return -1;
|
|
}
|
|
if (! obj instanceof MyClass.class) { // Noncompliant
|
|
return -1;
|
|
}
|
|
|
|
MyObject myObj = (MyObject) obj;
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
public int compareTo(Object obj) {
|
|
|
|
MyObject myObj = (MyObject) obj;
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|