user126593
user126593

Reputation: 2817

Using generics with jakarta commons collections Buffer

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.

Upvotes: 0

Views: 1776

Answers (2)

KingInk
KingInk

Reputation: 536

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

Upvotes: 0

Thilo
Thilo

Reputation: 262494

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.

Upvotes: 2

Related Questions