You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Niall Pemberton (JIRA)" <ji...@apache.org> on 2011/01/07 00:48:45 UTC
[jira] Created: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Add a Null-safe compare() method to ObjectUtils
-----------------------------------------------
Key: LANG-667
URL: https://issues.apache.org/jira/browse/LANG-667
Project: Commons Lang
Issue Type: New Feature
Components: lang.*
Affects Versions: 2.5
Reporter: Niall Pemberton
Priority: Minor
Fix For: 3.0
Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Niall Pemberton (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978707#action_12978707 ]
Niall Pemberton commented on LANG-667:
--------------------------------------
@Julien - thanks for the suggestion, I'll look at this when I get home
@Paul - Lang is all about adding useful utilities that Java itself doesn't have and theres alot of *prior art* here of null-safe utilities. Julien's example is a great demonstration of how this function can be used to simplify code. This feature is not a Comparable implementation and so breaks no contract. I really don't understand your objection and IMO its a very weak argument to object on the basis of Comparable's JavaDoc. How would you handle implementing a compareTo() method where the properties being compared may be null - surely you're not arguing that Julien's example should fail and throw a NullPointerException if one of the properties is null?
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Julien Aymé (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978693#action_12978693 ]
Julien Aymé commented on LANG-667:
----------------------------------
Even is this method does break the contract of Comparable, it is really usefull when comparing many fields of two comparable beans (which may be null):
{code}
public int compareTo(Bean other) {
int c = ObjectUtils.compare(this.getField1(), other.getField1());
if (c != 0) {
return c;
}
c = ObjectUtils.compare(this.getField2(), other.getField2());
if (c != 0) {
return c;
}
return ObjectUtils.compare(this.getField3(), other.getField3());
}
{code}
I don't want to be forced to handle null on field1, 2, or 3 for the 2 beans (and that's why in my projects I already have this method added to ObjectUtils).
I propose that we add a javadoc comment stating that the method breaks the Comparable contract.
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Niall Pemberton (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978972#action_12978972 ]
Niall Pemberton commented on LANG-667:
--------------------------------------
Simplified the code as per Julien's suggestion:
http://svn.apache.org/viewvc?view=revision&revision=1056520
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Paul Benedict (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978662#action_12978662 ]
Paul Benedict commented on LANG-667:
------------------------------------
This feature sounds like a bad idea because it goes against the contract of Comparable. From the javadocs:
"e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false."
There is explicitly no such thing as a null-safe compare() in Java.
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Paul Benedict (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12979105#action_12979105 ]
Paul Benedict commented on LANG-667:
------------------------------------
"This feature is not a Comparable implementation and so breaks no contract." Agreed. It would be nice to note that in the method's javadoc. Otherwise, I withdraw my objection.
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Stephen Colebourne (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978733#action_12978733 ]
Stephen Colebourne commented on LANG-667:
-----------------------------------------
I support this change, its a useful feature that is clearly documented and no threat to the Comparable contract.
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Julien Aymé (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12978701#action_12978701 ]
Julien Aymé commented on LANG-667:
----------------------------------
Regarding the code, I think it would be faster and simpler to understand if we add a "==" test first:
{code}
public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater) {
if (c1 == c2) {
return 0;
} else if (c1 == null) {
return nullGreater ? 1 : -1;
} else if (c2 == null) {
return nullGreater ? -1 : 1;
}
return c1.compareTo(c2);
}
{code}
Note: the commited code in rev 1056124 was:
{code}
public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater) {
int result = 0;
if ((c1 == null) || (c2 == null)) {
if (nullGreater) {
result = (c1 == null ? 1 : 0) - (c2 == null ? 1 : 0);
} else {
result = (c1 == null ? -1 : 0) - (c2 == null ? -1 : 0);
}
} else {
result = c1.compareTo(c2);
}
return result;
}
{code}
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Resolved: (LANG-667) Add a Null-safe compare() method to
ObjectUtils
Posted by "Niall Pemberton (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/LANG-667?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Niall Pemberton resolved LANG-667.
----------------------------------
Resolution: Fixed
Assignee: Niall Pemberton
Added: http://svn.apache.org/viewvc?view=revision&revision=1056124
> Add a Null-safe compare() method to ObjectUtils
> -----------------------------------------------
>
> Key: LANG-667
> URL: https://issues.apache.org/jira/browse/LANG-667
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Affects Versions: 2.5
> Reporter: Niall Pemberton
> Assignee: Niall Pemberton
> Priority: Minor
> Fix For: 3.0
>
>
> Add a Null-safe compare() method to ObjectUtils
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.