Custom Comparators to reflect multiple changes as

2019-09-16 12:14发布

问题:

I am attempting to implement CustomComparators for certain classes. The first question I have is how to return back multiple ValueChanges in my CustomComparator. I seem to only be able to return back a single ValueChange on that object when more than one field might have changed. Additionally, I can't figure out how to get Javers to generate a diff for objects that appear as fields in the class my CustomCommparator is overriding. Example below:

I have the following classes defined below where we have a Person owning a Store that has an Item they sell there.

public class Person
   public Store store


public class Store
   public String name
   public Item item
   public String location

public class Item
  public String name

Question: 1. How can I implement a CustomComparator for Store such that it creates two ValueChanges: one for name and one for location. 2. How can I use that same CustomComparator to get Javers to also do a diff on item? Thanks!

回答1:

Shortly, you can't. The CustomPropertyComparator.compare() method returns PropertyChange which can be complex object (like ListChange) or any kind of object that extends PropertyChange. But still, PropertyChange describes a Change on exactly one Property.

Doing it javers-way means registering CustomValueComparator for each Value Type.

For example, if you have the Item class:

class Item {
    Price price
    Characteristics Characteristics 
}

Register custom comparators for each atomic Value Type:

javersBuilder
    .registerValue(Price.class, (a,b) -> a.compareTo(b) == 0)
    .registerValue(Characteristics.class, (a,b) -> a.compareTo(b) == 0)


标签: javers