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










share|improve this question




























    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










    share|improve this question


























      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










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 11:54

























      asked Nov 21 at 18:31









      Martin Baumgartner

      2,1281224




      2,1281224
























          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






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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






            share|improve this answer

























              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






              share|improve this answer























                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






                share|improve this answer












                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 at 10:20









                Bartek Walacik

                1,7241512




                1,7241512






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)