[Lang] Advice on "Diff" usage
Hi. In the Javadoc of the "DiffBuilder" class[1]: ---CUT--- [...] To use this class, write code as follows: public class Person implements Diffable { String name; int age; boolean smoker; [...] ---CUT--- However, a common use-case would be that we can't directly modify the class whose instances we want to compare. Assuming that interfaces are given and non-modifiable (i.e. they cannot implement "Diffable"): ---CUT--- public interface Content { double getValue(); } public interface Container { Content[] getContentArray(); } ---CUT--- and the modifiable user code is similar to ---CUT--- public class MyContent implements Content { private final double c; public MyContent(double input) { c = input: } @Override public double getValue() { return c; } } public class MyContainer implements Container { private final Content[] contentArr; MyContainer(double ... inputs) { final int len = inputs.length; contentArr = new Content[len]; for (int i = 0; i < len; i++) { contentArr[i] = new MyContent(inputs[i]); } } @Override public Content[] getContentArray() { return contentArr; } } ---CUT--- how should I go about in order to get the "diff" between instances of "MyContainer"? Thanks, Gilles [1] https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/builder/DiffBuilder.html - To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org
Re: [Lang] Advice on "Diff" usage
How about the reflection variant? You won't need to edit any of the target code. Gary On Sun, Jul 9, 2023, 09:51 Gilles Sadowski wrote: > Hi. > > In the Javadoc of the "DiffBuilder" class[1]: > ---CUT--- > [...] > To use this class, write code as follows: > > public class Person implements Diffable { >String name; >int age; >boolean smoker; > [...] > ---CUT--- > > However, a common use-case would be that we can't directly > modify the class whose instances we want to compare. > Assuming that interfaces are given and non-modifiable (i.e. > they cannot implement "Diffable"): > ---CUT--- > public interface Content { > double getValue(); > } > > public interface Container { > Content[] getContentArray(); > } > ---CUT--- > and the modifiable user code is similar to > ---CUT--- > public class MyContent implements Content { > private final double c; > > public MyContent(double input) { > c = input: > } > > @Override > public double getValue() { > return c; > } > } > > public class MyContainer implements Container { > private final Content[] contentArr; > > MyContainer(double ... inputs) { > final int len = inputs.length; > contentArr = new Content[len]; > for (int i = 0; i < len; i++) { > contentArr[i] = new MyContent(inputs[i]); > } > } > > @Override > public Content[] getContentArray() { > return contentArr; > } > } > ---CUT--- > how should I go about in order to get the "diff" between instances > of "MyContainer"? > > Thanks, > Gilles > > [1] > https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/builder/DiffBuilder.html > > - > To unsubscribe, e-mail: user-unsubscr...@commons.apache.org > For additional commands, e-mail: user-h...@commons.apache.org > >
Re: [Lang] Advice on "Diff" usage
Le dim. 9 juil. 2023 à 19:32, Gary Gregory a écrit : > > How about the reflection variant? You won't need to edit any of the target > code. Thanks, I missed it. [Maybe worth adding a "@see ReflectionDiffBuilder" tag in "DiffBuilder" (and vice-versa) ?] However, the output is much too verbose when the "Container" is a large object. For example, in my case, the number of reported differences is 11 but the "toString()" outputs a line that is 371397 characters long... Is there a way to only print the (primitive) fields for which the values differ? [It seems (?) that if, deep down the composite "Container", something is different then all the enclosing instances are considered different (it's true) and printed in full.] Also, the "MULTI_LINE_STYLE" is somewhat inconsistent: It does indeed print each field of the top-level object on a new line, but it does not apply this layout to the "composing" objects: Referring to the example quoted below, the array "contentArr" would be printed on a single line (in my use case, that line is 183312 characters long).] For large objects, this makes it quite difficult to spot what differences were actually detected... Regards, Gilles > > Gary > > > On Sun, Jul 9, 2023, 09:51 Gilles Sadowski wrote: > > > Hi. > > > > In the Javadoc of the "DiffBuilder" class[1]: > > ---CUT--- > > [...] > > To use this class, write code as follows: > > > > public class Person implements Diffable { > >String name; > >int age; > >boolean smoker; > > [...] > > ---CUT--- > > > > However, a common use-case would be that we can't directly > > modify the class whose instances we want to compare. > > Assuming that interfaces are given and non-modifiable (i.e. > > they cannot implement "Diffable"): > > ---CUT--- > > public interface Content { > > double getValue(); > > } > > > > public interface Container { > > Content[] getContentArray(); > > } > > ---CUT--- > > and the modifiable user code is similar to > > ---CUT--- > > public class MyContent implements Content { > > private final double c; > > > > public MyContent(double input) { > > c = input: > > } > > > > @Override > > public double getValue() { > > return c; > > } > > } > > > > public class MyContainer implements Container { > > private final Content[] contentArr; > > > > MyContainer(double ... inputs) { > > final int len = inputs.length; > > contentArr = new Content[len]; > > for (int i = 0; i < len; i++) { > > contentArr[i] = new MyContent(inputs[i]); > > } > > } > > > > @Override > > public Content[] getContentArray() { > > return contentArr; > > } > > } > > ---CUT--- > > how should I go about in order to get the "diff" between instances > > of "MyContainer"? > > > > Thanks, > > Gilles > > > > [1] > > https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/builder/DiffBuilder.html - To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org
Re: [Lang] Advice on "Diff" usage
I updated the Javadoc for both classes. Gary On Mon, Jul 10, 2023, 13:40 Gilles Sadowski wrote: > Le dim. 9 juil. 2023 à 19:32, Gary Gregory a > écrit : > > > > How about the reflection variant? You won't need to edit any of the > target > > code. > > Thanks, I missed it. [Maybe worth adding a "@see ReflectionDiffBuilder" > tag in "DiffBuilder" (and vice-versa) ?] > > However, the output is much too verbose when the "Container" is a > large object. For example, in my case, the number of reported > differences is > 11 > but the "toString()" outputs a line that is > 371397 > characters long... > Is there a way to only print the (primitive) fields for which the values > differ? [It seems (?) that if, deep down the composite "Container", > something is different then all the enclosing instances are considered > different (it's true) and printed in full.] > > Also, the "MULTI_LINE_STYLE" is somewhat inconsistent: It does > indeed print each field of the top-level object on a new line, but it > does not apply this layout to the "composing" objects: Referring to > the example quoted below, the array "contentArr" would be printed on > a single line (in my use case, that line is 183312 characters long).] > > For large objects, this makes it quite difficult to spot what differences > were actually detected... > > Regards, > Gilles > > > > > Gary > > > > > > On Sun, Jul 9, 2023, 09:51 Gilles Sadowski wrote: > > > > > Hi. > > > > > > In the Javadoc of the "DiffBuilder" class[1]: > > > ---CUT--- > > > [...] > > > To use this class, write code as follows: > > > > > > public class Person implements Diffable { > > >String name; > > >int age; > > >boolean smoker; > > > [...] > > > ---CUT--- > > > > > > However, a common use-case would be that we can't directly > > > modify the class whose instances we want to compare. > > > Assuming that interfaces are given and non-modifiable (i.e. > > > they cannot implement "Diffable"): > > > ---CUT--- > > > public interface Content { > > > double getValue(); > > > } > > > > > > public interface Container { > > > Content[] getContentArray(); > > > } > > > ---CUT--- > > > and the modifiable user code is similar to > > > ---CUT--- > > > public class MyContent implements Content { > > > private final double c; > > > > > > public MyContent(double input) { > > > c = input: > > > } > > > > > > @Override > > > public double getValue() { > > > return c; > > > } > > > } > > > > > > public class MyContainer implements Container { > > > private final Content[] contentArr; > > > > > > MyContainer(double ... inputs) { > > > final int len = inputs.length; > > > contentArr = new Content[len]; > > > for (int i = 0; i < len; i++) { > > > contentArr[i] = new MyContent(inputs[i]); > > > } > > > } > > > > > > @Override > > > public Content[] getContentArray() { > > > return contentArr; > > > } > > > } > > > ---CUT--- > > > how should I go about in order to get the "diff" between instances > > > of "MyContainer"? > > > > > > Thanks, > > > Gilles > > > > > > [1] > > > > https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/builder/DiffBuilder.html > > - > To unsubscribe, e-mail: user-unsubscr...@commons.apache.org > For additional commands, e-mail: user-h...@commons.apache.org > >
Re: [Lang] Advice on "Diff" usage
Le mer. 12 juil. 2023 à 00:13, Gary Gregory a écrit : > > I updated the Javadoc for both classes. Thanks. FTR, I've filed https://issues.apache.org/jira/projects/LANG/issues/LANG-1701 for the more important issue(s). Regards, Gilles >>> [...] - To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org