How to make a photo smaller?
Fwd: How to cast generic collections..
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > Java Programming > Fwd: How to cast generic collections.. 1 September 2008 16:48:20

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Fwd: How to cast generic collections..

Endre St lsvik 1 September 2008 16:48:20
 Hi.

I have bumped into a problem a couple of times which I can't seem to
quite understand how to handle best. It revolves around casting the
type of a generic collection.

My situation is something like this: An interface DiskResource, whose
instances users get from a DiskProvider extends Provider instance.

The DiskProvider has these two methods:
releaseDiskResource­(DiskResource diskResource);
releaseDiskResource­s(Collection<DiskRes­ource> diskResources);

I have this helper class for implementing Providers, called Helper.
This helper requires that the Resources implement Helped. I therefore
make a DiskResourceImpl which implement DiskResource and Helped. I
thus need to implement the DiskProviderImpl as such:

releaseDiskResource­(DiskResource diskResource) {
// Cast, with unchecked-warning:
helper.doInternalSt­uff((Helped) diskResource)
}

releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
// PROBLEM HERE, this cast is an ERROR, not a warning:
helper.doInternalSt­uff((Collection<Help­ed>) resourceImpls);
}

How am I supposed to do this cast? See, I know why it isn't allowed to
cast like that, but I want to nevertheless! It seems strangely
non-symmetric that I just cast it in the singular version (with a
unchecked-warning),­ but end up with an error in the other. Is the
latter that more dangerous than the first?

Both these are "solutions":
releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
Collectioc<Helped> c = new HashSet<Helped>();
for(DiskResource diskResource : diskResources) {
c.add((Helped) diskResource);
}
helper.doInternalSt­uff(c);
}

releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
helper.doInternalSt­uff((Collection) c);
}

.. of which I refuse to use #1, while #2 seems like a just-to-weird
hack around the compiler error, replacing the error with two warnings
(!). Raw types are the evilest there is, are they not?

I hope this example points out my annoyance while not being too convoluted.

Where is my thinking derailing? I have a feeling it is a couple of
steps before these methods' implementation, but I've tried different
attacks now. I don't want the users of this API to see internal stuff
that is useless for them.

Regards.
Endre.

===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Kevin Bourrillion 29 August 2008 20:15:08 permanent link ]
 Endre,

Your helper methods need to accept "Collection<? extends Helped>". Then you
should be fine, and you don't even need to cast.

K




On Fri, Aug 29, 2008 at 5:58 AM, Endre St lsvik <Online@stolsvik.co­m> wrote:

Hi.
I have bumped into a problem a couple of times which I can't seem to
quite understand how to handle best. It revolves around casting the
type of a generic collection.
My situation is something like this: An interface DiskResource, whose
instances users get from a DiskProvider extends Provider instance.
The DiskProvider has these two methods:
releaseDiskResource­(DiskResource diskResource);
releaseDiskResource­s(Collection<DiskRes­ource> diskResources);
I have this helper class for implementing Providers, called Helper.
This helper requires that the Resources implement Helped. I therefore
make a DiskResourceImpl which implement DiskResource and Helped. I
thus need to implement the DiskProviderImpl as such:
releaseDiskResource­(DiskResource diskResource) {
// Cast, with unchecked-warning:
helper.doInternalSt­uff((Helped) diskResource)
}
releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
// PROBLEM HERE, this cast is an ERROR, not a warning:
helper.doInternalSt­uff((Collection<Help­ed>) resourceImpls);
}
How am I supposed to do this cast? See, I know why it isn't allowed to
cast like that, but I want to nevertheless! It seems strangely
non-symmetric that I just cast it in the singular version (with a
unchecked-warning),­ but end up with an error in the other. Is the
latter that more dangerous than the first?
Both these are "solutions":
releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
Collectioc<Helped> c = new HashSet<Helped>();
for(DiskResource diskResource : diskResources) {
c.add((Helped) diskResource);
}
helper.doInternalSt­uff(c);
}
releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
helper.doInternalSt­uff((Collection) c);
}
.. of which I refuse to use #1, while #2 seems like a just-to-weird
hack around the compiler error, replacing the error with two warnings
(!). Raw types are the evilest there is, are they not?
I hope this example points out my annoyance while not being too convoluted.
Where is my thinking derailing? I have a feeling it is a couple of
steps before these methods' implementation, but I've tried different
attacks now. I don't want the users of this API to see internal stuff
that is useless for them.
Regards.
Endre.
===================­================
This list is hosted by DevelopMentor(R) http://www.develop.­com
View archives and manage your subscription(s) at



--
Kevin Bourrillion @ Google
internal: go/javalibraries
google-collections.­googlecode.com
google-guice.google­code.com

===================­================
This list is hosted by DevelopMentor® http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Endre St lsvik 1 September 2008 16:48:20 permanent link ]
 On Fri, Aug 29, 2008 at 6:15 PM, kevin bourrillion <kevinb@google.com>­ wrote:

Thanks for answering!

Your helper methods need to accept "Collection<? extends Helped>". Then you
should be fine, and you don't even need to cast.

Hmm.. Maybe my simplified example was too simplified or convoluted.

Since the user-facing interface DiskResource does not extend Helped,
how would the suggested solution "match"? It is the internal
DiskResourceImpl that implements Helped (and obviously DiskResource).
What I need to be able to state, is what I do in the singular case:
tell the compiler that I know something it doesn't know: This
Collection of objects, which seemingly does not implement Helped by
any route, will actually in the correct use-cases implement it anyway.
This, in the singular case, translates to "cast".

I actually thought this was somewhat commonplace in e.g. containers:
The container knows that the API-supplied user-facing Context in
question is actually an InternalContextThin­gy implementing lots of
internal interfaces, and casts it to what it needs when the user
supplies it as argument to the API methods. How does it cast a
user-supplied Collection of them? Is the only properish answer "one by
one"? I guess one would need a "I promise I'll only read from it!"
collection-cast. I went looking for some solution in the
Collections.unmodif­iableCollection, but couldn't find any..

PS: here's another "solution" that works, possibly even more absurd
than the other two. However, we now at least avoid going all the way
down to raw types, thus giving me just the one warning which is the
same as the singular one: unchecked cast. I don't quite get why this
should be that much better than the direct cast, though..!

releaseDiskResource­s(Collection<DiskRes­orce> diskResources) {
helper.doInternalSt­uff((Collection<Help­ed>) (Collection<?>) c);
}

Kind regards,
Endre.

===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > Java Programming > Fwd: How to cast generic collections.. 1 September 2008 16:48:20

see also:
www.php.net offline for a bit
PHP Session song...
store file using socket/fopen
пройди тесты:
see also:
How to transfer WMAs and iTunes 8…
How to rip DVD movie, convert video and…
Error while using Itext.jar

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .