views:

488

answers:

2

This code compiles fine in Java <= 1.4. Java 1.6 bitches and moans with the warning:

"The method add(Object) belongs to the raw type Collection. References to generic type Collection should be parameterized"

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUtils;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;

private Buffer connectqueue = BufferUtils.blockingBuffer(new UnboundedFifoBuffer());

...

connectqueue.add(new Conn(this, address, port));

How do I tweak the code to make that warning go away without adding a @SupressWarnings directive ?

The problem is Jakarta Commons Collections Buffer is non-generic, but extends the generic java.util.Collection interface.

+2  A: 

You cannot. Until Jakarta Commons supports generics (which they will probaby not, because they want to be able to build on older Java versions as well), you need to suppress (or live with) the warning.

As an alternative, there is a fork of Commons Collections that supports generics, and Google also has a Collections library. I have not checked if either of them has a Buffer, though, and it would require you to switch APIs.

If none of your code uses post-1.4-language features, you could set the compiler's language level to "1.4", but that seems even less feasible (or desirable).

Probably just stick with @SupressWarnings.

Thilo
A: 

As mentioned above you could use the fork of Jakarta collections which will give you a buffer class that uses generics and won't give you warnings http://collections.sourceforge.net/api/org/apache/commons/collections/Buffer.html

KingInk