Re: Queue implementation: generics
On Fri, 06 Jul 2007 07:25:31 -0700, Karsten Wutzke wrote:
public class FixedSizeShiftQueue<E> extends AbstractQueue {
I smell what looks like a rare type here... Shouldn't AbstractQueue be
generified as well?
//private final E[] elems;
Arrays and generics don't mix. See below.
//this.elems = new E[size]; //ERROR: Cannot create
generic array of E
Ditto.
//elems[elems.length - 1] = (E)obj; //WARNING: Type
safety: The cast from Object to E is actually checking against the
erased type Object
The warning adequately describes what's going on. See below again.
return (E)elems[0]; //Type safety: The cast from Object
to E is actually checking against the erased type Object
Ditto.
return (E)elems[0]; //Type safety: The cast from Object
to E is actually checking against the erased type Object
Ditto.
ArrayList al = new ArrayList();
ArrayList should be generified: ArrayList<E> al = new ArrayList<E>();
for ( Object elem : elems )
Change Object to E ...
al.add(elem); //WARNING: Type safety: The method
add(Object) belongs to the raw type ArrayList. References to generic
type ArrayList<E> should be parameterized
... and that error goes poof.
I looked at the JDK sources of AbstractQueue, AbstractCollection and
mainly Vector, but I couldn't get it done.
Look at ArrayList. That is essentially what you are creating. Furthermore
notice the hefty amounts of @SuppressWarnings on said class. Because of
various constraints, all of the neat generic stuff is demolished in the
source code, rendering such constructs as 'E.class' and 'E[]' and '(E)'
useless, so a healthy mixture of reflection and suppressing the
compiler's type saftey makes code compilable. Before you complain, that's
what all the code in the JDK does.
Let's all hope that reified generics gets put into Java 7....