Search code examples
javanullcomparisonjaversempty-list

Javers - Comparison ValueObjects which are null and object with empty list


I have such class:

import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class ValueObjectList {
    private List<DummyClass> dummyClassList;
}

And in main method 2 object of that class:

ValueObjectList valueObjectList1 = ValueObjectList.builder()
            .dummyClassList(new ArrayList<>())
            .build();

ValueObjectList valueObjectList2 = null;

I want to compare such object by Javers. What's the best way to treat these objects by javers as the same objects? Do I have to create my own comparator for this case? Maybe can I set such a configuration in JaversBuilder?


Solution

  • You can use Custom Types and CustomPropertyComparator. See the javadoc for CustomType:

    Custom Types are not easy to manage, use it as a last resort, only for corner cases like comparing custom Collection types.

    JaVers treats a Custom Type as a black box and doesn't take any assumptions about its content or behaviour. It's a "not modeled" type, somehow similar to unbounded wildcard <?>

    Objects of Custom Type are compared by a CustomPropertyComparator. Registering this comparator is the only way to map a Custom Type.

    Custom Types are serialized to JSON using Gson defaults.

    see JaversBuilder#registerCustomType(Class, CustomPropertyComparator)