commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Re: [lang] ImmutablePair is final
Date Tue, 22 Oct 2013 19:29:01 GMT



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 <garydgregory@gmail.com> wrote:
> On Tue, Oct 22, 2013 at 1:22 PM, Paul Benedict <pbenedict@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 <sebbaz@gmail.com> wrote:
>>
>> > On 22 October 2013 18:10, Gary Gregory <garydgregory@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: garydgregory@gmail.com | ggregory@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-unsubscribe@commons.apache.org
>> > For additional commands, e-mail: dev-help@commons.apache.org
>> >
>> >
>>
>>
>> --
>> Cheers,
>> Paul
>>
>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@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-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message