Skip to content

Commit 7b11c8b

Browse files
jonpereiradevJonathan Pereira
authored andcommitted
Refactory
1 parent 9913bbd commit 7b11c8b

20 files changed

Lines changed: 391 additions & 272 deletions

src/main/java/com/github/jonpereiradev/diffobjects/DiffObjects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
44
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
5-
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
5+
import com.github.jonpereiradev.diffobjects.builder.DiffReflections;
66
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategy;
77
import java.util.ArrayList;
88

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffBuilder.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.jonpereiradev.diffobjects.DiffException;
44
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
55

6+
import java.lang.reflect.Field;
67
import java.util.LinkedHashMap;
78
import java.util.Map;
89
import java.util.Objects;
@@ -11,20 +12,19 @@
1112
* Responsible to map a class and fields to be able to generate diffs.
1213
*
1314
* @author Jonathan Pereira
14-
* @since 1.0
15-
*
1615
* @see DiffInstanceBuilder
1716
* @see DiffMappingBuilder
1817
* @see DiffConfiguration
18+
* @since 1.0
1919
*/
2020
public final class DiffBuilder implements DiffInstanceBuilder {
2121

22+
private final Class<?> classMap;
2223
private final Map<String, DiffMetadata> metadatas;
23-
private final DiffMappingBuilder diffMappingBuilder;
2424

2525
private DiffBuilder(Class<?> classMap) {
26+
this.classMap = classMap;
2627
this.metadatas = new LinkedHashMap<>();
27-
this.diffMappingBuilder = new DiffMappingBuilderImpl(classMap, metadatas, this);
2828
}
2929

3030
/**
@@ -33,45 +33,58 @@ private DiffBuilder(Class<?> classMap) {
3333
* @param clazz the class that will be registry to make diffs.
3434
* @return the diff instance instance.
3535
*/
36-
public static DiffInstanceBuilder map(Class<?> clazz) {
36+
public static DiffBuilder map(Class<?> clazz) {
3737
Objects.requireNonNull(clazz, "Class is required.");
3838
return new DiffBuilder(clazz);
3939
}
4040

4141
/**
42-
* Gets the mapping instance to registry the fields used in the diff.
42+
* Maps all the field of a class.
4343
*
44-
* @return a mapping instance instance.
44+
* @return the instance instance responsible for this mapping.
4545
*/
4646
@Override
47-
public DiffMappingBuilder mapper() {
48-
return diffMappingBuilder;
47+
public DiffMappingAllBuilder mappingAll() {
48+
Class<?> clazz = classMap;
49+
50+
while (clazz != null && !clazz.equals(Object.class)) {
51+
for (Field parentField : clazz.getDeclaredFields()) {
52+
if (!metadatas.containsKey(parentField.getName())) {
53+
mapping(parentField.getName());
54+
}
55+
}
56+
57+
clazz = clazz.getSuperclass();
58+
}
59+
60+
return new DiffMappingAllBuilderImpl(metadatas);
4961
}
5062

5163
/**
52-
* Finds a mapping in the builder to make operations.
64+
* Maps the getter of the field for the class.
5365
*
54-
* @param field the name of the field mapped in the builder.
55-
* @return the instance of this mapping.
66+
* @param field name of the field that will me used to find the getter method.
67+
* @return the instance of this mapping instance.
5668
*/
5769
@Override
58-
public DiffQueryBuilder query(String field) {
59-
DiffMetadata diffMetadata = metadatas.get(field);
60-
61-
if (diffMetadata == null) {
62-
throw new DiffException("No field \"" + field + "\" mapped in builder. You need to map the field before query.");
63-
}
64-
65-
return new DiffQueryBuilderImpl(diffMetadata, this);
70+
public DiffQueryMappingBuilder mapping(String field) {
71+
return new DiffMappingBuilderImpl(classMap, metadatas).mapping(field);
6672
}
6773

6874
/**
69-
* Gets the configuration instance to get the configuration generated by this instance instance.
75+
* Maps the getter of the field for the class with the value property to allow deep diff.
7076
*
71-
* @return a configuration instance instance.
77+
* @param field name of the field that will me used to find the getter method.
78+
* @param value the nested property of the object to make the diff.
79+
* @return the instance of this mapping instance.
80+
* @throws DiffException throw if the field doesn't have a public no args method for the field.
7281
*/
7382
@Override
74-
public DiffConfiguration configuration() {
75-
return new DiffConfigurationImpl(metadatas);
83+
public DiffQueryMappingBuilder mapping(String field, String value) {
84+
return new DiffMappingBuilderImpl(classMap, metadatas).mapping(field, value);
85+
}
86+
87+
Map<String, DiffMetadata> getMetadatas() {
88+
return metadatas;
7689
}
7790
}

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
* Responsible for generate the configuration of the instance.
99
*
1010
* @author Jonathan Pereira
11-
* @since 1.0
12-
*
1311
* @see DiffBuilder
1412
* @see DiffInstanceBuilder
1513
* @see DiffMappingBuilder
14+
* @since 1.0
1615
*/
1716
public interface DiffConfiguration {
1817

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffConfigurationImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
* Responsible for generate the configuration of the instance.
1313
*
1414
* @author Jonathan Pereira
15-
* @since 1.0
16-
*
1715
* @see DiffBuilder
1816
* @see DiffInstanceBuilder
1917
* @see DiffMappingBuilder
18+
* @since 1.0
2019
*/
2120
final class DiffConfigurationImpl implements DiffConfiguration {
2221

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffInstanceBuilder.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@
44
* Builder with the methods of a instance instance.
55
*
66
* @author Jonathan Pereira
7-
* @since 1.0
8-
*
97
* @see DiffBuilder
108
* @see DiffMappingBuilder
119
* @see DiffConfiguration
10+
* @since 1.0
1211
*/
1312
public interface DiffInstanceBuilder {
1413

1514
/**
16-
* Gets the mapping instance to registry the fields used in the diff.
15+
* Maps all the field of a class.
1716
*
18-
* @return a mapping instance instance.
17+
* @return the instance responsible for this mapping.
1918
*/
20-
DiffMappingBuilder mapper();
19+
DiffMappingAllBuilder mappingAll();
2120

2221
/**
23-
* Gets the object responsible for query mappings for change.
22+
* Maps the getter of the field for the class.
2423
*
25-
* @param field the name of the field mapped in the builder.
26-
* @return the instance of the builder.
24+
* @param field name of the field that will me used to find the getter method.
25+
* @return the instance of this mapping.
2726
*/
28-
DiffQueryBuilder query(String field);
27+
DiffQueryMappingBuilder mapping(String field);
2928

3029
/**
31-
* Gets the configuration instance to get the configuration generated by this instance instance.
30+
* Maps the getter of the field for the class with the value property to allow deep diff.
3231
*
33-
* @return a configuration instance instance.
32+
* @param field name of the field that will me used to find the getter method.
33+
* @param value the nested property of the object to make the diff.
34+
* @return the instance of this mapping.
3435
*/
35-
DiffConfiguration configuration();
36+
DiffQueryMappingBuilder mapping(String field, String value);
3637

3738
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
4+
/**
5+
* Builder responsible for mapping the fields of a class to create a configuration of diff.
6+
*
7+
* @author Jonathan Pereira
8+
* @see DiffBuilder
9+
* @see DiffInstanceBuilder
10+
* @see DiffConfiguration
11+
* @since 1.0
12+
*/
13+
public interface DiffMappingAllBuilder {
14+
15+
/**
16+
* Gets the object responsible for query mappings for change.
17+
*
18+
* @param field the name of the field mapped in the builder.
19+
* @return the instance of the builder.
20+
*/
21+
DiffQueryBuilder query(String field);
22+
23+
/**
24+
* Gets the configuration instance to get the configuration generated by this instance instance.
25+
*
26+
* @return a configuration instance instance.
27+
*/
28+
DiffConfiguration configuration();
29+
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
4+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
5+
6+
import java.util.Map;
7+
8+
9+
/**
10+
* Responsible to map a class and fields to be able to generate diffs.
11+
*
12+
* @author Jonathan Pereira
13+
* @see DiffInstanceBuilder
14+
* @see DiffMappingBuilder
15+
* @see DiffConfiguration
16+
* @since 1.0
17+
*/
18+
final class DiffMappingAllBuilderImpl implements DiffMappingAllBuilder {
19+
20+
private final Map<String, DiffMetadata> metadatas;
21+
22+
DiffMappingAllBuilderImpl(Map<String, DiffMetadata> metadatas) {
23+
this.metadatas = metadatas;
24+
}
25+
26+
/**
27+
* Finds a mapping in the builder to make operations.
28+
*
29+
* @param field the name of the field mapped in the builder.
30+
* @return the instance of this mapping.
31+
*/
32+
@Override
33+
public DiffQueryBuilder query(String field) {
34+
return new DiffQueryBuilderImpl(field, metadatas, this);
35+
}
36+
37+
/**
38+
* Gets the configuration instance to get the configuration generated by this instance instance.
39+
*
40+
* @return a configuration instance instance.
41+
*/
42+
@Override
43+
public DiffConfiguration configuration() {
44+
return new DiffConfigurationImpl(metadatas);
45+
}
46+
}

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffMappingBuilder.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,35 @@
44
* Builder responsible for mapping the fields of a class to create a configuration of diff.
55
*
66
* @author Jonathan Pereira
7-
* @since 1.0
8-
*
97
* @see DiffBuilder
108
* @see DiffInstanceBuilder
119
* @see DiffConfiguration
10+
* @since 1.0
1211
*/
1312
public interface DiffMappingBuilder {
1413

15-
/**
16-
* Returns to the instance to allow the fluent interface.
17-
*
18-
* @return the instance responsible for this mapping.
19-
*/
20-
DiffInstanceBuilder instance();
21-
22-
/**
23-
* Maps all the field of a class.
24-
*
25-
* @return the instance responsible for this mapping.
26-
*/
27-
DiffInstanceBuilder mappingAll();
28-
2914
/**
3015
* Maps the getter of the field for the class.
3116
*
3217
* @param field name of the field that will me used to find the getter method.
33-
*
3418
* @return the instance of this mapping.
3519
*/
36-
DiffQueryBuilder mapping(String field);
20+
DiffQueryMappingBuilder mapping(String field);
3721

3822
/**
39-
* Maps the getter of the field for the class with the value property to allow deep diff.
23+
* Maps the getter of the field for the class with the nestedField property to allow deep diff.
4024
*
4125
* @param field name of the field that will me used to find the getter method.
42-
* @param value the nested property of the object to make the diff.
43-
*
26+
* @param nestedField the nested property of the object to make the diff.
4427
* @return the instance of this mapping.
4528
*/
46-
DiffQueryBuilder mapping(String field, String value);
29+
DiffQueryMappingBuilder mapping(String field, String nestedField);
4730

4831
/**
49-
* Remove a mapping of the field for the class.
50-
*
51-
* @param field name of the field that will me used to remove.
32+
* Gets the configuration instance to get the configuration generated by this instance instance.
5233
*
53-
* @return the instance of this mapping.
34+
* @return a configuration instance instance.
5435
*/
55-
DiffMappingBuilder unmapping(String field);
36+
DiffConfiguration configuration();
5637

5738
}

0 commit comments

Comments
 (0)