17
17
package de .danielbechler .diff ;
18
18
19
19
import de .danielbechler .diff .accessor .*;
20
- import de .danielbechler .diff .accessor . exception . ExceptionListener ;
20
+ import de .danielbechler .diff .comparison .* ;
21
21
import de .danielbechler .diff .introspect .*;
22
- import de .danielbechler .diff .node .*;
23
22
import de .danielbechler .util .*;
24
23
25
24
/**
28
27
*
29
28
* @author Daniel Bechler
30
29
*/
31
- final class BeanDiffer implements Differ < Node >
30
+ final class BeanDiffer implements Differ
32
31
{
33
- private final NodeInspector nodeInspector ;
34
- private Introspector introspector = new StandardIntrospector ();
35
- private BeanPropertyComparisonDelegator beanPropertyComparisonDelegator ;
36
- private DefaultNodeFactory defaultNodeFactory = new DefaultNodeFactory ();
32
+ private final IsIntrospectableResolver isIntrospectableResolver ;
33
+ private final IsReturnableResolver isReturnableResolver ;
34
+ private final ComparisonStrategyResolver comparisonStrategyResolver ;
35
+ private final Introspector introspector ;
36
+ private final DifferDispatcher differDispatcher ;
37
37
38
- public BeanDiffer (final DifferDelegator delegator , final NodeInspector nodeInspector , final ExceptionListener exceptionListener )
38
+ public BeanDiffer (final DifferDispatcher differDispatcher ,
39
+ final Introspector introspector ,
40
+ final IsIntrospectableResolver introspectableResolver ,
41
+ final IsReturnableResolver returnableResolver ,
42
+ final ComparisonStrategyResolver comparisonStrategyResolver )
39
43
{
40
- Assert .notNull (delegator , "delegator" );
41
- Assert .notNull (nodeInspector , "configuration" );
42
- this .beanPropertyComparisonDelegator = new BeanPropertyComparisonDelegator (delegator , nodeInspector , exceptionListener );
43
- this .nodeInspector = nodeInspector ;
44
+ Assert .notNull (differDispatcher , "differDispatcher" );
45
+ this .differDispatcher = differDispatcher ;
46
+
47
+ Assert .notNull (introspector , "introspector" );
48
+ this .introspector = introspector ;
49
+
50
+ Assert .notNull (introspectableResolver , "introspectableResolver" );
51
+ this .isIntrospectableResolver = introspectableResolver ;
52
+
53
+ Assert .notNull (returnableResolver , "returnableResolver" );
54
+ this .isReturnableResolver = returnableResolver ;
55
+
56
+ Assert .notNull (comparisonStrategyResolver , "comparisonStrategyResolver" );
57
+ this .comparisonStrategyResolver = comparisonStrategyResolver ;
44
58
}
45
59
46
- public final Node compare (final Node parentNode , final Instances instances )
60
+ public boolean accepts (final Class <?> type )
47
61
{
48
- final Node beanNode = defaultNodeFactory .createNode (parentNode , instances );
49
- if (nodeInspector .isIgnored (beanNode ))
50
- {
51
- beanNode .setState (Node .State .IGNORED );
52
- }
53
- else if (nodeInspector .hasEqualsOnlyValueProviderMethod (beanNode )){
54
- String method = nodeInspector .getEqualsOnlyValueProviderMethod (beanNode );
55
- if (instances .areMethodResultsEqual (method ))
56
- {
57
- beanNode .setState (Node .State .UNTOUCHED );
58
- }
59
- else
60
- {
61
- beanNode .setState (Node .State .CHANGED );
62
- }
63
- }
64
- else if (instances .areNull () || instances .areSame ())
62
+ return !type .isPrimitive () && !type .isArray ();
63
+ }
64
+
65
+ public final DiffNode compare (final DiffNode parentNode , final Instances instances )
66
+ {
67
+ final DiffNode beanNode = new DiffNode (parentNode , instances .getSourceAccessor (), instances .getType ());
68
+ // if (isIgnoredResolver.isIgnored(beanNode))
69
+ // {
70
+ // beanNode.setState(Node.State.IGNORED);
71
+ // }
72
+ // else
73
+ if (instances .areNull () || instances .areSame ())
65
74
{
66
- beanNode .setState (Node .State .UNTOUCHED );
75
+ beanNode .setState (DiffNode .State .UNTOUCHED );
67
76
}
68
77
else if (instances .hasBeenAdded ())
69
78
{
70
79
compareUsingAppropriateMethod (beanNode , instances );
71
- beanNode .setState (Node .State .ADDED );
80
+ beanNode .setState (DiffNode .State .ADDED );
72
81
}
73
82
else if (instances .hasBeenRemoved ())
74
83
{
75
84
compareUsingAppropriateMethod (beanNode , instances );
76
- beanNode .setState (Node .State .REMOVED );
85
+ beanNode .setState (DiffNode .State .REMOVED );
77
86
}
78
87
else
79
88
{
@@ -82,80 +91,30 @@ else if (instances.hasBeenRemoved())
82
91
return beanNode ;
83
92
}
84
93
85
- private void compareUsingAppropriateMethod (final Node beanNode , final Instances instances )
94
+ private void compareUsingAppropriateMethod (final DiffNode beanNode , final Instances instances )
86
95
{
87
- if (nodeInspector .isCompareToOnly (beanNode ))
96
+ final ComparisonStrategy comparisonStrategy = comparisonStrategyResolver .resolveComparisonStrategy (beanNode );
97
+ if (comparisonStrategy != null )
88
98
{
89
- compareUsingCompareTo (beanNode , instances );
99
+ comparisonStrategy . compare (beanNode , instances );
90
100
}
91
- else if (nodeInspector . isEqualsOnly (beanNode ))
101
+ else if (isIntrospectableResolver . isIntrospectable (beanNode ))
92
102
{
93
- compareUsingEquals (beanNode , instances );
103
+ compareUsingIntrospection (beanNode , instances );
94
104
}
95
- else if (nodeInspector .isIntrospectible (beanNode ))
96
- {
97
- compareUsingIntrospection (beanNode , instances );
98
- }
99
105
}
100
106
101
- private void compareUsingIntrospection (final Node beanNode , final Instances beanInstances )
107
+ private void compareUsingIntrospection (final DiffNode beanNode , final Instances beanInstances )
102
108
{
103
109
final Class <?> beanType = beanInstances .getType ();
104
- final Iterable <Accessor > propertyAccessors = introspector .introspect (beanType );
105
- for (final Accessor propertyAccessor : propertyAccessors )
110
+ final Iterable <PropertyAwareAccessor > propertyAccessors = introspector .introspect (beanType );
111
+ for (final PropertyAwareAccessor propertyAccessor : propertyAccessors )
106
112
{
107
- final Node propertyNode = beanPropertyComparisonDelegator . compare (beanNode , beanInstances , propertyAccessor );
108
- if (nodeInspector .isReturnable (propertyNode ))
113
+ final DiffNode propertyNode = differDispatcher . dispatch (beanNode , beanInstances , propertyAccessor );
114
+ if (isReturnableResolver .isReturnable (propertyNode ))
109
115
{
110
116
beanNode .addChild (propertyNode );
111
117
}
112
118
}
113
119
}
114
-
115
- @ SuppressWarnings ({"MethodMayBeStatic" })
116
- private void compareUsingCompareTo (final Node beanNode , final Instances instances )
117
- {
118
- if (instances .areEqualByComparison ())
119
- {
120
- beanNode .setState (Node .State .UNTOUCHED );
121
- }
122
- else
123
- {
124
- beanNode .setState (Node .State .CHANGED );
125
- }
126
- }
127
-
128
- @ SuppressWarnings ({"MethodMayBeStatic" })
129
- private void compareUsingEquals (final Node beanNode , final Instances instances )
130
- {
131
- if (instances .areEqual ())
132
- {
133
- beanNode .setState (Node .State .UNTOUCHED );
134
- }
135
- else
136
- {
137
- beanNode .setState (Node .State .CHANGED );
138
- }
139
- }
140
-
141
- @ TestOnly
142
- void setIntrospector (final Introspector introspector )
143
- {
144
- Assert .notNull (introspector , "introspector" );
145
- this .introspector = introspector ;
146
- }
147
-
148
- @ TestOnly
149
- void setBeanPropertyComparisonDelegator (final BeanPropertyComparisonDelegator beanPropertyComparisonDelegator )
150
- {
151
- Assert .notNull (beanPropertyComparisonDelegator , "beanPropertyComparisonDelegator" );
152
- this .beanPropertyComparisonDelegator = beanPropertyComparisonDelegator ;
153
- }
154
-
155
- @ TestOnly
156
- public void setDefaultNodeFactory (final DefaultNodeFactory defaultNodeFactory )
157
- {
158
- Assert .notNull (defaultNodeFactory , "defaultNodeFactory" );
159
- this .defaultNodeFactory = defaultNodeFactory ;
160
- }
161
120
}
0 commit comments