Forum Stats

  • 3,852,353 Users
  • 2,264,094 Discussions


java.lang.ref.ReferenceQueue parameterisation

843793 Member Posts: 41,732 Green Ribbon
edited Nov 1, 2002 6:06AM in Generics
I want a ReferenceQueue into which I place only WeakReference<MyClass> objects, and from which I can poll WeakReference<MyClass> objects without casts or warnings.

I haven't been able to find any documentation on the genericised version of ReferenceQueue, and am going solely on error messages from the compiler. However, it appears that this class has public signature

public class ReferenceQueue<T>
Reference<T> poll();
� Reference<T> remove();
� Reference<T> remove(long�timeout);

Why? This doesn't seem very logical to me, and is forcing me to put in casts. Is there any good reason why it is not as follows?

public class ReferenceQueue<R extends Reference>
R poll();
R remove();
R remove(long timeout);

If not, is there any possibility that it would be altered thus before 1.5?


  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I don't understand. The code you originally show appears more typesafe and should not produce warnings. The code you would like it changed to would not be as typesafe (e.g. you would need more casts and you could get ClassCastExceptions) and would generate warnings when used with generic Reference types.

    Maybe you could post some code showing the casts you are forced to put in or warnings you get from the compiler?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    It appears that the casts are unnecessary after all, because Set<T>.remove takes an Object rather than a T, so will accept a Reference. I had been thinking I needed to give it a WeakReference<MyIterator>. So it is not a problem after all.

    You're right about my solution not being so typesafe. I hadn't fully thought through how the Reference constructor would be genericised.
This discussion has been closed.