Package org.javers.core.diff.custom
Class NullAsBlankStringComparator
- java.lang.Object
-
- org.javers.core.diff.custom.NullAsBlankStringComparator
-
- All Implemented Interfaces:
CustomValueComparator<java.lang.String>
public class NullAsBlankStringComparator extends java.lang.Object implements CustomValueComparator<java.lang.String>
Compares Strings treating blank and null strings as equal.
Usage example:JaversBuilder.javers() .registerValue(String.class, new NullAsBlankStringComparator()) .build();
-
-
Constructor Summary
Constructors Constructor Description NullAsBlankStringComparator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.String a, java.lang.String b)Called by Javers to compare two Values.booleanhandlesNulls()This flag is used to indicate to Javers whether a comparator implementation wants to handle nulls.java.lang.StringtoString(java.lang.String value)This method has two roles.
-
-
-
Method Detail
-
equals
public boolean equals(java.lang.String a, java.lang.String b)Description copied from interface:CustomValueComparatorCalled by Javers to compare two Values.- Specified by:
equalsin interfaceCustomValueComparator<java.lang.String>- Parameters:
a- not null ifCustomValueComparator.handlesNulls()returns falseb- not null ifCustomValueComparator.handlesNulls()returns false
-
toString
public java.lang.String toString(java.lang.String value)
Description copied from interface:CustomValueComparatorThis 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 customtoString(), it is used instead ofObject.hashCode()when comparing Values in hashing contexts, so:- Sets with Values
- Lists with Values compared as
ListCompareAlgorithm.AS_SET - Maps with Values as keys
toString()implementation should be aligned with customCustomValueComparator.equals(Object, Object)in the same way likeObject.hashCode()should be aligned withObject.equals(Object).
Entity Id role
Each Value can serve as an Entity Id.
When a Value has customtoString()function, it is used for creatingInstanceIdfor Entities. If a Value doesn't have a customtoString(), defaultReflectionUtil.reflectiveToString(Object)) is used.
See full example CustomToStringExample.groovy.- Specified by:
toStringin interfaceCustomValueComparator<java.lang.String>- Parameters:
value- not null ifCustomValueComparator.handlesNulls()returns false
-
handlesNulls
public boolean handlesNulls()
Description copied from interface:CustomValueComparatorThis 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.- Specified by:
handlesNullsin interfaceCustomValueComparator<java.lang.String>- See Also:
NullAsBlankStringComparator
-
-