Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3005
| From | Tom Anderson <twic@urchin.earth.li> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Novice to Generics Trying to Implement a Generic Priority Queue |
| Date | 2011-04-09 10:36 +0100 |
| Organization | Stack Usenet News Service |
| Message-ID | <alpine.DEB.2.00.1104091014130.20175@urchin.earth.li> (permalink) |
| References | <95a0645f-5c83-4028-8d82-259f83e45159@k9g2000yqi.googlegroups.com> <inll2n$lsc$1@dont-email.me> <c5f3fb99-c4d9-4eec-8ed4-cadf73b83bbe@x18g2000yqe.googlegroups.com> <ino3nm$3s9$1@dont-email.me> <ino9ra$52d$1@news.albasani.net> |
On Fri, 8 Apr 2011, Lew wrote:
> You do, however, have to guard against a possible
> 'NullPointerException'. The method is 'private', so it's up to its
> callers not to screw that up. You enforce that with an assertion:
>
> private static boolean inOrder( Da left, Da right)
> {
> assert left != null;
> return left.compareTo( right ) <= 0;
> }
compareTo will (or at least should) throw a NullPointerException if right
is null (from Comparable's javadoc - "Note that null is not an instance of
any class, and e.compareTo(null) should throw a NullPointerException even
though e.equals(null) returns false"), so if you're going to check left,
you ought to check right too.
But i don't see why you would check either. It is right and proper that if
a method which requires non-null arguments is passed nulls, it should
throw a NullPointerException (from NullPointerException's javadoc:
"Applications should throw instances of this class to indicate other
illegal uses of the null object"). This method will do that. The assertion
is unnecessary.
A method which is going to handle a parameter in such a way that it will
not naturally blow up if it is null, but which requires that it be
non-null, should of course make the assertion you describe, or an
equivalent explicit guard.
> If the method were 'public' you'd need to add an explicit guard against
> the 'NullPointerException':
>
> public static boolean inOrder( Da left, Da right)
> {
> if ( left == null )
> {
> return true;
> }
> assert left != null;
> return left.compareTo( right ) <= 0;
> }
This means something different to the assertion. This says that nulls are
smaller than any other element - and so implies that nulls are a
permissible element, whereas the assertion rejected them. That would be a
logically consistent thing to do, but in that case, the method also needs
to deal with a null right, by inserting "if (right == null) return false;"
after the first guard and before the compareTo call.
The other option would be to check both left and right for nullity, and
throw a NullPointerException (or IllegalArgumentException if you prefer)
if either is null. That would require all elements to be non-null. You can
do this by omitting any guard clauses - the compareTo call will do it.
> Don't forget your Javadocs!
Sage advice.
tom
--
secular utopianism is based on a belief in an unstoppable human ability
to make a better world -- Rt Rev Tom Wright
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-07 16:03 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-07 17:32 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-08 14:55 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-08 15:54 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue Lew <noone@lewscanon.com> - 2011-04-08 20:39 -0400
Re: Novice to Generics Trying to Implement a Generic Priority Queue Tom Anderson <twic@urchin.earth.li> - 2011-04-09 10:36 +0100
Re: Novice to Generics Trying to Implement a Generic Priority Queue Roedy Green <see_website@mindprod.com.invalid> - 2011-04-09 05:50 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-11 08:08 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-11 10:29 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-11 12:10 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-11 21:42 +0200
Re: Novice to Generics Trying to Implement a Generic Priority Queue Tom Anderson <twic@urchin.earth.li> - 2011-04-11 22:41 +0100
Re: Novice to Generics Trying to Implement a Generic Priority Queue Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-12 01:07 +0200
csiph-web