The safest approach is composition. It's the only way for the immutability
contract to be honored across development teams (Commons + end user).


On Wed, Oct 23, 2013 at 1:00 AM, Duncan Jones <djo...@cantab.net> wrote:

> On 23 Oct 2013 01:26, "Gary Gregory" <garydgreg...@gmail.com> wrote:
> >
> > On Tue, Oct 22, 2013 at 4:53 PM, Duncan Jones <djo...@cantab.net> wrote:
> >
> > > On 22 October 2013 21:07, Gary Gregory <garydgreg...@gmail.com> wrote:
> > > > On Tue, Oct 22, 2013 at 3:59 PM, Matt Benson <gudnabr...@gmail.com>
> > > wrote:
> > > >
> > > >> So what is having a non-generic runtime class accomplishing for you?
> > > >>
> > > >
> > > > The subclass "is a kind of" pair AND it is domain typed to my app.
> > > >
> > > > Gary
> > >
> > > Sebb is correct - composition would work well here. You can still
> > > defer to the equals/hashcode from the underlying ImmutablePair as you
> > > appear to store no other state within the class.
> > >
> >
> > I could do it that way and end up with a hierarchy likeL
> >
> > - Lang final ImmutablePair<K,V>
> > - My ImmutablePairProxy<K, V> wraps an ImmutablePair<K,V>
> > - My ImmutablePairProxyClass extends ImmutablePairProxy<Foo, Bar>
> > - My ImmutablePairProxyClass extends ImmutablePairProxy<Zing, Bat>
> >
> > It's doable but it feels more spaghetti like than subclassing
> ImmutablePair.
> >
> > Gary
>
> But that approach will suffer from the same problems discussed earlier. Any
> non final "immutable" class can be undone by a pesky subclass that doesn't
> obey the immutability, deliberately or accidentally.
>
> I guess it depends how much of a purist you are.
>
> A weaker alternative is to mark your inner Pair object as private (which it
> probably is) and mark all the public methods of ImmutablePairProxy as final
> too.
>
> Duncan
>
> > >
> > >
> > > >
> > > >
> > > >>
> > > >> Matt
> > > >>
> > > >>
> > > >> On Tue, Oct 22, 2013 at 2:29 PM, Gary Gregory <
> garydgreg...@gmail.com
> > > >> >wrote:
> > > >>
> > > >> >
> > > >> >
> > > >> >
> > > >> > Sent via the Samsung Galaxy NoteĀ® 3, an AT&T 4G LTE smartphone
> > > >> >
> > > >> > -------- Original message --------
> > > >> > From: sebb
> > > >> > Date:10/22/2013 13:37 (GMT-05:00)
> > > >> > To: Commons Developers List
> > > >> > Subject: Re: [lang] ImmutablePair is final
> > > >> >
> > > >> > On 22 October 2013 18:33, Gary Gregory <garydgreg...@gmail.com>
> > > wrote:
> > > >> > > On Tue, Oct 22, 2013 at 1:22 PM, Paul Benedict <
> > > pbened...@apache.org>
> > > >> > wrote:
> > > >> > >
> > > >> > >> If you can subclass, the class will likely be mutable somehow
> > > >> (accessing
> > > >> > >> protected or package-private data?) -- even introducing new
> > > variables
> > > >> > >> exclusive to the subclass. The "final" keyword is used well
> here.
> > > >> > >>
> > > >> > >
> > > >> > > Here is my use case for which I've cloned ImmutablePair into my
> own
> > > >> > package
> > > >> > > (yuck):
> > > >> >
> > > >> > Composition (rather than extension) would work better here surely?
> > > >> >
> > > >> > Surely indeed Shirley ;) (Airplane!)
> > > >> >
> > > >> > Right now I am getting hashCode and equals for free with the
> subclass
> > > >> > hack.
> > > >> >
> > > >> > Gary
> > > >> >
> > > >> >
> > > >> > > public final class ImmutableFooImmutableBarPair extends
> > > >> > > ImmutablePair<ImmutableFoo, ImmutableBar> {
> > > >> > >
> > > >> > >     private static final long serialVersionUID = 123L;
> > > >> > >
> > > >> > >     public ImmutableFooImmutableBarPair (final ImmutableFoo foo,
> > > final
> > > >> > > ImmutableBar bar) {
> > > >> > >         super(Validate.notNull(nameMatch),
> > > Validate.notNull(article));
> > > >> > >     }
> > > >> > >
> > > >> > >     public ImmutableFoo getImmutableFoo() {
> > > >> > >         return this.getValue();
> > > >> > >     }
> > > >> > >
> > > >> > >     public ImmutableBar getImmutableBar() {
> > > >> > >         return this.getKey();
> > > >> > >     }
> > > >> > > ...
> > > >> > > ImmutableFooImmutableBarPair pair = new
> > > >> > ImmutableFooImmutableBarPair(myFoo,
> > > >> > > myBar);
> > > >> > > ...
> > > >> > > pair.getImmutableFoo();
> > > >> > > ...
> > > >> > > pair.getImmutableBar();
> > > >> > >
> > > >> > > Gary
> > > >> > >
> > > >> > >
> > > >> > >>
> > > >> > >> On Tue, Oct 22, 2013 at 12:15 PM, sebb <seb...@gmail.com>
> wrote:
> > > >> > >>
> > > >> > >> > On 22 October 2013 18:10, Gary Gregory <
> garydgreg...@gmail.com
> >
> > > >> > wrote:
> > > >> > >> > > Hi All:
> > > >> > >> > >
> > > >> > >> > > Is there any reason we would want to keep ImmutablePair
> final?
> > > >> > >> >
> > > >> > >> > To stop mutable subclasses from being created?
> > > >> > >> >
> > > >> > >> > BTW, it's unfortunate that the fields are public; they should
> > > have
> > > >> > >> > been private (there are public getters).
> > > >> > >> >
> > > >> > >> > > Gary
> > > >> > >> > >
> > > >> > >> > > --
> > > >> > >> > > E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
> > > >> > >> > > Java Persistence with Hibernate, Second Edition<
> > > >> > >> > http://www.manning.com/bauer3/>
> > > >> > >> > > JUnit in Action, Second Edition <
> > > http://www.manning.com/tahchiev/
> > > >> >
> > > >> > >> > > Spring Batch in Action <http://www.manning.com/templier/>
> > > >> > >> > > Blog: http://garygregory.wordpress.com
> > > >> > >> > > Home: http://garygregory.com/
> > > >> > >> > > Tweet! http://twitter.com/GaryGregory
> > > >> > >> >
> > > >> > >> >
> > > >>
> ---------------------------------------------------------------------
> > > >> > >> > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> > > >> > >> > For additional commands, e-mail: dev-h...@commons.apache.org
> > > >> > >> >
> > > >> > >> >
> > > >> > >>
> > > >> > >>
> > > >> > >> --
> > > >> > >> Cheers,
> > > >> > >> Paul
> > > >> > >>
> > > >> > >
> > > >> > >
> > > >> > >
> > > >> > > --
> > > >> > > E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
> > > >> > > Java Persistence with Hibernate, Second Edition<
> > > >> > http://www.manning.com/bauer3/>
> > > >> > > JUnit in Action, Second Edition <
> http://www.manning.com/tahchiev/
> >
> > > >> > > Spring Batch in Action <http://www.manning.com/templier/>
> > > >> > > Blog: http://garygregory.wordpress.com
> > > >> > > Home: http://garygregory.com/
> > > >> > > Tweet! http://twitter.com/GaryGregory
> > > >> >
> > > >> >
> ---------------------------------------------------------------------
> > > >> > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> > > >> > For additional commands, e-mail: dev-h...@commons.apache.org
> > > >> >
> > > >> >
> > > >>
> > > >
> > > >
> > > >
> > > > --
> > > > E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
> > > > Java Persistence with Hibernate, Second Edition<
> > > http://www.manning.com/bauer3/>
> > > > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > > > Spring Batch in Action <http://www.manning.com/templier/>
> > > > Blog: http://garygregory.wordpress.com
> > > > Home: http://garygregory.com/
> > > > Tweet! http://twitter.com/GaryGregory
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> > > For additional commands, e-mail: dev-h...@commons.apache.org
> > >
> > >
> >
> >
> > --
> > E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
> > Java Persistence with Hibernate, Second Edition<
> http://www.manning.com/bauer3/>
> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > Spring Batch in Action <http://www.manning.com/templier/>
> > Blog: http://garygregory.wordpress.com
> > Home: http://garygregory.com/
> > Tweet! http://twitter.com/GaryGregory
>



-- 
Cheers,
Paul

Reply via email to