Re: Class<T> question

kevin bourrillion <[email protected]> Wed, 5 Dec 2007 09:56:07 -0800
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
This trick is hugely popular in Google code and as a result all these silly
methods are included along with other non-silly features in the
publicly-availble google collections library.  There's Lists.newArrayList(),
Sets.newLinkedHashSet(), Maps.immutableMap(), etc.

If you don't want to use the library you should still (copy these
methods/write em yourself) and watch your code get cleaner.

Hopefully Java 7 will render it obsolete and we can just use new
HashMap<>().


On Dec 5, 2007 8:36 AM, Patrick Schneider <[email protected]> wrote:

> In order to cut down on the type specifiers, I was shown this trick
> earlier
> in the week...
>
> Map<String, Object> map = createMap();
>
> ...
>
> public static <K, V> Map<K, V> createMap() {
>    return new HashMap<K, V>();
> }
>
> ...which is essentially the same idea as what's going on in the
> implementation of Collections.emptyMap().  Since the method is generic,
> you
> only need to specify the types of the Map on the left hand side, saving
> you
> from typing two type specifiers.
>
>
> Patrick
>
> On Dec 5, 2007 9:48 AM, Attila Szegedi <[email protected]> wrote:
>
> > In my recent wrestling with "genericizing" a codebase that was
> > originally pre-Java 5, I'd suggest you try replacing "Class" with
> > "Class<?>".
> >
> > Alternatively, disable compiler warning for generics. No, seriously.
> > Honestly, my feeling is that programming with generics causes more
> > noise in source code than it eliminates. Yeah, it eliminates casts,
> > but previously I would write:
> >
> > Map map = new HashMap();
> > ...
> > String x = (String)map.get(blah);
> >
> > Now I write monstrosities such as:
> >
> > Map<? extends Object, String> map = new HashMap<Object, String>();
> > ...
> > String x = map.get(blah);
> >
> > So, I need to write one cast less, and four type specifiers more. Net
> > loss in code conciseness.
> > I guess I'm just getting old and resistant to change.
> >
> > Attila.
> >
> > On 2007.12.05., at 14:12, Gart, Mitchell wrote:
> >
> > > I am trying to get rid of some compiler warnings in our code.  We
> > > have a
> > > method:
> > >
> > >   public static Logger getLogger(Class c) {
> > >      return new Log4jLogger(c.getName());
> > >   }
> > >
> > > and the compiler produces this warning:
> > >
> > >   Class is a raw type. References to generic type Class<T> should be
> > > parameterized
> > >
> > > It is because Class is a generic type but I don't quite understand
> > > how I
> > > would change the declaration of the method, and the calls to the
> > > method,
> > > to make it parameterized and get rid of the warning.  Can somebody
> > > explain this a little bit?  Thanks.
> > >
> > > - Mitch Gart
> >
> > ===================================
> > This list is hosted by DevelopMentor(R)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>



--
Kevin Bourrillion @ Google
go/javalibraries
google-collections.googlecode.com
google-guice.googlecode.com
jsr-310.dev.java.net

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com