Skip to content

Commit 977a6ce

Browse files
committed
Merged ObjectDiffEqualsOnlyValueProvidedType annotation with ObjectDiffEqualsOnlyType and did some clean-up
1 parent b70c1eb commit 977a6ce

File tree

7 files changed

+91
-70
lines changed

7 files changed

+91
-70
lines changed

src/main/java/de/danielbechler/diff/Configuration.java

+42-31
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public enum PrimitiveDefaultValueMode
7878
private final Collection<PropertyPath> excludedProperties = new HashSet<PropertyPath>(10);
7979
private final Collection<PropertyPath> equalsOnlyProperties = new LinkedHashSet<PropertyPath>(10);
8080
private final Collection<PropertyPathAndMethod> equalsOnlyValueProviderMethods = new LinkedHashSet<PropertyPathAndMethod>(10);
81-
private final Collection<Class<?>> compareToOnlyTypes = new LinkedHashSet<Class<?>>(10);
81+
private final Collection<Class<?>> compareToOnlyTypes = new LinkedHashSet<Class<?>>(10);
8282
private final Collection<Class<?>> equalsOnlyTypes = new LinkedHashSet<Class<?>>(10);
8383
private final Collection<ClassAndMethod> equalsOnlyValueProviderTypes = new LinkedHashSet<ClassAndMethod>(10);
8484
private boolean returnUnchangedNodes = false;
@@ -126,12 +126,12 @@ public Configuration withoutProperty(final PropertyPath propertyPath)
126126
this.excludedProperties.add(propertyPath);
127127
return this;
128128
}
129-
130-
public Configuration withCompareToOnlyType(final Class<?> type)
131-
{
132-
this.compareToOnlyTypes.add(type);
133-
return this;
134-
}
129+
130+
public Configuration withCompareToOnlyType(final Class<?> type)
131+
{
132+
this.compareToOnlyTypes.add(type);
133+
return this;
134+
}
135135

136136
public Configuration withEqualsOnlyType(final Class<?> type)
137137
{
@@ -145,22 +145,25 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath)
145145
return this;
146146
}
147147

