You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rgm <rg...@rgm.nu> on 2016/03/03 17:31:04 UTC

[lang] Proposal: Allow extending DiffResult

Ironically, it seems impossible to override DiffResult.toString(), which
one may wish to do in order to provide alternate formats for the
differences that are more compact than "lhs differs from rhs."

In my case, I wanted to support differences between Set<Integer> so that
one could get output like:
Object[groups:  added: {1, 2}, removed: {7},name:cat->dog]

Rather than:
Object[groups:[3,4,7],name=cat] differs from Object[groups:
[1,2,3,4],name=dog]

To support this I ended up wrapping the DiffResult class because I could
not extend it due to its private constructor.

re:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffResult.html

Re: [lang] Proposal: Allow extending DiffResult

Posted by rgm <rg...@rgm.nu>.
Thanks for the advice and your consideration, I have a better understanding
now. Cheers.
On Mar 5, 2016 2:06 AM, "Duncan Jones" <dj...@apache.org> wrote:

> On 3 March 2016 at 18:49, rgm <rg...@rgm.nu> wrote:
> > No, because ToStringStyle affects the rendering of only one of the
> object's
> > toString methods and cannot change the "left differs from right" output,
> > although it could change the rendering of either "left" or "right."  The
> > inclusion of two objects and the hard-coded string "differs from" are not
> > affected by ToStringStyle.
>
> I think your current approach sounds just fine. `toString()` isn't
> really intended for complex formatting tasks - the only reason I added
> a `ToStringStyle` override was to match the style of other classes in
> the builder package.
>
> Composition is also generally preferred to inheritance, so I would
> prefer not to make `DiffResult` extendable, but rather encourage you
> to make methods in your own classes that know how to format a
> `DiffResult` in the way you prefer.
>
> Duncan
>
> > On Thu, Mar 3, 2016 at 11:05 AM, Bhowmik, Bindul <
> bindulbhowmik@gmail.com>
> > wrote:
> >
> >> On Thu, Mar 3, 2016 at 9:31 AM, rgm <rg...@rgm.nu> wrote:
> >> > Ironically, it seems impossible to override DiffResult.toString(),
> which
> >> > one may wish to do in order to provide alternate formats for the
> >> > differences that are more compact than "lhs differs from rhs."
> >> >
> >> > In my case, I wanted to support differences between Set<Integer> so
> that
> >> > one could get output like:
> >> > Object[groups:  added: {1, 2}, removed: {7},name:cat->dog]
> >> >
> >> > Rather than:
> >> > Object[groups:[3,4,7],name=cat] differs from Object[groups:
> >> > [1,2,3,4],name=dog]
> >>
> >> Isn't this a use case for building your own ToStringStyle [1] and
> >> using the DiffResult#toString(ToStringStyle) method?
> >>
> >> >
> >> > To support this I ended up wrapping the DiffResult class because I
> could
> >> > not extend it due to its private constructor.
> >> >
> >> > re:
> >> >
> >>
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffResult.html
> >>
> >> - Bindul
> >>
> >> [1]
> >>
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html
> >>
> >> ---------------------------------------------------------------------
> >> 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
>
>

Re: [lang] Proposal: Allow extending DiffResult

Posted by Duncan Jones <dj...@apache.org>.
On 3 March 2016 at 18:49, rgm <rg...@rgm.nu> wrote:
> No, because ToStringStyle affects the rendering of only one of the object's
> toString methods and cannot change the "left differs from right" output,
> although it could change the rendering of either "left" or "right."  The
> inclusion of two objects and the hard-coded string "differs from" are not
> affected by ToStringStyle.

I think your current approach sounds just fine. `toString()` isn't
really intended for complex formatting tasks - the only reason I added
a `ToStringStyle` override was to match the style of other classes in
the builder package.

Composition is also generally preferred to inheritance, so I would
prefer not to make `DiffResult` extendable, but rather encourage you
to make methods in your own classes that know how to format a
`DiffResult` in the way you prefer.

Duncan

> On Thu, Mar 3, 2016 at 11:05 AM, Bhowmik, Bindul <bi...@gmail.com>
> wrote:
>
>> On Thu, Mar 3, 2016 at 9:31 AM, rgm <rg...@rgm.nu> wrote:
>> > Ironically, it seems impossible to override DiffResult.toString(), which
>> > one may wish to do in order to provide alternate formats for the
>> > differences that are more compact than "lhs differs from rhs."
>> >
>> > In my case, I wanted to support differences between Set<Integer> so that
>> > one could get output like:
>> > Object[groups:  added: {1, 2}, removed: {7},name:cat->dog]
>> >
>> > Rather than:
>> > Object[groups:[3,4,7],name=cat] differs from Object[groups:
>> > [1,2,3,4],name=dog]
>>
>> Isn't this a use case for building your own ToStringStyle [1] and
>> using the DiffResult#toString(ToStringStyle) method?
>>
>> >
>> > To support this I ended up wrapping the DiffResult class because I could
>> > not extend it due to its private constructor.
>> >
>> > re:
>> >
>> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffResult.html
>>
>> - Bindul
>>
>> [1]
>> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html
>>
>> ---------------------------------------------------------------------
>> 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


Re: [lang] Proposal: Allow extending DiffResult

Posted by rgm <rg...@rgm.nu>.
No, because ToStringStyle affects the rendering of only one of the object's
toString methods and cannot change the "left differs from right" output,
although it could change the rendering of either "left" or "right."  The
inclusion of two objects and the hard-coded string "differs from" are not
affected by ToStringStyle.

On Thu, Mar 3, 2016 at 11:05 AM, Bhowmik, Bindul <bi...@gmail.com>
wrote:

> On Thu, Mar 3, 2016 at 9:31 AM, rgm <rg...@rgm.nu> wrote:
> > Ironically, it seems impossible to override DiffResult.toString(), which
> > one may wish to do in order to provide alternate formats for the
> > differences that are more compact than "lhs differs from rhs."
> >
> > In my case, I wanted to support differences between Set<Integer> so that
> > one could get output like:
> > Object[groups:  added: {1, 2}, removed: {7},name:cat->dog]
> >
> > Rather than:
> > Object[groups:[3,4,7],name=cat] differs from Object[groups:
> > [1,2,3,4],name=dog]
>
> Isn't this a use case for building your own ToStringStyle [1] and
> using the DiffResult#toString(ToStringStyle) method?
>
> >
> > To support this I ended up wrapping the DiffResult class because I could
> > not extend it due to its private constructor.
> >
> > re:
> >
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffResult.html
>
> - Bindul
>
> [1]
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [lang] Proposal: Allow extending DiffResult

Posted by "Bhowmik, Bindul" <bi...@gmail.com>.
On Thu, Mar 3, 2016 at 9:31 AM, rgm <rg...@rgm.nu> wrote:
> Ironically, it seems impossible to override DiffResult.toString(), which
> one may wish to do in order to provide alternate formats for the
> differences that are more compact than "lhs differs from rhs."
>
> In my case, I wanted to support differences between Set<Integer> so that
> one could get output like:
> Object[groups:  added: {1, 2}, removed: {7},name:cat->dog]
>
> Rather than:
> Object[groups:[3,4,7],name=cat] differs from Object[groups:
> [1,2,3,4],name=dog]

Isn't this a use case for building your own ToStringStyle [1] and
using the DiffResult#toString(ToStringStyle) method?

>
> To support this I ended up wrapping the DiffResult class because I could
> not extend it due to its private constructor.
>
> re:
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffResult.html

- Bindul

[1] https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html

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