Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

More questions about generics

Thread view: 
Ricardo Palomares Martinez - 07 Mar 2008 21:13 GMT
Hi,

I'm failing to see the best way to organize a couple of classes and
containers for them. Maybe I'm stubbornly trying to base it in
generics and I should just use a Factory pattern, I'm not sure.

I have a base "element" class (GlossaryEntry) like this:

public class GlossaryEntry {
   private String value;
   ...
}

and two classes extending it, GlossaryTerm and GlossaryTranslation:

public class GlossaryTerm extends GlossaryEntry {
   private List l;
   ...
}

public class GlossaryTranslation extends GlossaryEntry {
   private String l10nCode;
   ...
}

Now I want to have a base container for GlossaryEntry, with
corresponding extended containers for the base classes. Something like
this:

public class GlossaryEntries {
   private final List<GlossaryEntry> geList;

   // Common methods to add, delete, and retrieve elements, which
   // will be useful for both GlossaryTerm and GlossaryTranslation
   // lists
}

public class GlossaryTerms extends GlossaryEntries {
   // Specific methods useful just for GlossaryTerm lists
}

public class GlossaryTranslations extends GlossaryEntries {
   // Specific methods useful just for GlossaryTranslations lists
}

However, I'd like to make sure that GlossaryTerms only holds
GlossaryTerm elements, and do the same check with
GlossaryTranslations. So, I redefine GlossaryEntries like this:

public class GlossaryEntries<T extends GlossaryEntry> {
   private final List<T> geList;
   ...
}

but I can't see how to tell in GlossaryTerms definition that "T" must
be only "GlossaryTerm". Is there a simple way to do it "in generic
syntax"?

The other option, I guess, would be to write a Factory so I could
request a GlossaryEntries object for terms (in which case I would
return a GlosaryTerms<GlossaryTerm>), translations
(GlossaryTranslations<GlossaryTranslation>) or base (a generic
GlossaryEntries<GlossaryEntry>). Is this a sensible approach?

TIA

Signature

If it's true that we are here to help others,
then what exactly are the OTHERS here for?

Mark Space - 07 Mar 2008 22:50 GMT
> The other option, I guess, would be to write a Factory so I could
> request a GlossaryEntries object for terms (in which case I would
> return a GlosaryTerms<GlossaryTerm>), translations
> (GlossaryTranslations<GlossaryTranslation>) or base (a generic
> GlossaryEntries<GlossaryEntry>). Is this a sensible approach?

This is how I would do it.  You say you only want GloasaryTerm's in the
GlosaryTerms list, but in the previous example (not quoted here) you
allow things that extend GlossaryEntry in the list.  That seems just
plain going in the wrong direction.
Daniel Pitts - 08 Mar 2008 17:37 GMT
> Hi,
>
[quoted text clipped - 61 lines]
>
> TIA

class GlosseryTerm extends GlosseryEntry {
   List<String> l; // Use generics here too!
}

class GlosseryEntries<T extends GlosseryEntry> {
   List<T> entries;
}

class GlosseryTerms<T extends GlosseryTerms> extends GlosseryEntries<T> {
}

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Lew - 08 Mar 2008 19:30 GMT
> class GlosseryTerm extends GlosseryEntry {
>    List<String> l; // Use generics here too!
[quoted text clipped - 6 lines]
> class GlosseryTerms<T extends GlosseryTerms> extends GlosseryEntries<T> {
> }

A work of art, both as code and as an example.

There seems to be no way around the twistedness of generics thinking, and no
end to its effectiveness when you get it right.

The Rosetta Stone for generics seems to be that generified types don't inherit
naively - you have to play with the 'extends' and 'super' keywords in the
formal type parameters.  Well, part - another part is to satori-ize the
interactions between compile-time generic type safety and run-time generic
type erasure.

<http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html>
<http://java.sun.com/docs/books/tutorial/extra/generics/morefun.html>
<http://java.sun.com/docs/books/tutorial/extra/generics/literals.html>

Signature

Lew

Ricardo Palomares Martinez - 10 Mar 2008 19:55 GMT
Daniel Pitts escribió:

> class GlosseryTerm extends GlosseryEntry {
>    List<String> l; // Use generics here too!
> }

To be honest, that list will actually be GlossaryTranslations, but I
didn't want to make the example too complex. :-)

> class GlosseryEntries<T extends GlosseryEntry> {
>    List<T> entries;
> }

OK, that's what I already have.

> class GlosseryTerms<T extends GlosseryTerms> extends GlosseryEntries<T> {
> }

Maybe there is a typo above? I'm not sure if you meant to write:

class GlosseryTerms<T extends *GlosseryEntry*> extends
       GlosseryEntries<T> {
}

or

class GlosseryTerms<T extends *GlosseryTerm*> extends
       GlosseryEntries<T> {
}

I understand that the first form doesn't add any further type
restriction, so I'm trying with the second one.

Thank you very much for your example. As Lew said, beatiful,
thoughtful and smart. I read the Generics tutorial, but after
finishing it I saw the link to the "other" Generics tutorial (the one
you have linked to) and, in a quick review of the TOC, I thought it
was more or less the same, so I mistakenly didn't bother to dig in it.
My fault (again, should I add).

Thanks for you patience. :-)

Signature

If it's true that we are here to help others,
then what exactly are the OTHERS here for?

Roedy Green - 14 Mar 2008 07:39 GMT
On Fri, 07 Mar 2008 22:13:08 +0100, Ricardo Palomares Martinez
<rpm.PUBLI@iespana.es> wrote, quoted or indirectly quoted someone who
said :

>I'm failing to see the best way to organize a couple of classes and
>containers for them. Maybe I'm stubbornly trying to base it in
>generics and I should just use a Factory pattern, I'm not sure.

some background information that may help:

see http://mindprod.com/jgloss/generics.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.