Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #27769

Re: Parameterized collection catch-22

Newsgroups comp.lang.java.programmer
Date 2014-03-16 17:06 -0700
References <6f84734b-69d5-471c-b041-8d37dd5657e5@googlegroups.com> <lg0Uu.13736$Nm1.6311@fx22.iad>
Message-ID <8bcf3da9-175d-4894-b80f-657d9f39afae@googlegroups.com> (permalink)
Subject Re: Parameterized collection catch-22
From zapanaz_googlegroups@hotmail.com

Show all headers | View raw


On Wednesday, March 12, 2014 9:51:33 AM UTC-7, Daniel Pitts wrote:
> On 3/11/14 9:36 AM, zapanaz_...@hotmail.com wrote:
> 
> > I have a parent class, AddressInfo. An abstract class. Two classes inherit from AddressInfo, ShippingInfo and BillingInfo. ShippingInfo and BillingInfo are nearly identical, and are identical for what I'm doing here.
> 
> >
> 
> > The example code I'm posting is stripped down, in case you wonder why it's a little dumb.
> 
> >
> 
> > I have two methods,
> 
> >
> 
> > public void mergeBillingInfoLists(List<BillingInfo> list1, List<BillingInfo> list2) {
> 
> > list1.add(1, list2.get(0));
> 
> > }
> 
> >
> 
> > And one for ShippingInfo which is identical, just with ShippingInfo where BillingInfo is.
> 
> >
> 
> > I want to combine the two methods, which are very redundant. It seems like it should be possible, given that they have a common parent class. I can do this:
> 
> >
> 
> > public void mergeAddressInfo(List<? extends AddressInfo> list1, List<? extends AddressInfo> list2) {
> 
> > list1.add(1, list2.get(0));
> 
> > }
> 
> >
> 
> > But Java won't allow this, it won't allow the add(). All it knows about what list2 contains is that it's something that descends from AddressInfo, so even if I pass in 2 List<BillingInfo>'s, inside the code it doesn't know they're the same classes.
> 
> >
> 
> > Does anybody offhand know a way to do this? (Without throwing away the parameterization :) )
> 
> >
> 
> > I don't necessarily need to use this exact approach, just anything that would allow me to combine the two methods into one method. In fact I have quite a lot of this kind of redundancy in the code base I'm working with, so this is something I'm running into a lot.
> 
> >
> 
> > Thanks for any help
> 
> >
> 
> 
> 
> There are three ways write that so it works as you desire:
> 
> 
> 
> Method one:
> 
> public <T> void mergeInfo(List<? super T> list1,List<? extends T> list2)
> 
> 
> 
> T is any type at all.
> 
> list1 can be any type of List of <T> or List of <SuperclassOfT>  This 
> 
> means the list1 can accept any object that is assignable *from* <T>.  It 
> 
> is unknown the type that it produces.
> 
> 
> 
> list2 can be any type of List of <T> or List of <SubclassOfT>.  It will 
> 
> produce objects that assignable *to* <T>.
> 
> 
> 
> 
> 
> 
> 
> Method two:
> 
> public <T> void mergeInfo(List<T> list1,List<? extends T> list2)
> 
> 
> 
> T is any type at all.
> 
> list1 can be any type of List of <T>, but not any other type parameter. 
> 
>   list1 can therefor produce and accept any object assignable to or from <T>
> 
> 
> 
> list2 is the same as list2 from Method One
> 
> 
> 
> 
> 
> Method three:
> 
> public <T> void mergeInfo(List<T> list1,List<T> list2)
> 
> 
> 
> T is any type at all.
> 
> list1 can be any type of List of <T>, but not any other type parameter. 
> 
>   list1 can therefor produce and accept any object assignable to or from <T>
> 
> 
> 
> list2 must be a List with the exact same type parameter as list1.
> 
> 
> 
> 
> 
> And finally, one last note.  For any of the examples above, if you want 
> 
> to ensure that you're only dealing with subclasses of AddressInfo:
> 
> 
> 
> public <T extends AddressInfo> void mergeInfo(... yata yata)
> 
> 
> 
> 
> 
> This says "T must be AddressInfo or a subclass thereof".
> 
> 
> 
> In your particular case, I would combine this with Method Three. Though 
> 
> often with generic functions, you're better off with Method one.
> 
> 
> 
> The rule-of-thumb I use is: In your scope if you have an object that 
> 
> only accepts <T> use <? super T>. If you have an object that only 
> 
> produces <T>, use <? extends T>.  If you have an object that produces 
> 
> and accepts <T>, then use <T>.  I'm talking about "produces" or 
> 
> "accepts" in what you call, not what methods are available.
> 
> 
> 
> HTH.


Thanks very much

-- 
Joe Cosby

Agent 99: Max, that knife missed you by inches.
Maxwell Smart: You think it's some kind of warning?

:: Currently listening to All My Life, 1972, by Uriah Heep, from "Demons and Wizards" 

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Parameterized collection catch-22 zapanaz_googlegroups@hotmail.com - 2014-03-11 09:36 -0700
  Re: Parameterized collection catch-22 markspace <markspace@nospam.nospam> - 2014-03-11 11:14 -0700
    Re: Parameterized collection catch-22 zapanaz_googlegroups@hotmail.com - 2014-03-16 17:04 -0700
      Re: Parameterized collection catch-22 Lew <lewbloch@gmail.com> - 2014-03-22 15:42 -0700
        Re: Parameterized collection catch-22 zapanaz_googlegroups@hotmail.com - 2014-03-23 16:35 -0700
  Re: Parameterized collection catch-22 Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-03-12 09:51 -0700
    Re: Parameterized collection catch-22 zapanaz_googlegroups@hotmail.com - 2014-03-16 17:06 -0700

csiph-web