Skip to content

CustomPropertyComparator should be used compare Collection items #747

Closed

Description

@bartoszwalacik

Currently, CustomPropertyComparator is used only for comparing object properties, for example, it can be used to compare Person objects in boss property:

class Company {
        private Person boss
        private List<Person> partners
}

But it is not used by JaVers to compare Person objects when they are collection items in the partners List.

Possible solution is a new method in CustomPropertyComparator with default impl:

   /**
     * This comparator is called by JaVers to calculate collection-to-collection diff.
     * <br/>
     * For example, when objects of type T are List items.
     * <br/><br/>
     * 
     * Both equals() and compare() should return consistent results. When compare() returns null,
     * equals() should return false.
     * 
     */
    default boolean equals(T a, T b) {
        return Objects.equals(a, b);
    }

Activity

  1. bartoszwalacik commented on Nov 27, 2018

    @bartoszwalacik
    MemberAuthor

    released in 4.0.0-RC3

  2. baumgartner commented on Dec 3, 2018

    @baumgartner

    Works with ListCompareAlgorithm.SIMPLE
    Assert fails for ListCompareAlgorithm.AS_SET and LEVENSHTEIN_DISTANCE.
    Test-Project available at https://github.com/baumgartner/javerstest/blob/master/src/test/java/javers/test/ListCompareTest.java

  3. bartoszwalacik commented on Dec 3, 2018

    @bartoszwalacik
    MemberAuthor

    omg, so we need a fix ...

  4. bartoszwalacik commented on Dec 5, 2018

    @bartoszwalacik
    MemberAuthor

    Fixed in 5.0.1 when CustomPropertyComparator is used with LEVENSHTEIN.
    CustomPropertyComparator can't be used with AS_SET because for comparing Sets, we need both equals() and hashCode() and CustomPropertyComparator implements only custom equals()

  5. dmitry-weirdo commented on Nov 25, 2020

    @dmitry-weirdo

    @bartoszwalacik any perspective to also be able to use CustomPropertyComparator together with CustomPropertyComparator?

    I described my case on #1038.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions