22
33import com .github .jonpereiradev .diffobjects .DiffException ;
44import com .github .jonpereiradev .diffobjects .strategy .DiffMetadata ;
5- import com .github .jonpereiradev .diffobjects .strategy .DiffReflections ;
6- import com .github .jonpereiradev .diffobjects .strategy .DiffStrategyType ;
7- import org .apache .commons .lang .StringUtils ;
85
9- import java .lang .reflect .Field ;
10- import java .lang .reflect .Method ;
11- import java .lang .reflect .Modifier ;
12- import java .util .*;
6+ import java .util .LinkedHashMap ;
7+ import java .util .Map ;
8+ import java .util .Objects ;
139
1410/**
1511 * Responsible to map a class and fields to be able to generate diffs.
1915 * @see DiffMappingBuilder
2016 * @see DiffConfiguration
2117 */
22- public final class DiffBuilder implements DiffInstanceBuilder , DiffMappingBuilder , DiffConfiguration {
18+ public final class DiffBuilder implements DiffInstanceBuilder {
2319
24- private final Class <?> classMap ;
2520 private final Map <String , DiffMetadata > metadatas ;
21+ private final DiffMappingBuilder diffMappingBuilder ;
2622
2723 private DiffBuilder (Class <?> classMap ) {
28- this .classMap = classMap ;
2924 this .metadatas = new LinkedHashMap <>();
25+ this .diffMappingBuilder = new DiffMappingBuilderImpl (classMap , metadatas , this );
3026 }
3127
3228 /**
@@ -47,107 +43,24 @@ public static DiffInstanceBuilder map(Class<?> clazz) {
4743 */
4844 @ Override
4945 public DiffMappingBuilder mapper () {
50- return this ;
46+ return diffMappingBuilder ;
5147 }
5248
5349 /**
54- * Maps all the field of a class .
50+ * Finds a mapping in the builder to make operations .
5551 *
56- * @return the instance instance responsible for this mapping.
57- */
58- @ Override
59- public DiffMappingBuilder mappingAll () {
60- Class <?> clazz = classMap ;
61-
62- if (!metadatas .isEmpty ()) {
63- throw new IllegalStateException ("The mappingAll cannot be used after a mapping(field) call." );
64- }
65-
66- while (clazz != null && !clazz .equals (Object .class )) {
67- for (Field parentField : clazz .getDeclaredFields ()) {
68- mapping (parentField .getName ());
69- }
70-
71- clazz = clazz .getSuperclass ();
72- }
73-
74- return this ;
75- }
76-
77- /**
78- * Maps the getter of the field for the class.
79- *
80- * @param field name of the field that will me used to find the getter method.
81- * @return the instance of this mapping instance.
82- */
83- @ Override
84- public DiffMappingBuilder mapping (String field ) {
85- return mapping (field , StringUtils .EMPTY );
86- }
87-
88- /**
89- * Maps the getter of the field for the class with the value property to allow deep diff.
90- *
91- * @param field name of the field that will me used to find the getter method.
92- * @param value the nested property of the object to make the diff.
93- * @return the instance of this mapping instance.
94- * @throws DiffException throw if the field doesn't have a public no args method for the field.
95- */
96- @ Override
97- public DiffMappingBuilder mapping (String field , String value ) {
98- Method method = DiffReflections .discoverGetter (classMap , field );
99- DiffStrategyType diffStrategyType = DiffStrategyType .SINGLE ;
100-
101- if (method == null ) {
102- throw new DiffException ("Method " + field + " not found in class " + classMap .getName ());
103- }
104-
105- if (!Modifier .isPublic (method .getModifiers ()) || method .getParameterTypes ().length > 0 ) {
106- throw new DiffException ("Method " + method .getName () + " must be public and no-args." );
107- }
108-
109- if (value != null && !value .isEmpty ()) {
110- diffStrategyType = DiffStrategyType .DEEP ;
111- }
112-
113- if (Collection .class .isAssignableFrom (method .getReturnType ())) {
114- diffStrategyType = DiffStrategyType .COLLECTION ;
115- }
116-
117- DiffMetadata diffMetadata = new DiffMetadata (value , method , diffStrategyType );
118- diffMetadata .getProperties ().put ("field" , field );
119-
120- metadatas .put (field , diffMetadata );
121-
122- return this ;
123- }
124-
125- /**
126- * Define a property for the last mapping.
127- *
128- * @param key the identifier of the property.
129- * @param value the value of the property.
52+ * @param field the name of the field mapped in the builder.
13053 * @return the instance of this mapping.
13154 */
13255 @ Override
133- public DiffMappingBuilder property (String key , String value ) {
134- if (metadatas .isEmpty ()) {
135- throw new DiffException ("A mapping field is required to associate the property." );
136- }
137-
138- metadatas .get (metadatas .size () - 1 ).getProperties ().put (key , value );
56+ public DiffQueryBuilder query (String field ) {
57+ DiffMetadata diffMetadata = metadatas .get (field );
13958
140- return this ;
141- }
59+ if (diffMetadata == null ) {
60+ throw new DiffException ("No field \" " + field + "\" mapped in builder. You need to map the field before query." );
61+ }
14262
143- /**
144- * Returns to the instance instance to allow the fluent interface.
145- *
146- * @return the instance instance responsible for this mapping.
147- */
148- @ Override
149- public DiffInstanceBuilder instance () {
150- return this ;
63+ return new DiffQueryBuilderImpl (diffMetadata , this );
15164 }
15265
15366 /**
@@ -157,16 +70,6 @@ public DiffInstanceBuilder instance() {
15770 */
15871 @ Override
15972 public DiffConfiguration configuration () {
160- return this ;
161- }
162-
163- /**
164- * Gets the configuration for the instance instance.
165- *
166- * @return the metadata generated by the instance instance.
167- */
168- @ Override
169- public List <DiffMetadata > build () {
170- return new ArrayList <>(metadatas .values ());
73+ return new DiffConfigurationImpl (metadatas );
17174 }
17275}
0 commit comments