- Notifications
You must be signed in to change notification settings - 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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
1d0b4c4
adding ability to compare by the result of a given method on a proper…
2271bf3
Signed-off-by: unknown <jonathan.lee@WKS-E32.hq.vistair.com>
3eb2705
removing unused imports. got test coverage.
151d38d
renaming methods, annotations and properties following discussion on …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions 15 src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions 77 src/main/java/de/danielbechler/diff/example/MethodEqualExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
| ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions 16 src/main/java/de/danielbechler/diff/path/ClassAndMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
isWithMethodEquals
sounds 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" notifications@github.com 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
isWithMethodResultEqual
though. 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" notifications@github.com 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 usingequalsOnlyValueProviderMethod
in 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" notifications@github.com wrote: