Java Generics FAQ

Discussion in 'Java' started by Angelika Langer, Oct 5, 2004.

  1. 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
    Angelika Langer, Oct 5, 2004
    #1
    1. Advertising

  2. 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/GenericsFAQ/JavaGenericsFAQ.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
    Andrew Thompson, Oct 5, 2004
    #2
    1. Advertising

  3. 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/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
    Kris Nuttycombe, Oct 5, 2004
    #3
  4. Andrew Thompson, Oct 5, 2004
    #4
  5. 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
    Jesper Nordenberg, Oct 6, 2004
    #5
  6. 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:...
    > 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
    Sebastian Millies, Oct 6, 2004
    #6
  7. 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
    >
    >
    Sebastian Millies, Oct 6, 2004
    #7
  8. 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
    >>
    >>

    >
    >
    >
    Kris Nuttycombe, Oct 6, 2004
    #8
  9. "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
    Jesper Nordenberg, Oct 6, 2004
    #9
  10. 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
    Kris Nuttycombe, Oct 6, 2004
    #10
  11. Angelika Langer

    David Guest

    David, Oct 6, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hung Jung Lu

    Java Generics: limitations?

    Hung Jung Lu, Nov 21, 2003, in forum: Java
    Replies:
    4
    Views:
    547
    Tor Iver Wilhelmsen
    Nov 24, 2003
  2. Juergen Berchtel
    Replies:
    1
    Views:
    5,970
    John C. Bollinger
    May 20, 2005
  3. Royan
    Replies:
    8
    Views:
    739
    Patricia Shanahan
    Feb 15, 2008
  4. Vikram
    Replies:
    4
    Views:
    515
    Vikram
    Jun 13, 2008
  5. Soul
    Replies:
    0
    Views:
    505
Loading...

Share This Page