From f64492960b9783ee6045b24a7c487d5095e90ecf Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 25 Jan 2006 00:40:20 +0000 Subject: [PATCH] Our old pal Occam did a bit of shaving around some of the bushier methods: they can be implemented in terms of some of the other methods. (For example, contains() now calls indexOf() instead of nearly duplicating it.) git-svn-id: https://samskivert.googlecode.com/svn/trunk@1772 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ListUtil.java | 70 ++++--------------- 1 file changed, 13 insertions(+), 57 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ListUtil.java b/projects/samskivert/src/java/com/samskivert/util/ListUtil.java index ab7b75b2..04d7b9b0 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ListUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/ListUtil.java @@ -106,7 +106,7 @@ public class ListUtil } // expand the list if necessary - if (index >= list.length) { + if (index >= llength) { list = accomodate(list, index); } @@ -226,7 +226,7 @@ public class ListUtil } // expand the list if necessary - if (index >= list.length) { + if (index >= llength) { list = accomodate(list, index); } @@ -262,22 +262,7 @@ public class ListUtil protected static boolean contains ( EqualityComparator eqc, Object[] list, Object element) { - if (element == null) { - throw NPE(); - } - if (list == null) { - return false; - } - - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - Object elem = list[i]; - if (eqc.equals(elem, element)) { - return true; - } - } - - return false; + return (-1 != indexOf(eqc, list, element)); } /** @@ -329,11 +314,9 @@ public class ListUtil throw NPE(); } if (list != null) { - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - Object elem = list[i]; - if (eqc.equals(elem, element)) { - return i; + for (int ii = 0, nn = list.length; ii < nn; ii++) { + if (eqc.equals(list[ii], element)) { + return ii; } } } @@ -367,23 +350,13 @@ public class ListUtil protected static Object clear ( EqualityComparator eqc, Object[] list, Object element) { - if (element == null) { - throw NPE(); - } - // nothing to clear from an empty list - if (list == null) { + int dex = indexOf(eqc, list, element); + if (dex == -1) { return null; } - - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - Object elem = list[i]; - if (eqc.equals(elem, element)) { - list[i] = null; - return elem; - } - } - return null; + Object elem = list[dex]; + list[dex] = null; + return elem; } /** @@ -418,24 +391,7 @@ public class ListUtil protected static Object remove ( EqualityComparator eqc, Object[] list, Object element) { - if (element == null) { - throw NPE(); - } - // nothing to remove from an empty list - if (list == null) { - return null; - } - - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - Object elem = list[i]; - if (eqc.equals(elem, element)) { - System.arraycopy(list, i+1, list, i, llength-(i+1)); - list[llength-1] = null; - return elem; - } - } - return null; + return remove(list, indexOf(eqc, list, element)); } /** @@ -451,7 +407,7 @@ public class ListUtil public static Object remove (Object[] list, int index) { int llength = list.length; - if (list == null || llength <= index) { + if (list == null || llength <= index || index < 0) { return null; }