Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting legacy code to use generics

0 views
Skip to first unread message

John Steel

unread,
Jun 29, 2007, 6:42:39 AM6/29/07
to
Hi, I'm trying to get rid of the warning ""GameListRenderer.java":
[unchecked] unchecked conversion; found : java.util.Collection, required:
java.util.Collection<? extends java.util.Collection> at line 95, column 83
in the following line:

ArrayList<Collection> gameList = new ArrayList <Collection>
(games.getValues());

games is an instance of this:

public class GameListManager {
TreeMap<String, GameRec> gameListTreeMap;

public GameListManager() {
gameListTreeMap = new TreeMap<String, GameRec>();
}

public Collection getValues() {
return gameListTreeMap.values();
}
...
}

As you can see getValues() returns a Collection, so I can't see what the
warning is trying to tell me.

Thanks very much

JB2006/jdk6.01

--
www.phonewebcam.com


Gillmer J. Derge [TeamB]

unread,
Jun 29, 2007, 8:39:08 AM6/29/07
to
John Steel wrote:
> public Collection getValues() {
> return gameListTreeMap.values();
> }
> ...
> }
>
> As you can see getValues() returns a Collection, so I can't see what the
> warning is trying to tell me.

gameListTreeMap is a TreeMap<String, GameRec>, so the values method
returns Collection<GameRec>. Since your getValues method returns a
plain Collection without any generic type parameters, you've lost the
restriction to GameRec, which is an unchecked conversion from
Collection<GameRec> to Collection. There are a few ways you can get rid
of the problem.

1. Change your getValues method's return type to Collection<GameRec>.
This is probably the most type safe solution, but depending on how big
your API is, it might just propagate the problem up the tree. Now
anyone that calls your getValues method expecting a Collection is going
to get a similar warning. In other words:

Collection foo = gameListManager.getValues(); // warning!

2. Add a SuppressWarnings annotation to the getValues method. This
basically tells the compiler, "Shut up, I know what I'm doing." It
works, and you probably do know what you're doing, but my preference is
to only do that when I have no other choice (ex. the warning comes from
interacting with some 3rd party API that I can't change).

@SuppressWarnings("unchecked")


public Collection getValues() {
return gameListTreeMap.values();
}

--
Gillmer J. Derge [TeamB]

John Steel

unread,
Jun 29, 2007, 7:36:54 PM6/29/07
to
"Gillmer J. Derge [TeamB]" <sp...@gillmerderge.com> wrote in message
news:4684...@newsgroups.borland.com...

>> As you can see getValues() returns a Collection, so I can't see what the
>> warning is trying to tell me.
>

> Collection<GameRec> to Collection. There are a few ways you can get rid
> of the problem.
>
> 1. Change your getValues method's return type to Collection<GameRec>. This
> is probably the most type safe solution, but depending on how big your API
> is, it might just propagate the problem up the tree. Now anyone that
> calls your getValues method expecting a Collection is going to get a
> similar warning. In other words:
>
> Collection foo = gameListManager.getValues(); // warning!

Thanks very much for your help, this is very kind of you.
I am in control of all the source, and the api is pretty much limited to
these 2 classes (at present!).
As expected, when I changed the GameListManagers getValues() to read

public Collection<GameRec> getValues() {
return gameListTreeMap.values();
}

I found it compiled fine but then the original calling line now gives these
2 errors:
"GameListRenderer.java": cannot find symbol; symbol : constructor
ArrayList(java.util.Collection<GameRec>), location: class
java.util.ArrayList<java.util.Collection> at line 94, column 40
"GameListRenderer.java": internal error; cannot instantiate
java.util.ArrayList.<init> at java.util.ArrayList<java.util.Collection> to
() at line 94, column 40

I'm trying to think this through by describing it in English - I think I'm
asking for an ArrayList of GameRecs, but we know games.getValues() now
returns the type safe Collection so its almost as if the syntax end up like
ArrayList<Collection<GameRec>> gameList =... , but that wouldn't compile at
all

(the original caller is ArrayList<Collection> gameList = new ArrayList
<Collection> (games.getValues()); ).

I too would like a clean build without "hacks". There are a few others of
this ilk relating to Comparators, so I'd like to fix those having understood
this one.

Gillmer J. Derge [TeamB]

unread,
Jun 29, 2007, 8:20:56 PM6/29/07
to
John Steel wrote:
> (the original caller is ArrayList<Collection> gameList = new ArrayList
> <Collection> (games.getValues()); ).

Your new ArrayList isn't going to be a list of Collection's. It's a
list of GameRec's, containing the same GameRec's that are in the
games.getValues() collection.

ArrayList<GameRec> gameList = new ArrayList<GameRec>(games.getValues());

John Steel

unread,
Jul 1, 2007, 12:46:42 PM7/1/07
to
"Gillmer J. Derge [TeamB]" <sp...@gillmerderge.com> wrote in message
news:4685...@newsgroups.borland.com...

That did it - thanks. I even fixed the Comparater one after your help too -
thanks again.

-- John
www.phonewebcam.com


0 new messages