2020import de .danielbechler .diff .node .*;
2121import de .danielbechler .diff .path .*;
2222import de .danielbechler .util .*;
23+ import groovy .lang .Tuple ;
2324
2425import java .util .*;
2526
@@ -77,8 +78,10 @@ public enum PrimitiveDefaultValueMode
7778 private final Collection <PropertyPath > includedProperties = new HashSet <PropertyPath >(10 );
7879 private final Collection <PropertyPath > excludedProperties = new HashSet <PropertyPath >(10 );
7980 private final Collection <PropertyPath > equalsOnlyProperties = new LinkedHashSet <PropertyPath >(10 );
81+ private final Collection <MethodEqualPropertyPathAndMethod > methodEqualProperties = new LinkedHashSet <MethodEqualPropertyPathAndMethod >(10 );
8082 private final Collection <Class <?>> compareToOnlyTypes = new LinkedHashSet <Class <?>>(10 );
8183 private final Collection <Class <?>> equalsOnlyTypes = new LinkedHashSet <Class <?>>(10 );
84+ private final Collection <MethodEqualClassAndMethod > methodEqualTypes = new LinkedHashSet <MethodEqualClassAndMethod >(10 );
8285 private boolean returnUnchangedNodes = false ;
8386 private boolean returnIgnoredNodes = false ;
8487 private boolean returnCircularNodes = true ;
@@ -124,7 +127,7 @@ public Configuration withoutProperty(final PropertyPath propertyPath)
124127 this .excludedProperties .add (propertyPath );
125128 return this ;
126129 }
127-
130+
128131 public Configuration withCompareToOnlyType (final Class <?> type )
129132 {
130133 this .compareToOnlyTypes .add (type );
@@ -143,12 +146,22 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath)
143146 return this ;
144147 }
145148
149+ public Configuration withMethodEqualsProperty (final PropertyPath propertyPath , final String methodName ) {
150+ this .methodEqualProperties .add (new MethodEqualPropertyPathAndMethod (propertyPath , methodName ));
151+ return this ;
152+ }
153+
154+ public Configuration withMethodEqualsProperty (MethodEqualPropertyPathAndMethod propertyPathEqualsMethod ) {
155+ this .methodEqualProperties .add (propertyPathEqualsMethod );
156+ return this ;
157+ }
158+
146159 public Configuration withIgnoredNodes ()
147160 {
148161 this .returnIgnoredNodes = true ;
149162 return this ;
150163 }
151-
164+
152165 public Configuration withoutIgnoredNodes ()
153166 {
154167 this .returnIgnoredNodes = false ;
@@ -309,6 +322,57 @@ public boolean isEqualsOnly(final Node node)
309322 }
310323 return false ;
311324 }
325+
326+ public boolean isWithMethodEquals (Node node ){
327+ return getWithMethodEqualsMethod (node ) != null ;
328+ }
329+
330+ public String getWithMethodEqualsMethod (Node node ){
331+ final Class <?> propertyType = node .getType ();
332+ if (propertyType != null )
333+ {
334+ ObjectDiffMethodEqualsType annotation = propertyType .getAnnotation (ObjectDiffMethodEqualsType .class );
335+ if (annotation != null )
336+ {
337+ return annotation .method ();
338+ }
339+
340+ MethodEqualClassAndMethod applicable = findMethodEqualPropertyForClass (propertyType );
341+ if (applicable != null )
342+ {
343+ return applicable .getMethod ();
344+ }
345+ }
346+ if (node .isWithMethodEquals ())
347+ {
348+ return node .getWithMethodEqualsMethod ();
349+ }
350+ MethodEqualPropertyPathAndMethod applicable = findMethodEqualPropertyForPath (node .getPropertyPath ());
351+ if (applicable != null )
352+ {
353+ return applicable .getMethod ();
354+ }
355+ return null ;
356+ }
357+
358+ private MethodEqualClassAndMethod findMethodEqualPropertyForClass (Class <?> clazz ){
359+ for (MethodEqualClassAndMethod propertyPathEqualsMethod : methodEqualTypes ){
360+ if (clazz .equals (propertyPathEqualsMethod .getClazz ())){
361+ return propertyPathEqualsMethod ;
362+ }
363+ }
364+ return null ;
365+
366+ }
367+
368+ private MethodEqualPropertyPathAndMethod findMethodEqualPropertyForPath (PropertyPath propertyPath ){
369+ for (MethodEqualPropertyPathAndMethod propertyPathEqualsMethod : methodEqualProperties ){
370+ if (propertyPath .equals (propertyPathEqualsMethod .getPropertyPath ())){
371+ return propertyPathEqualsMethod ;
372+ }
373+ }
374+ return null ;
375+ }
312376
313377 public boolean isReturnable (final Node node )
314378 {
@@ -347,4 +411,5 @@ else if (node.isRemoved())
347411 }
348412 return true ;
349413 }
414+
350415}
0 commit comments