Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!2a01:348:65::2.MISMATCH!news-transit.tcx.org.uk!news.stack.nl!.POSTED!ipv6.urchin.earth.li!twic From: Tom Anderson Newsgroups: comp.lang.java.programmer Subject: Re: Novice to Generics Trying to Implement a Generic Priority Queue Date: Sat, 9 Apr 2011 10:36:53 +0100 Organization: Stack Usenet News Service Lines: 64 Message-ID: References: <95a0645f-5c83-4028-8d82-259f83e45159@k9g2000yqi.googlegroups.com> NNTP-Posting-Host: ipv6.urchin.earth.li Mime-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII X-Trace: mud.stack.nl 1302341813 75388 2001:ba8:0:1b4::6 (9 Apr 2011 09:36:53 GMT) X-Complaints-To: abuse@stack.nl NNTP-Posting-Date: Sat, 9 Apr 2011 09:36:53 +0000 (UTC) User-Agent: Alpine 2.00 (DEB 1167 2008-08-23) In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3005 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