-
Couldn't load subscription status.
- Fork 179
ability to compare a bean or map or collection by the result of a method call #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1d0b4c4
2271bf3
3eb2705
151d38d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,17 @@ else if (nodeInspector.isEqualsOnly(collectionNode)) | |
| collectionNode.setState(Node.State.CHANGED); | ||
| } | ||
| } | ||
| else if (nodeInspector.isWithMethodEquals(collectionNode)){ | ||
| String method = nodeInspector.getWithMethodEqualsMethod(collectionNode); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd generally prefer |
||
| if (collectionInstances.areMethodResultEqual(method)) | ||
| { | ||
| collectionNode.setState(Node.State.UNTOUCHED); | ||
| } | ||
| else | ||
| { | ||
| collectionNode.setState(Node.State.CHANGED); | ||
| } | ||
| } | ||
| else if (collectionInstances.hasBeenAdded()) | ||
| { | ||
| compareItems(collectionNode, collectionInstances, collectionInstances.getWorking(Collection.class)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package de.danielbechler.diff.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| @Inherited | ||
| @ObjectDiffAnnotation | ||
| public @interface ObjectDiffMethodEqualsType { | ||
| public String method(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package de.danielbechler.diff.example; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import de.danielbechler.diff.Configuration; | ||
| import de.danielbechler.diff.ObjectDifferFactory; | ||
| import de.danielbechler.diff.annotation.ObjectDiffMethodEqualsType; | ||
| import de.danielbechler.diff.annotation.ObjectDiffProperty; | ||
| import de.danielbechler.diff.node.Node; | ||
| import de.danielbechler.diff.path.PropertyPath; | ||
| import de.danielbechler.diff.visitor.PrintingVisitor; | ||
|
|
||
| class MethodEqualExample { | ||
| private MethodEqualExample() | ||
| { | ||
| } | ||
|
|
||
| public static void main(final String[] args) | ||
| { | ||
| PropertyClass prop = new PropertyClass("1", "2"); | ||
| final EncompassingClass base = new EncompassingClass(prop); | ||
| PropertyClass prop2 = new PropertyClass("1", "3"); | ||
| final EncompassingClass working = new EncompassingClass(prop2); | ||
|
|
||
| final Configuration configuration = new Configuration(); | ||
|
|
||
| // (Option 1) Causes the ObjectDiffer to compare using the method "getProp1" on the 'prop' property of the root object | ||
| configuration.withMethodEqualsProperty(PropertyPath.buildWith("prop"), "getProp1"); | ||
|
|
||
| final Node node = ObjectDifferFactory.getInstance(configuration).compare(working, base); | ||
|
|
||
| node.visit(new PrintingVisitor(working, base)); | ||
|
|
||
| // Output with ignore: | ||
| // Property at path '/' has not changed | ||
| // Output without ignore: | ||
| // Property at path '/prop/prop2' has changed from [ 2 ] to [ 3 ] | ||
| } | ||
|
|
||
| public static class EncompassingClass | ||
| { | ||
| private final PropertyClass prop; | ||
|
|
||
| public EncompassingClass(final PropertyClass prop) | ||
| { | ||
| this.prop = prop; | ||
| } | ||
|
|
||
| /* (Option 2) This annotation causes the ObjectDiffer to use getProp1 method to compare */ | ||
| //@ObjectDiffProperty(methodEqual = "getProp1") | ||
| public PropertyClass getProp() { | ||
| return prop; | ||
| } | ||
| } | ||
|
|
||
| /* (Option 3) This annotation causes the ObjectDiffer to use getProp1 method to compare */ | ||
| //@ObjectDiffMethodEqualsType(method="getProp1") | ||
| public static class PropertyClass | ||
| { | ||
| private String prop1; | ||
| private String prop2; | ||
|
|
||
| public PropertyClass(String prop1, String prop2) | ||
| { | ||
| this.prop1 = prop1; | ||
| this.prop2 = prop2; | ||
| } | ||
| public String getProp1() { | ||
| return prop1; | ||
| } | ||
| public String getProp2() { | ||
| return prop2; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package de.danielbechler.diff.path; | ||
|
|
||
| public class ClassAndMethod { | ||
| private Class<?> clazz; | ||
| private String method; | ||
| public ClassAndMethod(Class<?> clazz, String method){ | ||
| this.clazz = clazz; | ||
| this.method = method; | ||
| } | ||
| public Class<?> getClazz() { | ||
| return clazz; | ||
| } | ||
| public String getMethod() { | ||
| return method; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isWithMethodEqualssounds a little strange. How abouthasAlternativeEqualsMethod?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is checking equality of the result of the method, so
"isWithMethodResultEqual" is more explicit, if a little verbose.
Your call, I suppose!
On 25 Jul 2013 21:10, "Daniel Bechler" [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, now I see. I'm still not sold on
isWithMethodResultEqualthough. Even with the context I had, I failed to see what it does by just reading the name. But I think we should rather look for a better name in the Configuration, as it will dictate the names in the remaining places.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
How about "isComparedByMethodResult"?
I think this is a little clearer. I will let you know if I think of
something better!
On 25 Jul 2013 21:38, "Daniel Bechler" [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better, but still not as descriptive, as I'd like. How about
hasEqualsOnlyValueProviderMethod, given we're usingequalsOnlyValueProviderMethodin theConfiguration?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea cool, is more specific. Gotta stay consistent too. Sounds good.
On 25 Jul 2013 21:56, "Daniel Bechler" [email protected] wrote: