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 / January 2005

Tip: Looking for answers? Try searching our database.

question on generics in class type declarations

Thread view: 
Roger Levy - 13 Oct 2004 23:05 GMT
I have a simple problem using generics.  Suppose I want to make a
parameterized holder class, call it Singleton, that contains a single
object o, and instances of Singleton containing objects of comparable
type should themselves be comparable by these objects.  So I want
something like

public class Singleton<T1 extends Comparable<? super T1>> implements
Comparable<Singleton<T2>> {
 T1 x;

 public int compareTo(Singleton<T2> s) {
   return x.compareTo(s.x);
 }

}

while constraint that T1 and T2 are themselves mutually comparable.
At any rate, the above is not legal Java 1.5 code.

Any suggestions will be much appreciated!

Many thanks,

Roger Levy
Larry Barowski - 14 Oct 2004 08:38 GMT
> I have a simple problem using generics.  Suppose I want to make a
> parameterized holder class, call it Singleton, that contains a single
[quoted text clipped - 14 lines]
> while constraint that T1 and T2 are themselves mutually comparable.
> At any rate, the above is not legal Java 1.5 code.

How about this?

   class Singleton<T1 extends Comparable<? super T1>> implements
     Comparable<Singleton<? extends Comparable<? super T1>>> {
     T1 x;

      public int compareTo(Singleton<? extends Comparable<? super T1>> s) {
        return s.x.compareTo(x);
     }
  }
Roger Levy - 15 Oct 2004 01:37 GMT
> > I have a simple problem using generics.  Suppose I want to make a
> > parameterized holder class, call it Singleton, that contains a single
[quoted text clipped - 25 lines]
>       }
>    }

Thank you!  This works.

It appears that the problem with my version is that although we have
stipulated that the type of x extends Comparable<T1A super T1> and the
type of s.x extends Comparable<T1B super T1> for some T1A and T1B, we
have no guarantee that the type of s.x extends T1A.  This seems to me
like a shortcoming of the syntax of generics for Java 1.5, what does
anyone else think?

Thanks again.

Roger
Larry Barowski - 15 Oct 2004 08:44 GMT
> It appears that the problem with my version is that although we have
> stipulated that the type of x extends Comparable<T1A super T1> and the
> type of s.x extends Comparable<T1B super T1> for some T1A and T1B, we
> have no guarantee that the type of s.x extends T1A.  This seems to me
> like a shortcoming of the syntax of generics for Java 1.5, what does
> anyone else think?

By "my version", do you mean if you had done:
  public class Singleton<T1 extends Comparable<? super T1>> implements
     Comparable<Singleton<?>>>
In that case there would be no required relation between "T2"
and T1 at all.

I don't think more generic bounds syntax would help
in your case. The bounds I gave above are about as
complex as the English language description of the
required type restrictions. Anything more concise than
that would be obfuscation.
Roger Levy - 15 Oct 2004 18:45 GMT
> > It appears that the problem with my version is that although we have
> > stipulated that the type of x extends Comparable<T1A super T1> and the
[quoted text clipped - 8 lines]
> In that case there would be no required relation between "T2"
> and T1 at all.

Sorry, my reference wasn't clear at all.  I had originally written
(privately, not in my post) the same class class declaration and
method signature that you had, but in the method body I'd used

      public int compareTo(Singleton<? extends Comparable<? super
T1>> s) {
        return x.compareTo(s.x);
     }

> I don't think more generic bounds syntax would help
> in your case. The bounds I gave above are about as
> complex as the English language description of the
> required type restrictions. Anything more concise than
> that would be obfuscation.

Hmm...let's compare the generics syntax for method signatures and
class declarations.  In a method signature, you can declare and place
constraints on type variables that never occur in the method body, and
that never need to be specified by the API client.  This makes code
like the following possible:

 public static <T extends Comparable<? super T>, T1 extends T, T2
extends T> int compare(T1 x1, T2 x2) {
   return x1.compareTo(x2);
 }

 public static void main(String[] args) {
   Date x1 = new Date(1);
   Timestamp x2 = new Timestamp(3);
   int i = compare(x1,x2);
 }

