From ebdbd964b51168c4b6952c0eb62978e3ec7d1a44 Mon Sep 17 00:00:00 2001 From: samskivert Date: Wed, 1 Dec 2010 00:43:12 +0000 Subject: [PATCH] Do our null checking less wonkily. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2959 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ListUtil.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/samskivert/util/ListUtil.java b/src/main/java/com/samskivert/util/ListUtil.java index 36b6b9e2..69d283e0 100644 --- a/src/main/java/com/samskivert/util/ListUtil.java +++ b/src/main/java/com/samskivert/util/ListUtil.java @@ -103,9 +103,8 @@ public class ListUtil */ public static Object[] add (Object[] list, int startIdx, Object element) { - if (element == null) { - throw NPE(); - } + requireNotNull(element); + // make sure we've got a list to work with if (list == null) { list = new Object[DEFAULT_LIST_SIZE]; @@ -147,9 +146,8 @@ public class ListUtil */ public static Object[] insert (Object[] list, int index, Object element) { - if (element == null) { - throw NPE(); - } + requireNotNull(element); + // make sure we've got a list to work with if (list == null) { list = new Object[DEFAULT_LIST_SIZE]; @@ -215,9 +213,8 @@ public class ListUtil protected static Object[] testAndAdd ( EqualityComparator eqc, Object[] list, Object element) { - if (element == null) { - throw NPE(); - } + requireNotNull(element == null); + // make sure we've got a list to work with if (list == null) { list = new Object[DEFAULT_LIST_SIZE]; @@ -327,9 +324,7 @@ public class ListUtil protected static int indexOf ( EqualityComparator eqc, Object[] list, Object element) { - if (element == null) { - throw NPE(); - } + requireNotNull(element == null); if (list != null) { for (int ii = 0, nn = list.length; ii < nn; ii++) { if (eqc.equals(list[ii], element)) { @@ -489,12 +484,13 @@ public class ListUtil } /** - * Throw a NullPointerException with the bad news. + * Throws a NullPointerException if the element is null. */ - protected static NullPointerException NPE () + protected static void requireNotNull (Object element) { - return new NullPointerException( - "ListUtil does not support null elements."); + if (element == null) { + throw new NullPointerException("ListUtil does not support null elements."); + } } /**