Skip to content

Commit 4be8d20

Browse files
author
Jonathan Pereira
committed
Merge branch 'development'
# Conflicts: # pom.xml
2 parents 788f97f + 08e511e commit 4be8d20

22 files changed

Lines changed: 476 additions & 227 deletions

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ public class Main {
4747
DiffConfiguration configuration = DiffBuilder.map(User.class)
4848
.mapper()
4949
.mappingAll()
50+
.mapper()
5051
.mapping("emails", "description")
5152
.instance()
5253
.configuration();
5354
54-
List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2, configuration);
55+
List<DiffResult> diffs = DiffObjects.diff(u1, u2, configuration);
5556
56-
for (DiffResult<?> diff : diffs) {
57+
for (DiffResult diff : diffs) {
5758
if (!diff.isEquals()) {
5859
System.out.println("Field: " + diff.getProperties().get("field"));
5960
System.out.println("Before: " + diff.getBefore());
@@ -116,9 +117,9 @@ public class Main {
116117
u1.getEmails().add(new Email("user@gmail.com", true));
117118
u2.getEmails().add(new Email("user@gmail.com", false));
118119
119-
List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2);
120+
List<DiffResult> diffs = DiffObjects.diff(u1, u2);
120121
121-
for (DiffResult<?> diff : diffs) {
122+
for (DiffResult diff : diffs) {
122123
if (!diff.isEquals()) {
123124
System.out.println("Field: " + diff.getProperties().get("field"));
124125
System.out.println("Before: " + diff.getBefore());
@@ -159,9 +160,9 @@ public class Main {
159160
public static void main(String[] args) {
160161
// code omitted ...
161162
162-
List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2);
163+
List<DiffResult> diffs = DiffObjects.diff(u1, u2);
163164
164-
for (DiffResult<?> diff : diffs) {
165+
for (DiffResult diff : diffs) {
165166
if (diff.getProperties().containsKey("id")) {
166167
String id = diff.getProperties().get("id");
167168
String maxlength = diff.getProperties().get("maxlength");
@@ -180,7 +181,5 @@ public class Main {
180181
## Future implementations
181182

182183
- Order of the result using @DiffOrder not implemented yet;
183-
- Disable mapped fields after calling DiffBuilder.map(User.class).mapper().mappingAll();
184184
- Enable only different object for diff result;
185185
- Collection with value working with navigate properties like "emails.principal.description";
186-
- Remove wildcard from DiffResult class;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class DiffObjects {
2222
* @param afterState objeto com as informaçnoes depois da alteração.
2323
* @return resultado do instance.
2424
*/
25-
public static <T> List<DiffResult<?>> diff(T beforeState, T afterState) {
25+
public static <T> List<DiffResult> diff(T beforeState, T afterState) {
2626
Objects.requireNonNull(beforeState, "Before state is required.");
2727
Objects.requireNonNull(afterState, "After state is required.");
2828

@@ -52,15 +52,15 @@ public static <T> boolean isEquals(T beforeState, T afterState) {
5252
* @param afterState objeto com as informaçnoes depois da alteração.
5353
* @return resultado do instance.
5454
*/
55-
public static <T> List<DiffResult<?>> diff(T beforeState, T afterState, DiffConfiguration configuration) {
55+
public static <T> List<DiffResult> diff(T beforeState, T afterState, DiffConfiguration configuration) {
5656
Objects.requireNonNull(beforeState, "Before state is required.");
5757
Objects.requireNonNull(afterState, "After state is required.");
5858
Objects.requireNonNull(configuration, "Configuration is required.");
5959

60-
List<DiffResult<?>> results = new LinkedList<>();
60+
List<DiffResult> results = new LinkedList<>();
6161

6262
for (DiffMetadata metadata : configuration.build()) {
63-
DiffResult<Object> diff = metadata.getStrategy().diff(beforeState, afterState, metadata);
63+
DiffResult diff = metadata.getStrategy().diff(beforeState, afterState, metadata);
6464
diff.setProperties(Collections.unmodifiableMap(metadata.getProperties()));
6565
results.add(diff);
6666
}
@@ -82,7 +82,7 @@ public static <T> boolean isEquals(T beforeState, T afterState, DiffConfiguratio
8282
Objects.requireNonNull(configuration, "Configuration is required.");
8383

8484
for (DiffMetadata metadata : configuration.build()) {
85-
DiffResult<T> result = metadata.getStrategy().diff(beforeState, afterState, metadata);
85+
DiffResult result = metadata.getStrategy().diff(beforeState, afterState, metadata);
8686

8787
if (!result.isEquals()) {
8888
return false;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
/**
66
* @author jonpereiradev@gmail.com
77
*/
8-
public class DiffResult<T> {
8+
public class DiffResult {
99

10-
private T before;
11-
private T after;
10+
private Object before;
11+
private Object after;
1212
private boolean equals;
1313
private Map<String, String> properties;
1414

15-
public DiffResult(T before, T after, boolean equals) {
15+
public DiffResult(Object before, Object after, boolean equals) {
1616
this.before = before;
1717
this.after = after;
1818
this.equals = equals;
1919
}
2020

21-
public T getBefore() {
21+
public Object getBefore() {
2222
return before;
2323
}
2424

25-
public T getAfter() {
25+
public Object getAfter() {
2626
return after;
2727
}
2828

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

Lines changed: 16 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
import com.github.jonpereiradev.diffobjects.DiffException;
44
import 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.
@@ -19,14 +15,14 @@
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
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
import com.github.jonpereiradev.diffobjects.annotation.DiffOrder;
4+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
final class DiffConfigurationImpl implements DiffConfiguration {
12+
13+
private final Map<String, DiffMetadata> metadatas;
14+
private final List<DiffMetadata> diffMetadatas;
15+
16+
DiffConfigurationImpl(Map<String, DiffMetadata> metadatas) {
17+
this.metadatas = metadatas;
18+
this.diffMetadatas = new ArrayList<>(metadatas.keySet().size());
19+
}
20+
21+
/**
22+
* Gets the configuration for the instance instance.
23+
*
24+
* @return the metadata generated by the instance instance.
25+
*/
26+
@Override
27+
public List<DiffMetadata> build() {
28+
if (diffMetadatas.isEmpty()) {
29+
boolean isSortable = false;
30+
31+
for (Map.Entry<String, DiffMetadata> entry : metadatas.entrySet()) {
32+
DiffOrder annotation = entry.getValue().getMethod().getAnnotation(DiffOrder.class);
33+
34+
if (annotation != null) {
35+
entry.getValue().setOrder(annotation.value());
36+
isSortable = true;
37+
}
38+
39+
diffMetadatas.add(entry.getValue());
40+
}
41+
42+
if (isSortable) {
43+
Collections.sort(diffMetadatas);
44+
}
45+
}
46+
47+
return diffMetadatas;
48+
}
49+
50+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public interface DiffInstanceBuilder {
1717
*/
1818
DiffMappingBuilder mapper();
1919

20+
/**
21+
* Gets the object responsible for query mappings for change.
22+
*
23+
* @param field the name of the field mapped in the builder.
24+
* @return the instance of the builder.
25+
*/
26+
DiffQueryBuilder query(String field);
27+
2028
/**
2129
* Gets the configuration instance to get the configuration generated by this instance instance.
2230
*

0 commit comments

Comments
 (0)