Java Generics FAQ

  • Thread starter Angelika Langer
  • Start date
A

Angelika Langer

I've been putting together a page with FAQs regarding Java Generics at
http://www.langer.camelot.de/GenericsFAQ/JavaGenericsFAQ.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
 
A

Andrew Thompson

K

Kris Nuttycombe

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
 
J

Jesper Nordenberg

Kris Nuttycombe said:
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
 
S

Sebastian Millies

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
 
S

Sebastian Millies

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
 
K

Kris Nuttycombe

That's exactly what I was looking for, thanks! I like the addition of
the ConvertibleTo<E> interface as well.

Kris
 
J

Jesper Nordenberg

Sebastian Millies said:
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
 
K

Kris Nuttycombe

Aha, Now I understand what I was doing wrong. My confusion was between
the runtime and compile time types. Thanks!

Kris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top