Skip to content

Commit f851cf1

Browse files
jonpereiradevJonathan Pereira
authored andcommitted
Refactory of the strategies.
1 parent ad177f6 commit f851cf1

22 files changed

Lines changed: 531 additions & 205 deletions

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<scope>test</scope>
2525
</dependency>
2626
<!--<dependency>-->
27-
<!--<groupId>org.mockito</groupId>-->
28-
<!--<artifactId>mockito-core</artifactId>-->
29-
<!--<version>1.10.19</version>-->
30-
<!--<scope>test</scope>-->
27+
<!--<groupId>org.mockito</groupId>-->
28+
<!--<artifactId>mockito-core</artifactId>-->
29+
<!--<version>1.10.19</version>-->
30+
<!--<scope>test</scope>-->
3131
<!--</dependency>-->
3232
</dependencies>
3333
</project>

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

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,91 @@
11
package com.github.jonpereiradev.diffobjects;
22

3+
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
4+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
5+
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
6+
7+
import java.util.List;
8+
39
/**
410
* @author jonpereiradev@gmail.com
511
*/
612
public final class DiffObjects {
713

814
/**
9-
* Executa o diff entre o antes e o depois.
15+
* Executa o builder entre o antes e o depois.
1016
*
1117
* @param <T> tipo do objeto comparado.
1218
* @param before objeto com as informações antes da alteração.
1319
* @param after objeto com as informaçnoes depois da alteração.
14-
* @return resultado do diff.
20+
* @return resultado do builder.
1521
*/
1622
public static <T> DiffResults diff(T before, T after) {
1723
DiffResults result = new DiffResults();
18-
// List<DiffMetadata> metadatas = DiffReflections.discover(before.getClass());
19-
//
20-
// for (DiffMetadata metadata : metadatas) {
21-
// Object beforeObject = DiffReflections.invoke(metadata.getMethod(), before);
22-
// Object afterObject = DiffReflections.invoke(metadata.getMethod(), after);
23-
//
24-
// result.diff(metadata, beforeObject, afterObject);
25-
// }
24+
List<DiffMetadata> metadatas = DiffReflections.discover(before.getClass());
25+
26+
for (DiffMetadata metadata : metadatas) {
27+
result.getResults().add(metadata.getStrategy().diff(before, after, metadata));
28+
}
2629

2730
return result;
2831
}
2932

3033
/**
31-
* Verifica se os objetos são iguais no diff.
34+
* Verifica se os objetos são iguais no builder.
3235
*
3336
* @param <T> tipo do objeto comparado.
3437
* @param before objeto com as informações antes da alteração.
35-
* @param after objeto com as informaçnoes depois da alteração.
36-
* @return resultado do diff.
38+
* @param after objeto com as informações depois da alteração.
39+
* @return resultado do builder.
3740
*/
3841
public static <T> boolean isEquals(T before, T after) {
42+
List<DiffMetadata> metadatas = DiffReflections.discover(before.getClass());
43+
44+
for (DiffMetadata metadata : metadatas) {
45+
DiffResult<T> result = metadata.getStrategy().diff(before, after, metadata);
46+
47+
if (!result.isEquals()) {
48+
return false;
49+
}
50+
}
51+
52+
return true;
53+
}
54+
55+
/**
56+
* Executa o builder entre o antes e o depois.
57+
*
58+
* @param <T> tipo do objeto comparado.
59+
* @param before objeto com as informações antes da alteração.
60+
* @param after objeto com as informaçnoes depois da alteração.
61+
* @return resultado do builder.
62+
*/
63+
public static <T> DiffResults diff(T before, T after, DiffConfiguration configuration) {
3964
DiffResults result = new DiffResults();
40-
// List<DiffMetadata> metadatas = DiffReflections.discover(before.getClass());
41-
//
42-
// for (DiffMetadata metadata : metadatas) {
43-
// Object beforeObject = DiffReflections.invoke(metadata.getMethod(), before);
44-
// Object afterObject = DiffReflections.invoke(metadata.getMethod(), after);
45-
//
46-
// result.diff(metadata, beforeObject, afterObject);
47-
//
48-
// if (!result.isEquals()) {
49-
// return false;
50-
// }
51-
// }
65+
66+
for (DiffMetadata metadata : configuration.getConfigurations()) {
67+
result.getResults().add(metadata.getStrategy().diff(before, after, metadata));
68+
}
69+
70+
return result;
71+
}
72+
73+
/**
74+
* Verifica se os objetos são iguais no builder.
75+
*
76+
* @param <T> tipo do objeto comparado.
77+
* @param before objeto com as informações antes da alteração.
78+
* @param after objeto com as informações depois da alteração.
79+
* @return resultado do builder.
80+
*/
81+
public static <T> boolean isEquals(T before, T after, DiffConfiguration configuration) {
82+
for (DiffMetadata metadata : configuration.getConfigurations()) {
83+
DiffResult<T> result = metadata.getStrategy().diff(before, after, metadata);
84+
85+
if (!result.isEquals()) {
86+
return false;
87+
}
88+
}
5289

5390
return true;
5491
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.jonpereiradev.diffobjects;
22

3-
import com.github.jonpereiradev.diffobjects.annotation.Diff;
4-
53
/**
64
* @author jonpereiradev@gmail.com
75
*/
@@ -17,11 +15,6 @@ public DiffResult(T before, T after, boolean equals) {
1715
this.equals = equals;
1816
}
1917

20-
public DiffResult(Diff annotation, T before, T after) {
21-
this.before = before;
22-
this.after = after;
23-
}
24-
2518
public T getBefore() {
2619
return before;
2720
}

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@ public class DiffResults {
1010

1111
private final List<DiffResult> results = new LinkedList<DiffResult>();
1212

13-
// void diff(DiffMetadata metadata, Object before, Object after) {
14-
// DiffStrategy diffExecutable = DiffStrategyType.SINGLE.getDiffExecutable();
15-
//
16-
// if (metadata.getStrategy() != null) {
17-
// diffExecutable = metadata.getStrategy().type().getDiffExecutable();
18-
// }
19-
//
20-
// DiffResult diff = diffExecutable.diff(metadata.getAnnotation(), before, after);
21-
//
22-
// if (diff != null) {
23-
// results.add(diff);
24-
// }
25-
// }
26-
27-
public boolean isEquals() {
28-
return results.isEmpty();
29-
}
30-
3113
public List<DiffResult> getResults() {
3214
return results;
3315
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
4+
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
5+
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategyType;
6+
import org.apache.commons.lang.StringUtils;
7+
8+
import java.lang.reflect.Method;
9+
import java.util.Collection;
10+
import java.util.LinkedList;
11+
import java.util.List;
12+
13+
public final class DiffBuilder implements DiffInstanceBuilder, DiffMappingBuilder, DiffConfiguration {
14+
15+
private final Class<?> classMap;
16+
private final List<DiffMetadata> metadatas;
17+
18+
private DiffBuilder(Class<?> classMap) {
19+
this.classMap = classMap;
20+
this.metadatas = new LinkedList<>();
21+
}
22+
23+
public static DiffInstanceBuilder map(Class<?> classMap) {
24+
return new DiffBuilder(classMap);
25+
}
26+
27+
@Override
28+
public DiffMappingBuilder mapper() {
29+
return this;
30+
}
31+
32+
@Override
33+
public DiffMappingBuilder mapping(String field) {
34+
return mapping(field, StringUtils.EMPTY);
35+
}
36+
37+
@Override
38+
public DiffMappingBuilder mapping(String field, String value) {
39+
Method method = DiffReflections.discoverGetter(classMap, field);
40+
DiffStrategyType diffStrategyType = DiffStrategyType.SINGLE;
41+
42+
if (method == null) {
43+
throw new IllegalArgumentException("Method " + field + " not found in class " + classMap.getName());
44+
}
45+
46+
if (value != null && value.split("\\.").length > 1) {
47+
diffStrategyType = DiffStrategyType.DEEP;
48+
}
49+
50+
if (method.getReturnType().isAssignableFrom(Collection.class)) {
51+
diffStrategyType = DiffStrategyType.COLLECTION;
52+
}
53+
54+
metadatas.add(new DiffMetadata(value, method, diffStrategyType));
55+
56+
return this;
57+
}
58+
59+
@Override
60+
public DiffInstanceBuilder builder() {
61+
return this;
62+
}
63+
64+
@Override
65+
public DiffConfiguration getConfiguration() {
66+
return this;
67+
}
68+
69+
@Override
70+
public List<DiffMetadata> getConfigurations() {
71+
return metadatas;
72+
}
73+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
4+
5+
import java.util.List;
6+
7+
public interface DiffConfiguration {
8+
9+
List<DiffMetadata> getConfigurations();
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
interface DiffInstanceBuilder {
4+
5+
DiffMappingBuilder mapper();
6+
7+
DiffConfiguration getConfiguration();
8+
9+
}
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.jonpereiradev.diffobjects.builder;
2+
3+
interface DiffMappingBuilder {
4+
5+
DiffInstanceBuilder builder();
6+
7+
DiffMappingBuilder mapping(String field);
8+
9+
DiffMappingBuilder mapping(String field, String value);
10+
11+
}
12+

src/main/java/com/github/jonpereiradev/diffobjects/strategy/DiffCollectionStrategy.java

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,79 @@
44

55
import java.lang.reflect.Method;
66
import java.util.Collection;
7-
import java.util.Collections;
87
import java.util.Iterator;
98

9+
/**
10+
* Responsible for check the difference between two collections.
11+
*
12+
* @author jonpereiradev@gmail.com
13+
*/
1014
final class DiffCollectionStrategy implements DiffStrategy {
1115

16+
/**
17+
* Check the difference between two objects for the diffMetadata configuration.
18+
*
19+
* @param before object that is considered a state before the after object.
20+
* @param after object that is considered the before object updated.
21+
* @param diffMetadata the diffMetadata that is mapped to make the builder.
22+
* @param <T> the type of object returned by the builder.
23+
* @return the builder result between the two objects.
24+
*/
1225
@Override
13-
public <T> DiffResult<T> diff(Object before, Object after, Method method) {
14-
Collection<T> beforeCollection = DiffReflections.invoke(method, before);
15-
Collection<T> afterCollection = DiffReflections.invoke(method, after);
26+
public <T> DiffResult<T> diff(Object before, Object after, DiffMetadata diffMetadata) {
27+
Collection<?> beforeCollection = initializeCollection(before, diffMetadata.getMethod());
28+
Collection<?> afterCollection = initializeCollection(after, diffMetadata.getMethod());
1629

17-
if (beforeCollection == null) {
18-
beforeCollection = Collections.emptyList();
30+
if (beforeCollection == null && afterCollection == null) {
31+
return new DiffResult<>(null, null, true);
1932
}
2033

21-
if (afterCollection == null) {
22-
afterCollection = Collections.emptyList();
23-
}
24-
25-
boolean isEquals = beforeCollection.size() == afterCollection.size();
26-
27-
if (beforeCollection.size() == afterCollection.size()) {
34+
if (isEqualsSize(beforeCollection, afterCollection)) {
2835
Iterator<?> beforeIterator = beforeCollection.iterator();
2936
Iterator<?> afterIterator = afterCollection.iterator();
30-
DiffStrategy diffStrategy = DiffStrategyType.DEEP.getStrategy();
3137

3238
while (beforeIterator.hasNext() && afterIterator.hasNext()) {
33-
Object beforeObject = beforeIterator.next();
34-
Object afterObject = afterIterator.next();
35-
36-
isEquals = diffStrategy.diff(beforeObject, afterObject, method) == null;
39+
if (diffMetadata.getValue().isEmpty()) {
40+
Object beforeObject = beforeIterator.next();
41+
Object afterObject = afterIterator.next();
3742

38-
if (!isEquals) {
39-
break;
43+
if (!beforeObject.equals(afterObject)) {
44+
return new DiffResult<>((T) beforeCollection, (T) afterCollection, false);
45+
}
4046
}
4147
}
4248
}
4349

44-
return isEquals ? null : new DiffResult<T>(null, null, false);
50+
return new DiffResult<>((T) beforeCollection, (T) afterCollection, true);
51+
}
52+
53+
private boolean isEqualsSize(Collection<?> beforeCollection, Collection<?> afterCollection) {
54+
if (beforeCollection == null && !afterCollection.isEmpty()) {
55+
return false;
56+
}
57+
58+
return beforeCollection != null && afterCollection != null && beforeCollection.isEmpty();
59+
}
60+
61+
/**
62+
* Inicializa a coleção de objetos para verificação do builder.
63+
*
64+
* @param object
65+
* @param method
66+
* @param <T>
67+
* @return
68+
*/
69+
private <T> Collection<T> initializeCollection(Object object, Method method) {
70+
Collection<T> collection = null;
71+
72+
if (object != null) {
73+
collection = DiffReflections.invoke(method, object);
74+
}
75+
76+
if (collection != null && collection.isEmpty()) {
77+
return null;
78+
}
79+
80+
return collection;
4581
}
4682
}

0 commit comments

Comments
 (0)