148-
public Configuration withEqualsOnlyValueProviderMethod(final PropertyPath propertyPath, final String methodName) {
148+
public Configuration withEqualsOnlyValueProviderMethod(final PropertyPath propertyPath,
149+
final String methodName)
150+
{
149151
this.equalsOnlyValueProviderMethods.add(new PropertyPathAndMethod(propertyPath, methodName));
150152
return this;
151153
}
152-
153-
public Configuration withEqualsOnlyValueProviderMethod(PropertyPathAndMethod propertyPathEqualsMethod) {
154+
155+
public Configuration withEqualsOnlyValueProviderMethod(final PropertyPathAndMethod propertyPathEqualsMethod)
156+
{
154157
this.equalsOnlyValueProviderMethods.add(propertyPathEqualsMethod);
155158
return this;
156159
}
157-
160+
158161
public Configuration withIgnoredNodes()
159162
{
160163
this.returnIgnoredNodes = true;
161164
return this;
162165
}
163-
166+
164167
public Configuration withoutIgnoredNodes()
165168
{
166169
this.returnIgnoredNodes = false;
@@ -331,22 +334,24 @@ public boolean isEqualsOnly(final Node node)
331334
}
332335
return false;
333336
}
334-
335-
public boolean hasEqualsOnlyValueProviderMethod(Node node){
336-
return getEqualsOnlyValueProviderMethod(node) != null;
337+
338+
public boolean hasEqualsOnlyValueProviderMethod(final Node node)
339+
{
340+
return Strings.hasText(getEqualsOnlyValueProviderMethod(node));
337341
}
338-
339-
public String getEqualsOnlyValueProviderMethod(Node node){
342+
343+
public String getEqualsOnlyValueProviderMethod(final Node node)
344+
{
340345
final Class<?> propertyType = node.getType();
341346
if (propertyType != null)
342347
{
343-
ObjectDiffEqualsOnlyValueProvidedType annotation = propertyType.getAnnotation(ObjectDiffEqualsOnlyValueProvidedType.class);
348+
final ObjectDiffEqualsOnlyType annotation = propertyType.getAnnotation(ObjectDiffEqualsOnlyType.class);
344349
if (annotation != null)
345350
{
346-
return annotation.method();
351+
return annotation.valueProviderMethod();
347352
}
348-
349-
ClassAndMethod applicable = findEqualsOnlyValueProviderMethodForClass(propertyType);
353+
354+
final ClassAndMethod applicable = findEqualsOnlyValueProviderMethodForClass(propertyType);
350355
if (applicable != null)
351356
{
352357
return applicable.getMethod();
@@ -356,27 +361,33 @@ public String getEqualsOnlyValueProviderMethod(Node node){
356361
{
357362
return node.getEqualsOnlyValueProviderMethod();
358363
}
359-
PropertyPathAndMethod applicable = findEqualsOnlyValueProviderMethodForPath(node.getPropertyPath());
364+
final PropertyPathAndMethod applicable = findEqualsOnlyValueProviderMethodForPath(node.getPropertyPath());
360365
if (applicable != null)
361366
{
362367
return applicable.getMethod();
363368
}
364369
return null;
365370
}
366-
367-
private ClassAndMethod findEqualsOnlyValueProviderMethodForClass(Class<?> clazz){
368-
for(ClassAndMethod propertyPathEqualsOnValueProviderType: equalsOnlyValueProviderTypes){
369-
if(clazz.equals(propertyPathEqualsOnValueProviderType.getClazz())){
371+
372+
private ClassAndMethod findEqualsOnlyValueProviderMethodForClass(final Class<?> clazz)
373+
{
374+
for (final ClassAndMethod propertyPathEqualsOnValueProviderType : equalsOnlyValueProviderTypes)
375+
{
376+
if (clazz.equals(propertyPathEqualsOnValueProviderType.getClazz()))
377+
{
370378
return propertyPathEqualsOnValueProviderType;
371379
}
372380
}
373381
return null;
374-
382+
375383
}
376-
377-
private PropertyPathAndMethod findEqualsOnlyValueProviderMethodForPath(PropertyPath propertyPath){
378-
for(PropertyPathAndMethod propertyPathEqualsOnValueProviderMethod: equalsOnlyValueProviderMethods){
379-
if(propertyPath.equals(propertyPathEqualsOnValueProviderMethod.getPropertyPath())){
384+
385+
private PropertyPathAndMethod findEqualsOnlyValueProviderMethodForPath(final PropertyPath propertyPath)
386+
{
387+
for (final PropertyPathAndMethod propertyPathEqualsOnValueProviderMethod : equalsOnlyValueProviderMethods)
388+
{
389+
if (propertyPath.equals(propertyPathEqualsOnValueProviderMethod.getPropertyPath()))
390+
{
380391
return propertyPathEqualsOnValueProviderMethod;
381392
}
382393
}

src/main/java/de/danielbechler/diff/Instances.java

+19-12
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import java.util.*;
2626

27-
import static de.danielbechler.util.Objects.*;
2827
import static de.danielbechler.util.Comparables.*;
28+
import static de.danielbechler.util.Objects.*;
2929

3030
/** @author Daniel Bechler */
3131
@SuppressWarnings({"UnusedDeclaration"})
@@ -172,32 +172,39 @@ public boolean areEqual()
172172
{
173173
return isEqual(base, working);
174174
}
175-
176-
public boolean areMethodResultsEqual(String method) {
177-
try {
178-
if(base == null && working == null){
175+
176+
public boolean areMethodResultsEqual(final String method)
177+
{
178+
try
179+
{
180+
if (base == null && working == null)
181+
{
179182
return true;
180183
}
181-
if(base == null && working != null || base != null && working == null){
184+
if (base == null || working == null)
185+
{
182186
return false;
183187
}
184-
Object baseMethodResult = base.getClass().getMethod(method).invoke(base);
185-
Object workingMethodResult = working.getClass().getMethod(method).invoke(working);
186-
if(baseMethodResult == null){
188+
final Object baseMethodResult = base.getClass().getMethod(method).invoke(base);
189+
final Object workingMethodResult = working.getClass().getMethod(method).invoke(working);
190+
if (baseMethodResult == null)
191+
{
187192
return workingMethodResult == null;
188193
}
189194
return baseMethodResult.equals(workingMethodResult);
190-
} catch (Exception e) {
195+
}
196+
catch (Exception e)
197+
{
191198
throw new RuntimeException(e);
192199
}
193200
}
194201

195-
public boolean areEqualByComparison()
202+
public boolean areEqualByComparison()
196203
{
197204
return isEqualByComparison((Comparable) base, (Comparable) working);
198205
}
199206

200-
public boolean areSame()
207+
public boolean areSame()
201208
{
202209
return working == base;
203210
}

src/main/java/de/danielbechler/diff/NodeInspector.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import de.danielbechler.diff.node.*;
2020

2121
/** @author Daniel Bechler */
22+
@SuppressWarnings("SpellCheckingInspection")
2223
interface NodeInspector
2324
{
2425
boolean isIgnored(Node node);
@@ -27,13 +28,9 @@ interface NodeInspector
2728

2829
boolean isExcluded(Node node);
2930

30-
boolean isCompareToOnly(Node node);
31+
boolean isCompareToOnly(Node node);
3132

3233
boolean isEqualsOnly(Node node);
33-
34-
boolean hasEqualsOnlyValueProviderMethod(Node node);
35-
36-
String getEqualsOnlyValueProviderMethod(Node node);
3734

3835
boolean isReturnable(Node node);
3936

@@ -43,4 +40,14 @@ interface NodeInspector
4340
* #isEqualsOnly(de.danielbechler.diff.node.Node)} returns <code>true</code>.
4441
*/
4542
boolean isIntrospectible(Node node);
43+
44+
/*
45+
* TODO
46+
* Find a way to move the folloing methods out of here. I'd prefer if the equalsOnlyProvider would
47+
* be hidden a little bit deeper (e.g. the PropertyAccessor), so Differs don't need to worry about this.
48+
*/
49+
50+
boolean hasEqualsOnlyValueProviderMethod(Node node);
51+
52+
String getEqualsOnlyValueProviderMethod(Node node);
4653
}

src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,19 @@ public void setIgnored(final boolean ignored)
5555
{
5656
this.ignored = ignored;
5757
}
58-
59-
public boolean hasEqualsOnlyValueProviderMethod(){
60-
return this.equalsOnlyValueProviderMethod != null && !this.equalsOnlyValueProviderMethod.equals("");
58+
59+
public boolean hasEqualsOnlyValueProviderMethod()
60+
{
61+
return equalsOnlyValueProviderMethod != null && !equalsOnlyValueProviderMethod.equals("");
6162
}
6263

63-
public void setEqualsOnlyValueProviderMethod(String equalsOnlyValueProviderMethod) {
64+
public void setEqualsOnlyValueProviderMethod(final String equalsOnlyValueProviderMethod)
65+
{
6466
this.equalsOnlyValueProviderMethod = equalsOnlyValueProviderMethod;
6567
}
66-
67-
public String getEqualsOnlyValueProviderMethod(){
68+
69+
public String getEqualsOnlyValueProviderMethod()
70+
{
6871
return equalsOnlyValueProviderMethod;
6972
}
70-
7173
}

src/main/java/de/danielbechler/diff/annotation/ObjectDiffEqualsOnlyType.java

+8
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@
2525
@ObjectDiffAnnotation
2626
public @interface ObjectDiffEqualsOnlyType
2727
{
28+
/**
29+
* The name of a method of the annotated type. The value returned by this method will be used for the equals
30+
* check instead of the annotated object itself. This allows to provide alternative equality checks (e.g.
31+
* comparing a List type by size, by calling <code>size()</code>.) The method should have no parameters.
32+
*
33+
* @return The name of a method providing a different object for the equals check.
34+
*/
35+
String valueProviderMethod() default "";
2836
}

src/main/java/de/danielbechler/diff/annotation/ObjectDiffEqualsOnlyValueProvidedType.java

-15
This file was deleted.

src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private static void handleEqualsOnlyTypeAnnotation(final Method readMethod, fina
109109
if (annotation != null)
110110
{
111111
propertyAccessor.setEqualsOnly(true);
112+
propertyAccessor.setEqualsOnlyValueProviderMethod(annotation.valueProviderMethod());
112113
}
113114
}
114115
}

0 commit comments

Comments
 (0)