From 56e58fa431f0ff63032ee0b177f947d0d3123252 Mon Sep 17 00:00:00 2001 From: ray Date: Sat, 2 Sep 2006 01:08:38 +0000 Subject: [PATCH] Genericized methods that benefit from it. I originally had append have the signature: public static T[] append ( T[] values, E value) At first I thought I had done a good thing by ensuring that a subclass element could be added, but then realized that E was pointless because an E can be passed in for the T parameter anyway. That got me thinking about generics and I found this: http://weblogs.java.net/blog/arnold/archive/2005/06/generics_consid_1.html Some of it made me laugh, and while I didn't read all the comments, I spotted this gem: [excerpt] The trouble with generics--and this has been known for many years-- is what I'd like to call the Elvis/Einstein boundary. (Google for Mort/Elvis/Einstein if you wonder why the King enters the picture...) List. solves a problem that Elvis had: What the !@#$ is in that collection? But before he knows it, he is at int binarySearch(List> list, T key). That`s for Einstein, not Elvis. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1895 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/ArrayUtil.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/util/ArrayUtil.java b/src/java/com/samskivert/util/ArrayUtil.java index c525d9b2..224b20e6 100644 --- a/src/java/com/samskivert/util/ArrayUtil.java +++ b/src/java/com/samskivert/util/ArrayUtil.java @@ -613,7 +613,7 @@ public class ArrayUtil * which all subsequent values are to be removed. This must be a * valid index within the values array. */ - public static Object[] splice (Object[] values, int offset) + public static T[] splice (T[] values, int offset) { int length = (values == null) ? 0 : values.length - offset; return splice(values, offset, length); @@ -634,7 +634,8 @@ public class ArrayUtil * offset + length must be a valid index within the * values array. */ - public static Object[] splice (Object[] values, int offset, int length) + public static T[] splice ( + T[] values, int offset, int length) { // make sure we've something to work with if (values == null) { @@ -655,7 +656,8 @@ public class ArrayUtil } // create a new array and populate it with the spliced-in values - Object[] nvalues = (Object[])Array.newInstance( + @SuppressWarnings("unchecked") + T[] nvalues = (T[])Array.newInstance( values.getClass().getComponentType(), size - length); System.arraycopy(values, 0, nvalues, 0, offset); System.arraycopy(values, tstart, nvalues, offset, size - tstart); @@ -772,9 +774,10 @@ public class ArrayUtil * specified value inserted into the last slot. The type of the values * array will be preserved. */ - public static Object[] append (Object[] values, Object value) + public static T[] append (T[] values, T value) { - Object[] nvalues = (Object[])Array.newInstance( + @SuppressWarnings("unchecked") + T[] nvalues = (T[])Array.newInstance( values.getClass().getComponentType(), values.length+1); System.arraycopy(values, 0, nvalues, 0, values.length); nvalues[values.length] = value;