From dev-return-127279-apmail-commons-dev-archive=commons.apache.org@commons.apache.org Thu Jul 7 17:22:35 2011 Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1210C4EF8 for ; Thu, 7 Jul 2011 17:22:35 +0000 (UTC) Received: (qmail 83050 invoked by uid 500); 7 Jul 2011 17:22:34 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 82817 invoked by uid 500); 7 Jul 2011 17:22:33 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 82809 invoked by uid 99); 7 Jul 2011 17:22:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jul 2011 17:22:33 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [209.85.214.171] (HELO mail-iw0-f171.google.com) (209.85.214.171) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jul 2011 17:22:28 +0000 Received: by iwn34 with SMTP id 34so1277434iwn.30 for ; Thu, 07 Jul 2011 10:22:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.42.144.70 with SMTP id a6mr1114464icv.486.1310059326409; Thu, 07 Jul 2011 10:22:06 -0700 (PDT) Received: by 10.231.68.82 with HTTP; Thu, 7 Jul 2011 10:22:06 -0700 (PDT) In-Reply-To: References: Date: Thu, 7 Jul 2011 10:22:06 -0700 Message-ID: Subject: Re: [lang] NullArgumentException usage From: James Ring To: Commons Developers List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hey, On Thu, Jul 7, 2011 at 9:56 AM, Gary Gregory wrote= : > Hi All: > > I do like using NullArgumentException, but I find writing this over and o= ver > tedious: > > if (arg =3D=3D null) { > =A0thrown new NullArgumentException(argName); > } > something(arg); > > How about this instead: > > NullArgumentException.check(arg, argName); > something(arg); > > or: > > something(NullArgumentException.check(arg, argName)); > > Depending on the style you like. > > Where check is: > > =A0 =A0public static T check(T arg, String argName) { > =A0 =A0 =A0 =A0if (arg =3D=3D null) { > =A0 =A0 =A0 =A0 =A0 =A0throw new NullArgumentException(argName); > =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0return arg; > =A0 =A0} > > Yes, you are pushing the argName on the stack (or passing it in a registe= r) > and that is extra work, but you do not have to use the new method then ;) Google Guava does it with com.google.common.base.Preconditions: something(Preconditions.checkNotNull(arg)); > ? > > -- > Thank you, > Gary > > http://garygregory.wordpress.com/ > http://garygregory.com/ > http://people.apache.org/~ggregory/ > http://twitter.com/GaryGregory > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org