1

I'm searching for a method that can compare two non-primitive Long objects in Java. Something like this : Longs.compare(Long x, Long y). As I didn't find anything on the web yet, I am wondering if a such library implement that.

So far, I compared them with Long.compare(x, y) but now I've realized that x and y can be null, so sometimes I can get a NullPointerException.

Do you know any library that can do that?

PS : My need is not the same as this post. I want to know if x > y not if x == y.

Community
  • 1
  • 1
Clemzd
  • 895
  • 2
  • 15
  • 35
  • 2
    Possible duplicate of [Compare non-primitive Long values 127 and 128](http://stackoverflow.com/questions/20541636/compare-non-primitive-long-values-127-and-128) – burglarhobbit Oct 12 '15 at 15:11
  • 1
    @Mr.Robot Despite the similar summary line, I don't think this is a dupe of that question. That question is looking for equality comparisons only, and is basically about the confusion between `==` and `equals`. This question is asking for a `compareTo`-style comparison that includes nullity. – yshavit Oct 12 '15 at 15:14
  • 1
    Keep using `compare`, just check for `null` before you do. – Sotirios Delimanolis Oct 12 '15 at 15:15
  • 1
    Test for `null`, and then use `Long.compare(x,y)`. – Elliott Frisch Oct 12 '15 at 15:15
  • Yes, that's what I'm doing for now. But I think it's too bad there isn't library that implement this... – Clemzd Oct 12 '15 at 15:19
  • Asking for libraries is off-topic. – Sotirios Delimanolis Oct 12 '15 at 15:19
  • You don't need an entire library for something so simple. – Sotirios Delimanolis Oct 12 '15 at 15:20
  • While it's true that you don't need a whole library just for this, if you already depend on a library that gives you this behavior (say, Guava or Apache Commons), you may as well use it. (In fact, pretty much _all_ of Guava is stuff for which you could argue, "you don't need a whole library just for this." But you add all of them together, and that library starts being worthwhile.) – yshavit Oct 12 '15 at 15:25
  • 1
    There are already Guava and Apache Commons in the project. – Clemzd Oct 12 '15 at 15:39

4 Answers4

3

I believe what you're looking for is the ObjectUtils class from apache commons. It's specifically designed to handle general null-safe comparison.

Its compare() method will never throw an exception based on null input, and instead just assumes that

null is ... less than a non-null value.

So for instance:

    System.out.println(ObjectUtils.compare(null, null));                   // 0
    System.out.println(ObjectUtils.compare(new Long(1), null));            // 1
    System.out.println(ObjectUtils.compare(null, new Long(1)));            // -1
    System.out.println(ObjectUtils.compare(new Long(1), new Long(2)));     // -1
azurefrog
  • 10,785
  • 7
  • 42
  • 56
2

With Java SE 8 try:

Comparator<Long> nullsFirstLongComparator = Comparator.nullsFirst(Comparator.naturalOrder());

or

Comparator<Long> nullsLastLongComparator = Comparator.nullsLast(Comparator.naturalOrder());
Puce
  • 37,247
  • 13
  • 80
  • 152
0

Documentation is your best guide.

http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html

Please check the public int compareTo(Long anotherLong) method.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
  • 1
    This will still throw NPEs, since all that `compareTo()` does is try compare `this.value` with `anotherLong.value`. – azurefrog Oct 12 '15 at 15:16
  • 1
    neither `null.compareTo(1L)` nor `1L.compareTo(null)` works (it will throw a null pointer exception) – Clemzd Oct 12 '15 at 15:17
  • AFAIK there is no built in API which does this. You could perhaps write your own version and check if either of the inputs is null?. – Pavan Dittakavi Oct 12 '15 at 15:19
0

The longhand answer is to check for nulls first, but you can also use Guava's Ordering<T>, which is an extension of Comparator<T> that also supports a fluent builder style:

Comparator<Long> nullsFirst = Ordering.natural().nullsFirst();
// there's also a nullsLast()
yshavit
  • 42,327
  • 7
  • 87
  • 124