Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Generics FAQ

Reply
Thread Tools

Java Generics FAQ

 
 
Angelika Langer
Guest
Posts: n/a
 
      10-05-2004
I've been putting together a page with FAQs regarding Java Generics at
http://www.langer.camelot.de/Generic...nericsFAQ.html.

It was intially intended as companion material to my seminars, but it
may well be of interest to a broader audience. It should at least
answer many of the beginner's questions. If you're interested, feel
free to use it. Comments are welcome.

Angelika Langer
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-05-2004
On 5 Oct 2004 11:27:15 -0700, Angelika Langer wrote:

> I've been putting together a page with FAQs regarding Java Generics at
> http://www.langer.camelot.de/Generic...nericsFAQ.html.


Nice work. I've linked from my more general FAQ.
<http://www.physci.org/codes/javafaq.jsp#faq>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
 
Kris Nuttycombe
Guest
Posts: n/a
 
      10-05-2004
Thanks for the excellent FAQ! I have another question for it (although
whether it is frequently asked remains to be seen!

I have the following interface:

public interface DataConverter<I,O> {
public O convert(I obj);
}

It appears that it should be legal to define the following method:

/**
* Returns a Map<Class,DataConverter> that maps Java classes
* to DataConverter objects that know how to operate on
* data of the type specified by the map's keys.
*/
public <C extends Class> Map<C,DataConverter<? extends C,?>>
getDataConverters();

I cannot, however, figure out exactly how to instantiate a Map that I
could actually return from an implementation of this method!

Any help would be greatly appreciated.

Kris


Angelika Langer wrote:
> I've been putting together a page with FAQs regarding Java Generics at
> http://www.langer.camelot.de/Generic...nericsFAQ.html.
>
> It was intially intended as companion material to my seminars, but it
> may well be of interest to a broader audience. It should at least
> answer many of the beginner's questions. If you're interested, feel
> free to use it. Comments are welcome.
>
> Angelika Langer


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      10-05-2004
On Tue, 05 Oct 2004 15:59:05 -0600, Kris Nuttycombe wrote:

> I have another question for it ..


(chuckles) Usually people who post FAQ's demand that
you also provide the answer to your 'Q'..

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      10-06-2004
Kris Nuttycombe < > wrote in message news:<cjv5fa$8pg$>...
> Thanks for the excellent FAQ! I have another question for it (although
> whether it is frequently asked remains to be seen!
>
> I have the following interface:
>
> public interface DataConverter<I,O> {
> public O convert(I obj);
> }
>
> It appears that it should be legal to define the following method:
>
> /**
> * Returns a Map<Class,DataConverter> that maps Java classes
> * to DataConverter objects that know how to operate on
> * data of the type specified by the map's keys.
> */
> public <C extends Class> Map<C,DataConverter<? extends C,?>>
> getDataConverters();
>
> I cannot, however, figure out exactly how to instantiate a Map that I
> could actually return from an implementation of this method!


What are you trying to do? <C extends Class> is pointless since Class
is final and there can't be any class that extends it. So your
declaration can be reduced to:

Map<Class,DataConverter<Class,?>> getDataConverters();

So you return a Map which maps from an instance of Class to a
DataConverter that takes an instance of Class (not necessarily the
same instance used as the key) and returns anything. Doesn't make much
sense.

Are you trying to create a Map which maps from a Class instance to a
DataConverter that only takes instances of that Class as argument and
that this should be checked during compile time? It's impossible to
create a Map that does that.

/Jesper Nordenberg
 
Reply With Quote
 
Sebastian Millies
Guest
Posts: n/a
 
      10-06-2004
Really? I havn't done anything with generics, but in the1.5 API docs
define Class as
public final class Class<T>extends Objectimplements Serializable,
GenericDeclaration, Type, AnnotatedElementI suppose the parameter specifies
the type of object that is represented by
the Class instance? So would not a Map which maps from a Class instance to a
DataConverter that only takes instances of that Class as argument be
declared
as:

Map<Class<T>,DataConverter<Class<T>,?>>

Sorry, can't test this for lack of a JDK.

-- Sebastian

"Jesper Nordenberg" <> schrieb im Newsbeitrag
news: m...
> Kris Nuttycombe < > wrote

in message news:<cjv5fa$8pg$>...
> > Thanks for the excellent FAQ! I have another question for it (although
> > whether it is frequently asked remains to be seen!
> >
> > I have the following interface:
> >
> > public interface DataConverter<I,O> {
> > public O convert(I obj);
> > }
> >
> > It appears that it should be legal to define the following method:
> >
> > /**
> > * Returns a Map<Class,DataConverter> that maps Java classes
> > * to DataConverter objects that know how to operate on
> > * data of the type specified by the map's keys.
> > */
> > public <C extends Class> Map<C,DataConverter<? extends C,?>>
> > getDataConverters();
> >
> > I cannot, however, figure out exactly how to instantiate a Map that I
> > could actually return from an implementation of this method!

>
> What are you trying to do? <C extends Class> is pointless since Class
> is final and there can't be any class that extends it. So your
> declaration can be reduced to:
>
> Map<Class,DataConverter<Class,?>> getDataConverters();
>
> So you return a Map which maps from an instance of Class to a
> DataConverter that takes an instance of Class (not necessarily the
> same instance used as the key) and returns anything. Doesn't make much
> sense.
>
> Are you trying to create a Map which maps from a Class instance to a
> DataConverter that only takes instances of that Class as argument and
> that this should be checked during compile time? It's impossible to
> create a Map that does that.
>
> /Jesper Nordenberg



 
Reply With Quote
 
Sebastian Millies
Guest
Posts: n/a
 
      10-06-2004
short addition: re-reading the OP's requuirement, I guess that
should really be:

Map<Class<T>,DataConverter<T,?>>

It would even be easy to add additional contraints on T,
perhaps in terms of an interface ConvertibleTo<E>, and
specify DataConverter as

public interface DataConverter<I extends ConvertibleTo<E>,E>

Does this make sense?

-- Sebastian

"Sebastian Millies" <> schrieb im
Newsbeitrag news:4163cbac$0$287$...
> Really? I havn't done anything with generics, but in the1.5 API docs
> define Class as
> public final class Class<T>extends Objectimplements Serializable,
> GenericDeclaration, Type, AnnotatedElementI suppose the parameter

specifies
> the type of object that is represented by
> the Class instance? So would not a Map which maps from a Class instance to

a
> DataConverter that only takes instances of that Class as argument be
> declared
> as:
>
> Map<Class<T>,DataConverter<Class<T>,?>>
>
> Sorry, can't test this for lack of a JDK.
>
> -- Sebastian
>
>



 
Reply With Quote
 
Kris Nuttycombe
Guest
Posts: n/a
 
      10-06-2004
That's exactly what I was looking for, thanks! I like the addition of
the ConvertibleTo<E> interface as well.

Kris

Sebastian Millies wrote:
> short addition: re-reading the OP's requuirement, I guess that
> should really be:
>
> Map<Class<T>,DataConverter<T,?>>
>
> It would even be easy to add additional contraints on T,
> perhaps in terms of an interface ConvertibleTo<E>, and
> specify DataConverter as
>
> public interface DataConverter<I extends ConvertibleTo<E>,E>
>
> Does this make sense?
>
> -- Sebastian
>
> "Sebastian Millies" <> schrieb im
> Newsbeitrag news:4163cbac$0$287$...
>
>>Really? I havn't done anything with generics, but in the1.5 API docs
>>define Class as
>>public final class Class<T>extends Objectimplements Serializable,
>>GenericDeclaration, Type, AnnotatedElementI suppose the parameter

>
> specifies
>
>>the type of object that is represented by
>>the Class instance? So would not a Map which maps from a Class instance to

>
> a
>
>>DataConverter that only takes instances of that Class as argument be
>>declared
>>as:
>>
>>Map<Class<T>,DataConverter<Class<T>,?>>
>>
>>Sorry, can't test this for lack of a JDK.
>>
>>-- Sebastian
>>
>>

>
>
>


 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      10-06-2004
"Sebastian Millies" <> wrote in message news:<4163d061$0$286$>...
> short addition: re-reading the OP's requuirement, I guess that
> should really be:
>
> Map<Class<T>,DataConverter<T,?>>


This is not what I meant, but sure, this can be done, but it doesn't
make any sense. There is only one key you can use for such a map
(since there is only one instance of Class<T> for any T), so why are
you even using a Map in the first place?

I think the OP wants a map where you given a class can lookup a
DataConverter for a objects of that class. Something like this:

interface ObjectConverterMap<O> {
<T> void put(Class<T> key, DataConverter<? super T, O> value);
<T> DataConverter<? super T, O> get(Class<T> key);
}

Or maybe he wants a DataConverter for the class object:

interface ClassConverterMap<O> {
<T> void put(Class<T> key, DataConverter<Class<T>, O> value);
<T> DataConverter<Class<T>, O> get(Class<T> key);
}

Neither of these interfaces are compatible with the java.util.Map
interface.

/Jesper Nordenberg
 
Reply With Quote
 
Kris Nuttycombe
Guest
Posts: n/a
 
      10-06-2004
Aha, Now I understand what I was doing wrong. My confusion was between
the runtime and compile time types. Thanks!

Kris

Jesper Nordenberg wrote:
> "Sebastian Millies" <> wrote in message news:<4163d061$0$286$>...
>
>>short addition: re-reading the OP's requuirement, I guess that
>>should really be:
>>
>>Map<Class<T>,DataConverter<T,?>>

>
>
> This is not what I meant, but sure, this can be done, but it doesn't
> make any sense. There is only one key you can use for such a map
> (since there is only one instance of Class<T> for any T), so why are
> you even using a Map in the first place?
>
> I think the OP wants a map where you given a class can lookup a
> DataConverter for a objects of that class. Something like this:
>
> interface ObjectConverterMap<O> {
> <T> void put(Class<T> key, DataConverter<? super T, O> value);
> <T> DataConverter<? super T, O> get(Class<T> key);
> }
>
> Or maybe he wants a DataConverter for the class object:
>
> interface ClassConverterMap<O> {
> <T> void put(Class<T> key, DataConverter<Class<T>, O> value);
> <T> DataConverter<Class<T>, O> get(Class<T> key);
> }
>
> Neither of these interfaces are compatible with the java.util.Map
> interface.
>
> /Jesper Nordenberg


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Generics in Java 1.5 ( or is it java 5.0 ?... I always haveconfusion) Vikram Java 4 06-13-2008 11:40 AM
Any tool to convert java raw code (a la java 1.4) into generics code Royan Java 8 02-15-2008 02:35 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Java Generics: limitations? Hung Jung Lu Java 4 11-24-2003 05:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57