Fix #107 when we grow the original array, use the same Class that was specified during construction.

pull/113/head
Brett Wooldridge 11 years ago
parent 5b5841e616
commit 19eaea93aa

@ -25,6 +25,7 @@ import java.lang.reflect.Array;
*/
public final class FastList<T>
{
private final Class<?> clazz;
private T[] elementData;
private int size;
@ -36,6 +37,7 @@ public final class FastList<T>
public FastList(Class<?> clazz)
{
this.elementData = (T[]) Array.newInstance(clazz, 32);
this.clazz = clazz;
}
/**
@ -47,6 +49,7 @@ public final class FastList<T>
public FastList(Class<?> clazz, int capacity)
{
this.elementData = (T[]) Array.newInstance(clazz, capacity);
this.clazz = clazz;
}
/**
@ -64,7 +67,7 @@ public final class FastList<T>
final int oldCapacity = elementData.length;
final int newCapacity = oldCapacity << 1;
@SuppressWarnings("unchecked")
final T[] newElementData = (T[]) Array.newInstance(element.getClass(), newCapacity);
final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);
System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
newElementData[size - 1] = element;
elementData = newElementData;

@ -74,4 +74,31 @@ public class TestFastList
Assert.assertSame(verifyList.get(i), list.get(i));
}
}
@Test
public void testPolyMorphism1()
{
class Foo implements Base2 {
}
class Bar extends Foo {
}
FastList<Base> list = new FastList<>(Base.class, 2);
list.add(new Foo());
list.add(new Foo());
list.add(new Bar());
}
interface Base
{
}
interface Base2 extends Base
{
}
}

@ -25,6 +25,7 @@ import java.lang.reflect.Array;
*/
public final class FastList<T>
{
private final Class<?> clazz;
private T[] elementData;
private int size;
@ -36,6 +37,7 @@ public final class FastList<T>
public FastList(Class<?> clazz)
{
this.elementData = (T[]) Array.newInstance(clazz, 32);
this.clazz = clazz;
}
/**
@ -47,6 +49,7 @@ public final class FastList<T>
public FastList(Class<?> clazz, int capacity)
{
this.elementData = (T[]) Array.newInstance(clazz, capacity);
this.clazz = clazz;
}
/**
@ -64,7 +67,7 @@ public final class FastList<T>
final int oldCapacity = elementData.length;
final int newCapacity = oldCapacity << 1;
@SuppressWarnings("unchecked")
final T[] newElementData = (T[]) Array.newInstance(element.getClass(), newCapacity);
final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);
System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
newElementData[size - 1] = element;
elementData = newElementData;

@ -74,4 +74,31 @@ public class TestFastList
Assert.assertSame(verifyList.get(i), list.get(i));
}
}
@Test
public void testPolyMorphism1()
{
class Foo implements Base2 {
}
class Bar extends Foo {
}
FastList<Base> list = new FastList<>(Base.class, 2);
list.add(new Foo());
list.add(new Foo());
list.add(new Bar());
}
interface Base
{
}
interface Base2 extends Base
{
}
}

Loading…
Cancel
Save