Using CustomPropertyComparator with java.util.List
up vote
0
down vote
favorite
I've created an CustomPropertyComparator to use it to compare Objects which where created by jaxb. So in my case i am not able to use the @DiffIgnore annotation offered by the framework. Also the Collections genereted by jaxb are based on java.util.List and not java.util.Collection. Unfortunatly i couldn't manage to make javers use my CustomPropertyComparator with the List-Interface.
public class Person {
private String name;
private String ignoreThis;
}
public class Company {
private String id;
private Person owner;
private Collection<Person> clients;
private List<Person> partners;
}
Comparator that only compares the name, but ignores the field 'ignoreThis'
public class EntityComparator implements CustomPropertyComparator<Person, ValueChange> {
public ValueChange compare(Person left, Person right, GlobalId affectedId, Property property) {
if (left.getName().equals(right.getName()))
return null;
return new ValueChange(affectedId, "entity/name", left.getName(), right.getName());
}
}
My test-cases looks like this:
This tests works, cause it compares the collection
@Test
public void equalEntityClientTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", null, Arrays.asList(e1), null);
Person e2 = new Person("james", "");
Company le2 = new Company("1", null, Arrays.asList(e2), null);
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
This tests fails, cause it doesn't use my comparator to compare the entity and the diff of the ignored field is true.
@Test
public void equalEntityPartnerTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", e1, null, Arrays.asList(e1));
Person e2 = new Person("james", "");
Company le2 = new Company("1", e2, null, Arrays.asList(e2));
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
In the reference of javers they explain if you have a custom Collection-Interface you need to implement your own comparator, which is okay if you use a collection not based on java.util.Collection. But actually i would expect that java.util.List is supported by the javers Library.
Also i wasn't able to figure out how i can add/create a Comparator for the List-Interface.
Working example can be found under: https://github.com/baumgartner/javerstest
java collections javers
add a comment |
up vote
0
down vote
favorite
I've created an CustomPropertyComparator to use it to compare Objects which where created by jaxb. So in my case i am not able to use the @DiffIgnore annotation offered by the framework. Also the Collections genereted by jaxb are based on java.util.List and not java.util.Collection. Unfortunatly i couldn't manage to make javers use my CustomPropertyComparator with the List-Interface.
public class Person {
private String name;
private String ignoreThis;
}
public class Company {
private String id;
private Person owner;
private Collection<Person> clients;
private List<Person> partners;
}
Comparator that only compares the name, but ignores the field 'ignoreThis'
public class EntityComparator implements CustomPropertyComparator<Person, ValueChange> {
public ValueChange compare(Person left, Person right, GlobalId affectedId, Property property) {
if (left.getName().equals(right.getName()))
return null;
return new ValueChange(affectedId, "entity/name", left.getName(), right.getName());
}
}
My test-cases looks like this:
This tests works, cause it compares the collection
@Test
public void equalEntityClientTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", null, Arrays.asList(e1), null);
Person e2 = new Person("james", "");
Company le2 = new Company("1", null, Arrays.asList(e2), null);
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
This tests fails, cause it doesn't use my comparator to compare the entity and the diff of the ignored field is true.
@Test
public void equalEntityPartnerTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", e1, null, Arrays.asList(e1));
Person e2 = new Person("james", "");
Company le2 = new Company("1", e2, null, Arrays.asList(e2));
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
In the reference of javers they explain if you have a custom Collection-Interface you need to implement your own comparator, which is okay if you use a collection not based on java.util.Collection. But actually i would expect that java.util.List is supported by the javers Library.
Also i wasn't able to figure out how i can add/create a Comparator for the List-Interface.
Working example can be found under: https://github.com/baumgartner/javerstest
java collections javers
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've created an CustomPropertyComparator to use it to compare Objects which where created by jaxb. So in my case i am not able to use the @DiffIgnore annotation offered by the framework. Also the Collections genereted by jaxb are based on java.util.List and not java.util.Collection. Unfortunatly i couldn't manage to make javers use my CustomPropertyComparator with the List-Interface.
public class Person {
private String name;
private String ignoreThis;
}
public class Company {
private String id;
private Person owner;
private Collection<Person> clients;
private List<Person> partners;
}
Comparator that only compares the name, but ignores the field 'ignoreThis'
public class EntityComparator implements CustomPropertyComparator<Person, ValueChange> {
public ValueChange compare(Person left, Person right, GlobalId affectedId, Property property) {
if (left.getName().equals(right.getName()))
return null;
return new ValueChange(affectedId, "entity/name", left.getName(), right.getName());
}
}
My test-cases looks like this:
This tests works, cause it compares the collection
@Test
public void equalEntityClientTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", null, Arrays.asList(e1), null);
Person e2 = new Person("james", "");
Company le2 = new Company("1", null, Arrays.asList(e2), null);
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
This tests fails, cause it doesn't use my comparator to compare the entity and the diff of the ignored field is true.
@Test
public void equalEntityPartnerTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", e1, null, Arrays.asList(e1));
Person e2 = new Person("james", "");
Company le2 = new Company("1", e2, null, Arrays.asList(e2));
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
In the reference of javers they explain if you have a custom Collection-Interface you need to implement your own comparator, which is okay if you use a collection not based on java.util.Collection. But actually i would expect that java.util.List is supported by the javers Library.
Also i wasn't able to figure out how i can add/create a Comparator for the List-Interface.
Working example can be found under: https://github.com/baumgartner/javerstest
java collections javers
I've created an CustomPropertyComparator to use it to compare Objects which where created by jaxb. So in my case i am not able to use the @DiffIgnore annotation offered by the framework. Also the Collections genereted by jaxb are based on java.util.List and not java.util.Collection. Unfortunatly i couldn't manage to make javers use my CustomPropertyComparator with the List-Interface.
public class Person {
private String name;
private String ignoreThis;
}
public class Company {
private String id;
private Person owner;
private Collection<Person> clients;
private List<Person> partners;
}
Comparator that only compares the name, but ignores the field 'ignoreThis'
public class EntityComparator implements CustomPropertyComparator<Person, ValueChange> {
public ValueChange compare(Person left, Person right, GlobalId affectedId, Property property) {
if (left.getName().equals(right.getName()))
return null;
return new ValueChange(affectedId, "entity/name", left.getName(), right.getName());
}
}
My test-cases looks like this:
This tests works, cause it compares the collection
@Test
public void equalEntityClientTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", null, Arrays.asList(e1), null);
Person e2 = new Person("james", "");
Company le2 = new Company("1", null, Arrays.asList(e2), null);
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
This tests fails, cause it doesn't use my comparator to compare the entity and the diff of the ignored field is true.
@Test
public void equalEntityPartnerTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", e1, null, Arrays.asList(e1));
Person e2 = new Person("james", "");
Company le2 = new Company("1", e2, null, Arrays.asList(e2));
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
In the reference of javers they explain if you have a custom Collection-Interface you need to implement your own comparator, which is okay if you use a collection not based on java.util.Collection. But actually i would expect that java.util.List is supported by the javers Library.
Also i wasn't able to figure out how i can add/create a Comparator for the List-Interface.
Working example can be found under: https://github.com/baumgartner/javerstest
java collections javers
java collections javers
edited Nov 22 at 11:54
asked Nov 21 at 18:31
Martin Baumgartner
2,1281224
2,1281224
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You have touched two issues here.
Your first test passes because Javers ignores Collection
properties and only this warning is logged:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
I think it's not OK, so I have created an issue, see https://github.com/javers/javers/issues/746
Second issue concerns CustomPropertyComparator
. It was designed to compare large structures like Multimap, and it's not called by JaVers when comparing List items, because when you are comparing List items you need boolean equals(a,b)
method. That's what CustomValueComparator
does.
Although, CustomPropertyComparator can be extended to have also boolean equals(a,b)
method. I have created the second issue for that, see https://github.com/javers/javers/issues/747
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You have touched two issues here.
Your first test passes because Javers ignores Collection
properties and only this warning is logged:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
I think it's not OK, so I have created an issue, see https://github.com/javers/javers/issues/746
Second issue concerns CustomPropertyComparator
. It was designed to compare large structures like Multimap, and it's not called by JaVers when comparing List items, because when you are comparing List items you need boolean equals(a,b)
method. That's what CustomValueComparator
does.
Although, CustomPropertyComparator can be extended to have also boolean equals(a,b)
method. I have created the second issue for that, see https://github.com/javers/javers/issues/747
add a comment |
up vote
1
down vote
accepted
You have touched two issues here.
Your first test passes because Javers ignores Collection
properties and only this warning is logged:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
I think it's not OK, so I have created an issue, see https://github.com/javers/javers/issues/746
Second issue concerns CustomPropertyComparator
. It was designed to compare large structures like Multimap, and it's not called by JaVers when comparing List items, because when you are comparing List items you need boolean equals(a,b)
method. That's what CustomValueComparator
does.
Although, CustomPropertyComparator can be extended to have also boolean equals(a,b)
method. I have created the second issue for that, see https://github.com/javers/javers/issues/747
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You have touched two issues here.
Your first test passes because Javers ignores Collection
properties and only this warning is logged:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
I think it's not OK, so I have created an issue, see https://github.com/javers/javers/issues/746
Second issue concerns CustomPropertyComparator
. It was designed to compare large structures like Multimap, and it's not called by JaVers when comparing List items, because when you are comparing List items you need boolean equals(a,b)
method. That's what CustomValueComparator
does.
Although, CustomPropertyComparator can be extended to have also boolean equals(a,b)
method. I have created the second issue for that, see https://github.com/javers/javers/issues/747
You have touched two issues here.
Your first test passes because Javers ignores Collection
properties and only this warning is logged:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
I think it's not OK, so I have created an issue, see https://github.com/javers/javers/issues/746
Second issue concerns CustomPropertyComparator
. It was designed to compare large structures like Multimap, and it's not called by JaVers when comparing List items, because when you are comparing List items you need boolean equals(a,b)
method. That's what CustomValueComparator
does.
Although, CustomPropertyComparator can be extended to have also boolean equals(a,b)
method. I have created the second issue for that, see https://github.com/javers/javers/issues/747
answered Nov 25 at 10:20
Bartek Walacik
1,7241512
1,7241512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418466%2fusing-custompropertycomparator-with-java-util-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown