Skip to content

Commit a340c6d

Browse files
committed
Documentation in english and nested value in collections mapping.
1 parent 08e511e commit a340c6d

21 files changed

Lines changed: 246 additions & 126 deletions

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

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

3+
/**
4+
* Exception for any generic error found in the diff execution.
5+
*
6+
* @author jonpereiradev@gmail.com
7+
*/
38
public class DiffException extends RuntimeException {
49

510
public DiffException(String message) {

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

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,32 @@
33
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
44
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
55
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
6+
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategy;
7+
import java.util.ArrayList;
68

79
import java.util.Collections;
8-
import java.util.LinkedList;
910
import java.util.List;
1011
import java.util.Objects;
1112

1213
/**
14+
* Responsible for execute the diff between two objects.
15+
*
1316
* @author jonpereiradev@gmail.com
1417
*/
1518
public final class DiffObjects {
1619

20+
private DiffObjects() {
21+
throw new UnsupportedOperationException("No need to create an instance of this class.");
22+
}
23+
1724
/**
18-
* Executa o instance entre o antes e o depois.
25+
* Execute the diff between two objects using annotations.
1926
*
20-
* @param <T> tipo do objeto comparado.
21-
* @param beforeState objeto com as informações antes da alteração.
22-
* @param afterState objeto com as informaçnoes depois da alteração.
23-
* @return resultado do instance.
27+
* @param <T> type of object been compared.
28+
* @param beforeState the before object state to compare with after object.
29+
* @param afterState the after object state to compare with befor object.
30+
*
31+
* @return a list with the results of the diff.
2432
*/
2533
public static <T> List<DiffResult> diff(T beforeState, T afterState) {
2634
Objects.requireNonNull(beforeState, "Before state is required.");
@@ -30,59 +38,69 @@ public static <T> List<DiffResult> diff(T beforeState, T afterState) {
3038
}
3139

3240
/**
33-
* Verifica se os objetos são iguais no instance.
41+
* Execute the diff between two objects using a configuration.
3442
*
35-
* @param <T> tipo do objeto comparado.
36-
* @param beforeState objeto com as informações antes da alteração.
37-
* @param afterState objeto com as informações depois da alteração.
38-
* @return resultado do instance.
39-
*/
40-
public static <T> boolean isEquals(T beforeState, T afterState) {
41-
Objects.requireNonNull(beforeState, "Before state is required.");
42-
Objects.requireNonNull(afterState, "After state is required.");
43-
44-
return isEquals(beforeState, afterState, DiffReflections.mapAnnotations(beforeState.getClass()));
45-
}
46-
47-
/**
48-
* Executa o instance entre o antes e o depois.
43+
* @param <T> type of object been compared.
44+
* @param beforeState the before object state to compare with after object.
45+
* @param afterState the after object state to compare with befor object.
46+
* @param configuration the configuration of the diff.
4947
*
50-
* @param <T> tipo do objeto comparado.
51-
* @param beforeState objeto com as informações antes da alteração.
52-
* @param afterState objeto com as informaçnoes depois da alteração.
53-
* @return resultado do instance.
48+
* @return a list with the results of the diff.
5449
*/
5550
public static <T> List<DiffResult> diff(T beforeState, T afterState, DiffConfiguration configuration) {
5651
Objects.requireNonNull(beforeState, "Before state is required.");
5752
Objects.requireNonNull(afterState, "After state is required.");
5853
Objects.requireNonNull(configuration, "Configuration is required.");
5954

60-
List<DiffResult> results = new LinkedList<>();
55+
List<DiffMetadata> metadatas = configuration.build();
56+
List<DiffResult> results = new ArrayList<>(metadatas.size());
57+
58+
for (DiffMetadata metadata : metadatas) {
59+
DiffStrategy strategy = metadata.getStrategy();
60+
DiffResult diff = strategy.diff(beforeState, afterState, metadata);
6161

62-
for (DiffMetadata metadata : configuration.build()) {
63-
DiffResult diff = metadata.getStrategy().diff(beforeState, afterState, metadata);
6462
diff.setProperties(Collections.unmodifiableMap(metadata.getProperties()));
63+
6564
results.add(diff);
6665
}
6766

6867
return results;
6968
}
7069

7170
/**
72-
* Verifica se os objetos são iguais no instance.
71+
* Check if exists any difference between the two objects using annotations.
72+
*
73+
* @param <T> type of object been compared.
74+
* @param beforeState the before object state to compare with after object.
75+
* @param afterState the after object state to compare with befor object.
76+
*
77+
* @return {@code true} if no difference exists between the objects or {@code false} otherwise.
78+
*/
79+
public static <T> boolean isEquals(T beforeState, T afterState) {
80+
Objects.requireNonNull(beforeState, "Before state is required.");
81+
Objects.requireNonNull(afterState, "After state is required.");
82+
83+
return isEquals(beforeState, afterState, DiffReflections.mapAnnotations(beforeState.getClass()));
84+
}
85+
86+
/**
87+
* Check if exists any difference between the two objects.
88+
*
89+
* @param <T> type of object been compared.
90+
* @param beforeState the before object state to compare with after object.
91+
* @param afterState the after object state to compare with befor object.
92+
* @param configuration the configuration of the diff.
7393
*
74-
* @param <T> tipo do objeto comparado.
75-
* @param beforeState objeto com as informações antes da alteração.
76-
* @param afterState objeto com as informações depois da alteração.
77-
* @return resultado do instance.
94+
* @return {@code true} if no difference exists between the objects or {@code false} otherwise.
7895
*/
7996
public static <T> boolean isEquals(T beforeState, T afterState, DiffConfiguration configuration) {
8097
Objects.requireNonNull(beforeState, "Before state is required.");
8198
Objects.requireNonNull(afterState, "After state is required.");
8299
Objects.requireNonNull(configuration, "Configuration is required.");
83100

84101
for (DiffMetadata metadata : configuration.build()) {
85-
DiffResult result = metadata.getStrategy().diff(beforeState, afterState, metadata);
102+
DiffStrategy strategy = metadata.getStrategy();
103+
DiffResult result = strategy.diff(beforeState, afterState, metadata);
86104

87105
if (!result.isEquals()) {
88106
return false;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import java.util.Map;
44

55
/**
6+
* Result of a diff with the before and after state.
7+
*
68
* @author jonpereiradev@gmail.com
79
*/
8-
public class DiffResult {
10+
public final class DiffResult {
911

10-
private Object before;
11-
private Object after;
12-
private boolean equals;
12+
private final Object before;
13+
private final Object after;
14+
private final boolean equals;
1315
private Map<String, String> properties;
1416

1517
public DiffResult(Object before, Object after, boolean equals) {
@@ -34,7 +36,7 @@ public Map<String, String> getProperties() {
3436
return properties;
3537
}
3638

37-
public void setProperties(Map<String, String> properties) {
39+
void setProperties(Map<String, String> properties) {
3840
this.properties = properties;
3941
}
4042
}

src/main/java/com/github/jonpereiradev/diffobjects/annotation/DiffMapping.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
import java.lang.annotation.Target;
77

88
/**
9-
* <p>A property or object annotated with this will be checked for difference between two objects.</p>
10-
* <p>When annotated on a object without value(), the equals method of this object will be executed.</p>
11-
* <p>When annotated on a object with value(), the property will be evaluated for equality.</p>
9+
* <p>A method annotated with this will be checked for difference between two objects.</p>
10+
* <p>When annotated on a method without value(), the equals method of this object will be executed.</p>
11+
* <p>When annotated on a method with value(), the property will be evaluated for equality.</p>
1212
*
1313
* @author jonpereiradev@gmail.com
1414
*/
1515
@Retention(RetentionPolicy.RUNTIME)
16-
@Target({ElementType.METHOD, ElementType.TYPE})
16+
@Target({ElementType.METHOD})
1717
public @interface DiffMapping {
1818

1919
/**
20-
* Defines the property that will be evaluated for equality. It can be nested property like user.address.id.
20+
* @return Defines the property that will be evaluated for equality. It can be nested property like user.address.id.
2121
*/
2222
String value() default "";
2323

2424
/**
25-
* Aditional properties that will be on result for identification.
25+
* @return Aditional properties that will be on the diff result object.
2626
*/
2727
DiffProperty[] properties() default {};
2828
}

src/main/java/com/github/jonpereiradev/diffobjects/annotation/DiffMappings.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import java.lang.annotation.Target;
77

88
/**
9+
* Annotation to group multiple mappings on a method or to enable all methods if is used on a class.
10+
*
911
* @author jonpereiradev@gmail.com
1012
*/
1113
@Retention(RetentionPolicy.RUNTIME)
12-
@Target({ElementType.FIELD, ElementType.METHOD})
14+
@Target({ElementType.METHOD, ElementType.TYPE})
1315
public @interface DiffMappings {
1416

15-
DiffMapping[] value();
17+
/**
18+
* @return a group of mappings to make multiple diffs on a single method.
19+
*/
20+
DiffMapping[] value() default {};
1621

1722
}

src/main/java/com/github/jonpereiradev/diffobjects/annotation/DiffOrder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import java.lang.annotation.Target;
77

88
/**
9+
* Defines an order for the method if necessary.
10+
*
911
* @author jonpereiradev@gmail.com
10-
* @see <a href="#">https://github.com/jonpereiradev/diff-objects</a>
1112
*/
1213
@Retention(RetentionPolicy.RUNTIME)
1314
@Target({ElementType.FIELD, ElementType.METHOD})
1415
public @interface DiffOrder {
1516

17+
/**
18+
* @return a number order for the method.
19+
*/
1620
int value();
1721

1822
}

src/main/java/com/github/jonpereiradev/diffobjects/annotation/DiffProperty.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
public @interface DiffProperty {
1616

1717
/**
18-
* Identifier of the property.
18+
* @return identifier of the property.
1919
*/
2020
String key();
2121

2222
/**
23-
* Value of the property.
23+
* @return value of the property.
2424
*/
2525
String value();
2626

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
/**
12+
* Responsible for generate the configuration of the instance.
13+
*
14+
* @see DiffBuilder
15+
* @see DiffInstanceBuilder
16+
* @see DiffMappingBuilder
17+
*/
1118
final class DiffConfigurationImpl implements DiffConfiguration {
1219

1320
private final Map<String, DiffMetadata> metadatas;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Builder with the methods of a instance instance.
55
*
66
* @author jonpereiradev@gmail.com
7+
*
78
* @see DiffBuilder
89
* @see DiffMappingBuilder
910
* @see DiffConfiguration
@@ -33,4 +34,3 @@ public interface DiffInstanceBuilder {
3334
DiffConfiguration configuration();
3435

3536
}
36-

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Builder responsible for mapping the fields of a class to create a configuration of diff.
55
*
66
* @author jonpereiradev@gmail.com
7+
*
78
* @see DiffBuilder
89
* @see DiffInstanceBuilder
910
* @see DiffConfiguration
@@ -28,6 +29,7 @@ public interface DiffMappingBuilder {
2829
* Maps the getter of the field for the class.
2930
*
3031
* @param field name of the field that will me used to find the getter method.
32+
*
3133
* @return the instance of this mapping.
3234
*/
3335
DiffQueryBuilder mapping(String field);
@@ -37,6 +39,7 @@ public interface DiffMappingBuilder {
3739
*
3840
* @param field name of the field that will me used to find the getter method.
3941
* @param value the nested property of the object to make the diff.
42+
*
4043
* @return the instance of this mapping.
4144
*/
4245
DiffQueryBuilder mapping(String field, String value);
@@ -45,9 +48,9 @@ public interface DiffMappingBuilder {
4548
* Remove a mapping of the field for the class.
4649
*
4750
* @param field name of the field that will me used to remove.
51+
*
4852
* @return the instance of this mapping.
4953
*/
5054
DiffMappingBuilder unmapping(String field);
5155

5256
}
53-

0 commit comments

Comments
 (0)