Skip to content

Commit bbdb381

Browse files
author
Jonathan Pereira
committed
Merge branch 'development'
# Conflicts: # src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffQueryBuilderImpl.java # src/test/java/com/github/jonpereiradev/diffobjects/builder/DiffBuilderTest.java # src/test/java/com/github/jonpereiradev/diffobjects/strategy/BaseStrategyTest.java
2 parents c0ca26a + a208731 commit bbdb381

21 files changed

Lines changed: 368 additions & 254 deletions

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Diff Objects
22

3-
This project helps you to build functionalities that needs to show the differences between two objects.
3+
This project helps you to build functionalities that needs to show the differences between two objects. You can use
4+
this project in two ways:
45

5-
You can use this project in two ways, using annotations or a Builder to map the properties of the object that will be
6-
checked for difference.
6+
- using the annotations @DiffMappings, @DiffMapping and @DiffProperty;
7+
- using the DiffBuilder to map the properties of a class;
78

89
## DiffBuilder
910

10-
The DiffBuilder provide an API to map an object without the use of annotations:
11+
The DiffBuilder provides an API to map an object without the use of annotations:
1112

1213
**Example**
1314

@@ -44,12 +45,11 @@ public class Main {
4445
u1.getEmails().add(new Email("user@gmail.com", true));
4546
u2.getEmails().add(new Email("user@gmail.com", false));
4647
47-
DiffConfiguration configuration = DiffBuilder.map(User.class)
48-
.mapper()
49-
.mappingAll()
50-
.mapper()
48+
DiffConfiguration configuration = DiffBuilder
49+
.map(User.class)
50+
.mapping("login")
51+
.mapping("password")
5152
.mapping("emails", "description")
52-
.instance()
5353
.configuration();
5454
5555
List<DiffResult> diffs = DiffObjects.diff(u1, u2, configuration);
@@ -69,9 +69,9 @@ public class Main {
6969

7070
The annotations provided by the diff objects are:
7171

72-
- DiffMapping - to map a method of the object;
73-
- DiffMappings - to map multiple properties of an object relationship;
74-
- DiffProperty - to add properties that will be on the diff result for access;
72+
- __DiffMappings:__ to map multiple properties of an object relationship;
73+
- __DiffMapping:__ to map a method of an object;
74+
- __DiffProperty:__ to add properties that will be on the diff result for access;
7575

7676
**Example**
7777

@@ -181,4 +181,4 @@ public class Main {
181181
## Future implementations
182182

183183
- Enable only different object for diff result;
184-
- Collection with value working with navigate properties like "emails.principal.description";
184+
- Collection with value working with nested fields (Ex: "emails.principal.description");

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

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

44
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
55
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
6-
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
6+
import com.github.jonpereiradev.diffobjects.builder.DiffReflections;
77
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategy;
88
import java.util.ArrayList;
99

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

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

7+
import java.lang.reflect.Field;
78
import java.util.LinkedHashMap;
89
import java.util.Map;
910
import java.util.Objects;
@@ -13,20 +14,19 @@
1314
* Responsible to map a class and fields to be able to generate diffs.
1415
*
1516
* @author Jonathan Pereira
16-
* @since 1.0
17-
*
1817
* @see DiffInstanceBuilder
1918
* @see DiffMappingBuilder
2019
* @see DiffConfiguration
20+
* @since 1.0
2121
*/
2222
public final class DiffBuilder implements DiffInstanceBuilder {
2323

24+
private final Class<?> classMap;
2425
private final Map<String, DiffMetadata> metadatas;
25-
private final DiffMappingBuilder diffMappingBuilder;
2626

2727
private DiffBuilder(Class<?> classMap) {
28+
this.classMap = classMap;
2829
this.metadatas = new LinkedHashMap<>();
29-
this.diffMappingBuilder = new DiffMappingBuilderImpl(classMap, metadatas, this);
3030
}
3131

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

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

5365
/**
54-
* Finds a mapping in the builder to make operations.
66+
* Maps the getter of the field for the class.
5567
*
56-
* @param field the name of the field mapped in the builder.
57-
* @return the instance of this mapping.
68+
* @param field name of the field that will me used to find the getter method.
69+
* @return the instance of this mapping instance.
5870
*/
5971
@Override
60-
public DiffQueryBuilder query(String field) {
61-
DiffMetadata diffMetadata = metadatas.get(field);
62-
63-
if (diffMetadata == null) {
64-
throw new DiffException("No field \"" + field + "\" mapped in builder. You need to map the field before query.");
65-
}
66-
67-
return new DiffQueryBuilderImpl(diffMetadata, this);
72+
public DiffQueryMappingBuilder mapping(String field) {
73+
return new DiffMappingBuilderImpl(classMap, metadatas).mapping(field);
6874
}
6975

7076
/**
71-
* Gets the configuration instance to get the configuration generated by this instance instance.
77+
* Maps the getter of the field for the class with the value property to allow deep diff.
7278
*
73-
* @return a configuration instance instance.
79+
* @param field name of the field that will me used to find the getter method.
80+
* @param value the nested property of the object to make the diff.
81+
* @return the instance of this mapping instance.
82+
* @throws DiffException throw if the field doesn't have a public no args method for the field.
7483
*/
7584
@Override
76-
public DiffConfiguration configuration() {
77-
return new DiffConfigurationImpl(metadatas);
85+
public DiffQueryMappingBuilder mapping(String field, String value) {
86+
return new DiffMappingBuilderImpl(classMap, metadatas).mapping(field, value);
87+
}
88+
89+
Map<String, DiffMetadata> getMetadatas() {
90+
return metadatas;
7891
}
7992
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
* Responsible for generate the configuration of the instance.
1111
*
1212
* @author Jonathan Pereira
13-
* @since 1.0
14-
*
1513
* @see DiffBuilder
1614
* @see DiffInstanceBuilder
1715
* @see DiffMappingBuilder
16+
* @since 1.0
1817
*/
1918
public interface DiffConfiguration {
2019

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
* Responsible for generate the configuration of the instance.
1515
*
1616
* @author Jonathan Pereira
17-
* @since 1.0
18-
*
1917
* @see DiffBuilder
2018
* @see DiffInstanceBuilder
2119
* @see DiffMappingBuilder
20+
* @since 1.0
2221
*/
2322
final class DiffConfigurationImpl implements DiffConfiguration {
2423

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

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

1615
/**
17-
* Gets the mapping instance to registry the fields used in the diff.
16+
* Maps all the field of a class.
1817
*
19-
* @return a mapping instance instance.
18+
* @return the instance responsible for this mapping.
2019
*/
21-
DiffMappingBuilder mapper();
20+
DiffMappingAllBuilder mappingAll();
2221

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

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

3839
}
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
@@ -5,54 +5,35 @@
55
* Builder responsible for mapping the fields of a class to create a configuration of diff.
66
*
77
* @author Jonathan Pereira
8-
* @since 1.0
9-
*
108
* @see DiffBuilder
119
* @see DiffInstanceBuilder
1210
* @see DiffConfiguration
11+
* @since 1.0
1312
*/
1413
public interface DiffMappingBuilder {
1514

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

3923
/**
40-
* Maps the getter of the field for the class with the value property to allow deep diff.
24+
* Maps the getter of the field for the class with the nestedField property to allow deep diff.
4125
*
4226
* @param field name of the field that will me used to find the getter method.
43-
* @param value the nested property of the object to make the diff.
44-
*
27+
* @param nestedField the nested property of the object to make the diff.
4528
* @return the instance of this mapping.
4629
*/
47-
DiffQueryBuilder mapping(String field, String value);
30+
DiffQueryMappingBuilder mapping(String field, String nestedField);
4831

4932
/**
50-
* Remove a mapping of the field for the class.
51-
*
52-
* @param field name of the field that will me used to remove.
33+
* Gets the configuration instance to get the configuration generated by this instance instance.
5334
*
54-
* @return the instance of this mapping.
35+
* @return a configuration instance instance.
5536
*/
56-
DiffMappingBuilder unmapping(String field);
37+
DiffConfiguration configuration();
5738

5839
}

0 commit comments

Comments
 (0)