Genericized methods that benefit from it.
I originally had append have the signature:
public static <T extends Object, E extends T> 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<E>. solves a problem that Elvis had: What the !@#$ is in that
collection?
But before he knows it, he is at
<T> int binarySearch(List<? extends Comparable<? super T>> list, T key).
That`s for Einstein, not Elvis.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1895 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -613,7 +613,7 @@ public class ArrayUtil
|
||||
* which all subsequent values are to be removed. This must be a
|
||||
* valid index within the <code>values</code> array.
|
||||
*/
|
||||
public static Object[] splice (Object[] values, int offset)
|
||||
public static <T extends Object> 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
|
||||
* <code>offset + length</code> must be a valid index within the
|
||||
* <code>values</code> array.
|
||||
*/
|
||||
public static Object[] splice (Object[] values, int offset, int length)
|
||||
public static <T extends Object> 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 extends Object> 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;
|
||||
|
||||
Reference in New Issue
Block a user