Guava: ComparisonChain doesn't support nulls

Created on 31 Oct 2014  路  5Comments  路  Source: google/guava

_Original issue created by philip.zeyliger on 2011-02-21 at 11:04 PM_


Currently ComparisonChain.start().compare(null, null) will throw a NPE. This is reasonable, of course, but sometimes it's useful to compare objects that have some of their fields unset, with null appearing at the beginning or the end.

How do people feel about supporting a compareNullableNullFirst() that does something reasonable along these lines? (Compare Objects.equals(), which is useful for handling equality with the possibility of null.)

status=working-as-intended

Most helpful comment

_Original comment posted by cgdecker on 2011-02-22 at 02:30 AM_


You can already compare Comparable values that may be null like this:

compare(a, b, Ordering.natural().nullsFirst())

This approach is flexible and the example from the class documentation uses it. I'm guessing this won't be changed.


Status: WorkingAsIntended

All 5 comments

_Original comment posted by philip.zeyliger on 2011-02-21 at 11:06 PM_


Note that https://github.com/google/guava/issues/500 is similar, but it's about documentation. I propose adding a method here; not amending the documentation.

_Original comment posted by cgdecker on 2011-02-22 at 02:30 AM_


You can already compare Comparable values that may be null like this:

compare(a, b, Ordering.natural().nullsFirst())

This approach is flexible and the example from the class documentation uses it. I'm guessing this won't be changed.


Status: WorkingAsIntended

_Original comment posted by kevinb9n on 2011-02-22 at 05:14 AM_


Yup. We'd have no idea how to compare (null, nonnull).

_Original comment posted by philip.zeyliger on 2011-02-22 at 06:16 AM_


Thanks! Ordering.natural().nullsFirst() will do the trick.

_Original comment posted by jokoswt on 2012-11-15 at 02:06 AM_


thank you philips.

Was this page helpful?
0 / 5 - 0 ratings