commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: [lang] ImmutablePair is final
Date Tue, 22 Oct 2013 17:34:44 GMT
On 22 October 2013 18:21, Matt Benson <gudnabrsam@gmail.com> wrote:
> Here's the thread where the issue was discussed before:
>
> http://markmail.org/thread/fcbesctp6ifxtecs
>
> As for the public fields, it was originally my intent that any Pair be
> immutable, and my take on final fields is that there is no reason not to
> make them public, particularly if, as in the case of an open ended utility
> class like Pair, it yields more compact code (something I personally strive
> for).  In practice, I rarely program to the ImmutablePair type anymore
> since Pair.of() looks clearer, but is declared as returning Pair.  The
> public fields, however, remain.  :)

Using a getter allows more control over access, if it should be
required in future implementations.

If the object itself is not immutable (e.g. an array) then the pair is
not and cannot be made immutable.
Nor can it even be made thread-safe.

Whereas if a getter is used, potentially there is scope for adding
synchronisation.
Also it would allow one to add access logging.

It's not a big deal, but public fields restrict the API.

Using the getter rather than the field name adds a 5 characters to the
source code.
Not a lot, considering the trade-off in flexibility.
[One can ignore the performance aspect, as the getter will be optimised away]

> Matt
>
>
> 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
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message