Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35471

Re: Why not String compare?

Newsgroups comp.lang.java.programmer
Date 2016-08-26 11:03 -0700
References <33463070-424f-4fb0-956b-2849f1a400eb@googlegroups.com> <nppu0n$nle$1@dont-email.me>
Message-ID <2dca46af-3b1f-4352-82c6-a413d9dac03c@googlegroups.com> (permalink)
Subject Re: Why not String compare?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Friday, August 26, 2016 at 1:24:48 PM UTC-4, Eric Sosman wrote:
>      Are you talking about Comparator, or about Comparable?  The
> latter has a compareTo() method, the former does not.
> 
>      If you're speaking of Comparable, I think you're in violation
> of the spec.  In
> 
> 	ComparableThing x = ...;  // non-null
> 	ComparableThing y = null;
> 	int xy = x.compareTo(y);
> 	int yx = y.compareTo(x);
> 
> ... you don't get xy and yx with opposite signs, as required by
> the Comparable documentation:
> 
> 	The implementor must ensure sgn(x.compareTo(y)) ==
> 	-sgn(y.compareTo(x)) for all x and y. (This implies that
> 	x.compareTo(y) must throw an exception iff y.compareTo(x)
> 	throws an exception.)
> 
>      If you're speaking of Comparator, simply returning 1 when either
> argument of compare() is null is also a breach.  Consider
> 
> 	Thing x = ...;  // non-null
> 	Thing y = null;
> 	Comparator<Thing> c = ...;
> 	int xy = c.compare(x, y);
> 	int yx = c.compare(y, x);
> 
> Again, you must ensure that sgn(xy) = -sgn(yx), but if both are 1
> this doesn't hold.  (You should also arrange that c.compare(null,null)
> returns 0, not 1 or -1.)


It seems silly to suggest you can't do x.compareTo(y) unless y.compareTo(x) is also legal, but you can do x.equals(y) where y.equals(x) is also not valid if y is null.

Another example where I needed null to be valid in my custom Comparable.  I wrote a custom table (extends JTable).  Every cell in the table is a custom object.  The cell class has a getValue() and a compareTo().  getValue() may return null because null has special meaning.  If the cell is editable by default and has a value of empty string it displays as editable for the user to click on it and type into it.  If it has a value of null it displays as blank and the user cannot edit.  This saves us the trouble of populating the cells with empty strings and separately telling it half those cells are not editable.  If it's a check-box style column it can populate with true (checked box), false (unchecked box), or null (blank cell).  If the column is sortable the compareTo method sorts nulls to the beginning.  null should always be a valid value on that side.  You can't compare something to nothing because you have no basis for comparison if you have nothing, but you can always compare nothing to a something.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 09:00 -0700
  Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 18:49 +0200
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:09 -0700
      Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 22:48 +0200
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 13:55 -0700
          Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 17:29 -0400
          Re: Why not String compare? Lew <lewbloch@gmail.com> - 2016-08-30 22:20 -0700
          Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 08:17 +0200
            Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-08-31 09:45 +0000
              Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 19:24 +0200
                Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-09-01 10:17 +0000
                Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-09-02 18:29 +0200
  Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 13:24 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:48 -0700
      Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 14:50 -0400
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 12:19 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-01 20:42 +0200
        Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-01 17:57 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 11:03 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-02 21:54 +0200

csiph-web