IMHO, the type variable declaration in this static compare method is
extremely clear -- the two argument types must have a common supertype
that is Comparable with itself.  Note that the type variable T exists
solely to specify the constraints on the common supertype.  It never
appears in the method body, and the API client never needs to worry
about it.

As far as I can tell, there is no way to do something analogous in a
class declaration, because each type variable declared must be
specified by the client upon type declaration.  What we'd really want,
as Jesper pointed out, is something that allows the introduction of
new type variables in the class declaration that do not need to be
specified by the API client at type declaration.  This could be
something like what I originally wrote:

public class Singleton<T1 extends Comparable<? super T1>> implements
Comparable<Singleton<T2>> {
 T1 x;

 public int compareTo(Singleton<T2> s) {
   return x.compareTo(s.x);
 }
}

or something like what Jesper wrote:

public class Singleton<T1 extends Comparable<T2>> implements
Comparable<Singleton<? extends T2>> { ... }

I appreciate all the input on my problem -- I understand the issues
much better now!

Cheers,

Roger
Larry Barowski - 15 Oct 2004 20:59 GMT
> Hmm...let's compare the generics syntax for method signatures and
> class declarations.  In a method signature, you can declare and place
[quoted text clipped - 10 lines]
> As far as I can tell, there is no way to do something analogous in a
> class declaration

Ah, I see your point. Something like that would add a lot more power
to the bounds.
Jesper Nordenberg - 15 Oct 2004 09:36 GMT
> It appears that the problem with my version is that although we have
> stipulated that the type of x extends Comparable<T1A super T1> and the
> type of s.x extends Comparable<T1B super T1> for some T1A and T1B, we
> have no guarantee that the type of s.x extends T1A.  This seems to me
> like a shortcoming of the syntax of generics for Java 1.5, what does
> anyone else think?

I would like to have the possibility to introduce named types that are
not type parameters of the parameterized class. For example:

class A<T extends B<U super T>> {
 private U value;
}

I don't see why this shouldn't be allowed.

/Jesper Nordenberg
Neal  Gafter - 05 Jan 2005 04:07 GMT
> I would like to have the possibility to introduce named types that are
> not type parameters of the parameterized class. For example:
[quoted text clipped - 5 lines]
>
> I don't see why this shouldn't be allowed.

You'd write it this way:

class A<U, T extends U&B<U>> {
private U value;
}

It doesn't make any sense to me that you don't want U to be a parameter
of A.
Jesper Nordenberg - 14 Oct 2004 13:53 GMT
> I have a simple problem using generics.  Suppose I want to make a
> parameterized holder class, call it Singleton, that contains a single
[quoted text clipped - 16 lines]
>
> Any suggestions will be much appreciated!

If I understand correctly you want:

public class Singleton<T1 extends Comparable<T2>> implements
Comparable<Singleton<? extends T2>> {

where T2 is automatically inferred by the compiler. This is not
supported by the compiler though, so I don't think you can solve your
problem without introducing new type parameters to the Singleton class
which you probably don't want to do. The best I could come up with is:

public class Singleton<T1 extends Comparable<? super T1>> implements
Comparable<Singleton<? extends T1>> {
 private T1 x;

 public int compareTo(Singleton<? extends T1> s) {
   return x.compareTo(s.x);
 }

}

which fails in this example:

class A implements Comparable<A>
class B extends A
class C extends A
new Singleton<B>().compareTo(new Singleton<C>());  // Compile error!

/Jesper Nordenberg
Larry Barowski - 15 Oct 2004 09:05 GMT
> > I have a simple problem using generics.  Suppose I want to make a
> > parameterized holder class, call it Singleton, that contains a single
[quoted text clipped - 26 lines]
> problem without introducing new type parameters to the Singleton class
> which you probably don't want to do.

Right, there is no solution if you want to use "x.compareTo(s.x)".
Using "s.x.compareTo(x)" instead leads to an obvious answer,
since then "T2" can be specified in terms of T1.


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.