Interface CustomValueComparator<T>

Type Parameters:
T - Value Type
All Known Subinterfaces:
CustomPropertyComparator<T,C>, CustomValueToStringTemplate<T>
All Known Implementing Classes:
BigDecimalComparatorWithFixedEquals, CustomBigDecimalComparator, NullAsBlankStringComparator

public interface CustomValueComparator<T>
A custom comparator for ValueType classes to be used instead of default Object.equals(Object).

Example implementation: CustomBigDecimalComparator

Usage:
 JaversBuilder.javers()
              .registerValue(BigDecimal.class, new CustomBigDecimalComparator(2))
              .build()
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(T a, T b)
    Called by Javers to compare two Values.
    default boolean
    This flag is used to indicate to Javers whether a comparator implementation wants to handle nulls.
    toString(T value)
    This method has two roles.
  • Method Details

    • equals

      boolean equals(T a, T b)
      Called by Javers to compare two Values.
      Parameters:
      a - not null if handlesNulls() returns false
      b - not null if handlesNulls() returns false
    • toString

      String toString(T value)
      This method has two roles. First, it is used when Values are compared in hashing contexts. Second, it is used to build Entity Ids from Values.

      Hashcode role

      When a Value class has custom toString(), it is used instead of Object.hashCode() when comparing Values in hashing contexts, so: Custom toString() implementation should be aligned with custom equals(Object, Object) in the same way like Object.hashCode() should be aligned with Object.equals(Object).

      Entity Id role

      Each Value can serve as an Entity Id.
      When a Value has custom toString() function, it is used for creating InstanceId for Entities. If a Value doesn't have a custom toString() , default ReflectionUtil.reflectiveToString(Object)) is used.

      See full example CustomToStringExample.groovy.
      Parameters:
      value - not null if handlesNulls() returns false
    • handlesNulls

      default boolean handlesNulls()
      This flag is used to indicate to Javers whether a comparator implementation wants to handle nulls.

      By default, the flag is false and Javers checks if both values are non-null before calling a comparator.
      If any of given values is null — Javers compares them using the standard Java logic:
      • null == null
      • null != non-null

      If the flag is true — Javers skips that logic and allows a comparator to handle nulls on its own. In that case, a comparator holds responsibility for null-safety.
      See Also: