Skip to content
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

Not able to use CustomComparator for a object in list #492

Closed
skarian92 opened this issue Jan 18, 2017 · 3 comments
Closed

Not able to use CustomComparator for a object in list #492

skarian92 opened this issue Jan 18, 2017 · 3 comments
Assignees
Labels

Comments

@skarian92
Copy link

Hi

I want to compare two objects(of type Sample2) which have one list member of a custom object(of type sample). I wrote custom comparator for Sample class but javers code never hits the custom comparator.

For eg:

public class Sample {

  private String one;

  private final int two;

  public Sample(String one, int two) {
    this.one = one;
    this.two = two;
  }
}
public class Sample2 {
  String id;
  
private List sample;

  public Sample2(String id, List sample) {
    this.id = id;
    this.sample = sample;
  }
}

CustomComparator:

public class CustomSampleComparator implements CustomPropertyComparator {

  public ValueChange compare(Sample left, Sample right, GlobalId affectedId, Property property) {
    System.out.println("Comparator called...");
    return new ValueChange(affectedId, "testing", 3, 6);
  }

}

Main class:

public class App {
  public static void main(String[] args) {

    Sample type1 = new Sample("34", 54);
    Sample type2 = new Sample("55", 89);

    List samples1 = new ArrayList();
    samples1.add(type1);
    samples1.add(type2);

    Sample type3 = new Sample("34", 4);
    Sample type4 = new Sample("55", 9);

    List samples2 = new ArrayList();
    samples2.add(type3);
    samples2.add(type4);

    Sample2 sam = new Sample2("1", samples1);
    Sample2 sam2 = new Sample2("1", samples2);

    Javers javers =
        JaversBuilder.javers().registerCustomComparator(new CustomSampleComparator(), Sample.class)
            .build();

    Diff diff = javers.compare(sam, sam2);
    System.out.println(diff.prettyPrint());
    System.out.println("Hello World!");
  }
}

Problem: My custom comparator is never bieng hit.

@bartoszwalacik
Copy link
Member

For now, CustomPropertyComparator compares only property values and not items inside collections. We need to check if it possible to use it as a collections Comparator.

@akrystian akrystian self-assigned this Jan 26, 2017
@akrystian akrystian removed their assignment Feb 10, 2017
@pszymczyk pszymczyk self-assigned this Jun 4, 2017
@bartoszwalacik
Copy link
Member

fixed in release 3.3.0, see https://javers.org/release-notes/

@bartoszwalacik
Copy link
Member

for example :

 javersBuilder.registerValue(BigDecimal.class, (a,b) -> a.compareTo(b) == 0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants