🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #116

Re: Converting legacy code to use generics

#116
From: Gillmer J. Derge [TeamB]
Date: Fri, 29 Jun 2007 12:39
38 lines
2518 bytes
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]
Technical Details
Message-ID: <4684fd6d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4684e22c@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,49d96784a5821e34
X-Google-Attributes: gidf74b3,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 29 Jun 2007 08:39:08 -0400
From: "Gillmer J. Derge [TeamB]" <spam@gillmerderge.com>
User-Agent: Thunderbird 2.0.0.4 (Windows/20070604)
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: Converting legacy code to use generics
References: <4684e22c@newsgroups.borland.com>
In-Reply-To: <4684e22c@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.149.56.247
Message-ID: <4684fd6d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1183120749 24.149.56.247 (29 Jun 2007 05:39:09 -0700)
Lines: 38
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail