@@ -77,8 +77,10 @@ public enum PrimitiveDefaultValueMode
7777 private final Collection <PropertyPath > includedProperties = new HashSet <PropertyPath >(10 );
7878 private final Collection <PropertyPath > excludedProperties = new HashSet <PropertyPath >(10 );
7979 private final Collection <PropertyPath > equalsOnlyProperties = new LinkedHashSet <PropertyPath >(10 );
80- private final Collection <Class <?>> compareToOnlyTypes = new LinkedHashSet <Class <?>>(10 );
80+ private final Collection <PropertyPathAndMethod > equalsOnlyValueProviderMethods = new LinkedHashSet <PropertyPathAndMethod >(10 );
81+ private final Collection <Class <?>> compareToOnlyTypes = new LinkedHashSet <Class <?>>(10 );
8182 private final Collection <Class <?>> equalsOnlyTypes = new LinkedHashSet <Class <?>>(10 );
83+ private final Collection <ClassAndMethod > equalsOnlyValueProviderTypes = new LinkedHashSet <ClassAndMethod >(10 );
8284 private boolean returnUnchangedNodes = false ;
8385 private boolean returnIgnoredNodes = false ;
8486 private boolean returnCircularNodes = true ;
@@ -124,12 +126,12 @@ public Configuration withoutProperty(final PropertyPath propertyPath)
124126 this .excludedProperties .add (propertyPath );
125127 return this ;
126128 }
127-
128- public Configuration withCompareToOnlyType (final Class <?> type )
129- {
130- this .compareToOnlyTypes .add (type );
131- return this ;
132- }
129+
130+ public Configuration withCompareToOnlyType (final Class <?> type )
131+ {
132+ this .compareToOnlyTypes .add (type );
133+ return this ;
134+ }
133135
134136 public Configuration withEqualsOnlyType (final Class <?> type )
135137 {
@@ -143,12 +145,22 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath)
143145 return this ;
144146 }
145147
148+ public Configuration withEqualsOnlyValueProviderMethod (final PropertyPath propertyPath , final String methodName ) {
149+ this .equalsOnlyValueProviderMethods .add (new PropertyPathAndMethod (propertyPath , methodName ));
150+ return this ;
151+ }
152+
153+ public Configuration withEqualsOnlyValueProviderMethod (PropertyPathAndMethod propertyPathEqualsMethod ) {
154+ this .equalsOnlyValueProviderMethods .add (propertyPathEqualsMethod );
155+ return this ;
156+ }
157+
146158 public Configuration withIgnoredNodes ()
147159 {
148160 this .returnIgnoredNodes = true ;
149161 return this ;
150162 }
151-
163+
152164 public Configuration withoutIgnoredNodes ()
153165 {
154166 this .returnIgnoredNodes = false ;
@@ -319,6 +331,57 @@ public boolean isEqualsOnly(final Node node)
319331 }
320332 return false ;
321333 }
334+
335+ public boolean hasEqualsOnlyValueProviderMethod (Node node ){
336+ return getEqualsOnlyValueProviderMethod (node ) != null ;
337+ }
338+
339+ public String getEqualsOnlyValueProviderMethod (Node node ){
340+ final Class <?> propertyType = node .getType ();
341+ if (propertyType != null )
342+ {
343+ ObjectDiffEqualsOnlyValueProvidedType annotation = propertyType .getAnnotation (ObjectDiffEqualsOnlyValueProvidedType .class );
344+ if (annotation != null )
345+ {
346+ return annotation .method ();
347+ }
348+
349+ ClassAndMethod applicable = findEqualsOnlyValueProviderMethodForClass (propertyType );
350+ if (applicable != null )
351+ {
352+ return applicable .getMethod ();
353+ }
354+ }
355+ if (node .hasEqualsOnlyValueProviderMethod ())
356+ {
357+ return node .getEqualsOnlyValueProviderMethod ();
358+ }
359+ PropertyPathAndMethod applicable = findEqualsOnlyValueProviderMethodForPath (node .getPropertyPath ());
360+ if (applicable != null )
361+ {
362+ return applicable .getMethod ();
363+ }
364+ return null ;
365+ }
366+
367+ private ClassAndMethod findEqualsOnlyValueProviderMethodForClass (Class <?> clazz ){
368+ for (ClassAndMethod propertyPathEqualsOnValueProviderType : equalsOnlyValueProviderTypes ){
369+ if (clazz .equals (propertyPathEqualsOnValueProviderType .getClazz ())){
370+ return propertyPathEqualsOnValueProviderType ;
371+ }
372+ }
373+ return null ;
374+
375+ }
376+
377+ private PropertyPathAndMethod findEqualsOnlyValueProviderMethodForPath (PropertyPath propertyPath ){
378+ for (PropertyPathAndMethod propertyPathEqualsOnValueProviderMethod : equalsOnlyValueProviderMethods ){
379+ if (propertyPath .equals (propertyPathEqualsOnValueProviderMethod .getPropertyPath ())){
380+ return propertyPathEqualsOnValueProviderMethod ;
381+ }
382+ }
383+ return null ;
384+ }
322385
323386 public boolean isReturnable (final Node node )
324387 {
@@ -357,4 +420,5 @@ else if (node.isRemoved())
357420 }
358421 return true ;
359422 }
423+
360424}
0 commit comments