Skip to content

Commit 15dfa52

Browse files
author
Jonathan Pereira
authored
Merge pull request #5 from jonpereiradev/feature/refactory
Upgrade logic and structure
2 parents 411a1ee + b06d21e commit 15dfa52

53 files changed

Lines changed: 1263 additions & 1142 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ this project in two ways:
1414
<dependency>
1515
<groupId>com.github.jonpereiradev</groupId>
1616
<artifactId>diff-objects</artifactId>
17-
<version>1.1.0</version>
17+
<version>1.3.0</version>
1818
</dependency>
1919
```
2020

@@ -48,24 +48,26 @@ public class Email {
4848
```
4949

5050
```java
51+
import com.github.jonpereiradev.diffobjects.DiffConfig;
52+
5153
public class Main {
52-
54+
5355
public static void main(String[] args) {
5456
User u1 = new User("user1", "123456", LocalDate.now().minusDays(1), true);
5557
User u2 = new User("user2", "12345678", LocalDate.now(), true);
56-
58+
5759
u1.getEmails().add(new Email("user@gmail.com", true));
5860
u2.getEmails().add(new Email("user@gmail.com", false));
59-
60-
DiffConfiguration configuration = DiffBuilder
61-
.map(User.class)
62-
.mapping("login")
63-
.mapping("password")
64-
.mapping("emails", "description")
65-
.configuration();
66-
61+
62+
DiffConfig configuration = DiffBuilder
63+
.map(User.class)
64+
.mapping("login")
65+
.mapping("password")
66+
.mapping("emails", "description")
67+
.configuration();
68+
6769
List<DiffResult> diffs = DiffObjects.forClass(User.class).diff(u1, u2, configuration);
68-
70+
6971
for (DiffResult diff : diffs) {
7072
if (!diff.isEquals()) {
7173
System.out.println("Field: " + diff.getProperties().get("field"));

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.github.jonpereiradev</groupId>
88
<artifactId>diff-objects</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.3.0</version>
1010
<packaging>jar</packaging>
1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<description>API to compare the difference between two objects</description>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.jonpereiradev.diffobjects;
2+
3+
4+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
5+
6+
import java.util.List;
7+
8+
9+
/**
10+
* Contract of the diff config object that maps the properties being check for diff.
11+
*
12+
* @author Jonathan Pereira
13+
* @version 1.2.0
14+
* @since 1.0.0
15+
*/
16+
public interface DiffConfig {
17+
18+
/**
19+
* Build the configuration for the instance.
20+
*
21+
* @return the metadata generated by the instance.
22+
*/
23+
List<DiffMetadata> build();
24+
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.jonpereiradev.diffobjects;
2+
3+
4+
import com.github.jonpereiradev.diffobjects.annotation.DiffOrder;
5+
import com.github.jonpereiradev.diffobjects.builder.DiffBuilderContext;
6+
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
7+
8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
14+
/**
15+
* Responsible for generate the configuration of the instance.
16+
*
17+
* @author Jonathan Pereira
18+
* @since 1.0.0
19+
*/
20+
21+
public final class DiffConfigImpl implements DiffConfig {
22+
23+
private final DiffBuilderContext<?> context;
24+
private final List<DiffMetadata> metadataList;
25+
26+
public DiffConfigImpl(DiffBuilderContext<?> context) {
27+
this.context = context;
28+
this.metadataList = new ArrayList<>(context.getMetadataMap().size());
29+
}
30+
31+
/**
32+
* Gets the configuration for the instance.
33+
*
34+
* @return the metadata generated by the instance.
35+
*/
36+
@Override
37+
public List<DiffMetadata> build() {
38+
if (metadataList.isEmpty()) {
39+
boolean sortable = false;
40+
41+
for (Map.Entry<String, DiffMetadata> entry : context.getMetadataMap().entrySet()) {
42+
DiffOrder annotation = entry.getValue().getMethod().getAnnotation(DiffOrder.class);
43+
44+
if (annotation != null) {
45+
entry.getValue().setOrder(annotation.value());
46+
sortable = true;
47+
}
48+
49+
metadataList.add(entry.getValue());
50+
}
51+
52+
if (sortable) {
53+
Collections.sort(metadataList);
54+
}
55+
}
56+
57+
return metadataList;
58+
}
59+
60+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public class DiffException extends RuntimeException {
1212
public DiffException(String message) {
1313
super(message);
1414
}
15+
1516
}

0 commit comments

Comments
 (0)