Skip to content

Commit 7069f19

Browse files
author
Jonathan Pereira
committed
Merge development
2 parents 7cd9d70 + 9913bbd commit 7069f19

25 files changed

Lines changed: 349 additions & 199 deletions

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

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

3+
/**
4+
* Exception for any generic error found in the diff execution.
5+
*
6+
* @author Jonathan Pereira
7+
* @since 1.0
8+
*/
39
public class DiffException extends RuntimeException {
410

511
public DiffException(String message) {

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

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@
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
/**
13-
* @author jonpereiradev@gmail.com
14+
* Responsible for execute the diff between two objects.
15+
*
16+
* @author Jonathan Pereira
17+
* @since 1.0
1418
*/
1519
public final class DiffObjects {
1620

21+
private DiffObjects() {
22+
throw new UnsupportedOperationException("No need to create an instance of this class.");
23+
}
24+
1725
/**
18-
* Executa o instance entre o antes e o depois.
26+
* Execute the diff between two objects using annotations.
1927
*
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.
28+
* @param <T> type of object been compared.
29+
* @param beforeState the before object state to compare with after object.
30+
* @param afterState the after object state to compare with befor object.
31+
*
32+
* @return a list with the results of the diff.
2433
*/
2534
public static <T> List<DiffResult> diff(T beforeState, T afterState) {
2635
Objects.requireNonNull(beforeState, "Before state is required.");
@@ -30,59 +39,69 @@ public static <T> List<DiffResult> diff(T beforeState, T afterState) {
3039
}
3140

3241
/**
33-
* Verifica se os objetos são iguais no instance.
42+
* Execute the diff between two objects using a configuration.
3443
*
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.
44+
* @param <T> type of object been compared.
45+
* @param beforeState the before object state to compare with after object.
46+
* @param afterState the after object state to compare with befor object.
47+
* @param configuration the configuration of the diff.
4948
*
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.
49+
* @return a list with the results of the diff.
5450
*/
5551
public static <T> List<DiffResult> diff(T beforeState, T afterState, DiffConfiguration configuration) {
5652
Objects.requireNonNull(beforeState, "Before state is required.");
5753
Objects.requireNonNull(afterState, "After state is required.");
5854
Objects.requireNonNull(configuration, "Configuration is required.");
5955

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

62-
for (DiffMetadata metadata : configuration.build()) {
63-
DiffResult diff = metadata.getStrategy().diff(beforeState, afterState, metadata);
6463
diff.setProperties(Collections.unmodifiableMap(metadata.getProperties()));
64+
6565
results.add(diff);
6666
}
6767

6868
return results;
6969
}
7070

7171
/**
72-
* Verifica se os objetos são iguais no instance.
72+
* Check if exists any difference between the two objects using annotations.
73+
*
74+
* @param <T> type of object been compared.
75+
* @param beforeState the before object state to compare with after object.
76+
* @param afterState the after object state to compare with befor object.
77+
*
78+
* @return {@code true} if no difference exists between the objects or {@code false} otherwise.
79+
*/
80+
public static <T> boolean isEquals(T beforeState, T afterState) {
81+
Objects.requireNonNull(beforeState, "Before state is required.");
82+
Objects.requireNonNull(afterState, "After state is required.");
83+
84+
return isEquals(beforeState, afterState, DiffReflections.mapAnnotations(beforeState.getClass()));
85+
}
86+
87+
/**
88+
* Check if exists any difference between the two objects.
89+
*
90+
* @param <T> type of object been compared.
91+
* @param beforeState the before object state to compare with after object.
92+
* @param afterState the after object state to compare with befor object.
93+
* @param configuration the configuration of the diff.
7394
*
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.
95+
* @return {@code true} if no difference exists between the objects or {@code false} otherwise.
7896
*/
7997
public static <T> boolean isEquals(T beforeState, T afterState, DiffConfiguration configuration) {
8098
Objects.requireNonNull(beforeState, "Before state is required.");
8199
Objects.requireNonNull(afterState, "After state is required.");
82100
Objects.requireNonNull(configuration, "Configuration is required.");
83101

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

87106
if (!result.isEquals()) {
88107
return false;

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

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

55
/**
6-
* @author jonpereiradev@gmail.com
6+
* Result of a diff with the before and after state.
7+
*
8+
* @author Jonathan Pereira
9+
* @since 1.0
710
*/
8-
public class DiffResult {
11+
public final class DiffResult {
912

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

1518
public DiffResult(Object before, Object after, boolean equals) {
@@ -34,7 +37,7 @@ public Map<String, String> getProperties() {
3437
return properties;
3538
}
3639

37-
public void setProperties(Map<String, String> properties) {
40+
void setProperties(Map<String, String> properties) {
3841
this.properties = properties;
3942
}
4043
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
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+
* A method annotated with this will be checked for difference between two objects. When annotated on a method without
10+
* value(), the equals method of this object will be executed. When annotated on a method with value(), the property
11+
* will be evaluated for equality.
1212
*
13-
* @author jonpereiradev@gmail.com
13+
* @author Jonathan Pereira
14+
* @since 1.0
1415
*/
1516
@Retention(RetentionPolicy.RUNTIME)
16-
@Target({ElementType.METHOD, ElementType.TYPE})
17+
@Target({ElementType.METHOD})
1718
public @interface DiffMapping {
1819

1920
/**
20-
* Defines the property that will be evaluated for equality. It can be nested property like user.address.id.
21+
* @return Defines the property that will be evaluated for equality. It can be nested property like user.address.id.
2122
*/
2223
String value() default "";
2324

2425
/**
25-
* Aditional properties that will be on result for identification.
26+
* @return Aditional properties that will be on the diff result object.
2627
*/
2728
DiffProperty[] properties() default {};
2829
}

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

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

88
/**
9-
* @author jonpereiradev@gmail.com
9+
* Annotation to group multiple mappings on a method or to enable all methods if is used on a class.
10+
*
11+
* @author Jonathan Pereira
12+
* @since 1.0
1013
*/
1114
@Retention(RetentionPolicy.RUNTIME)
12-
@Target({ElementType.FIELD, ElementType.METHOD})
15+
@Target({ElementType.METHOD, ElementType.TYPE})
1316
public @interface DiffMappings {
1417

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

1723
}

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

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

88
/**
9-
* @author jonpereiradev@gmail.com
10-
* @see <a href="#">https://github.com/jonpereiradev/diff-objects</a>
9+
* Defines an order for the method if necessary.
10+
*
11+
* @author Jonathan Pereira
12+
* @since 1.0
1113
*/
1214
@Retention(RetentionPolicy.RUNTIME)
1315
@Target({ElementType.FIELD, ElementType.METHOD})
1416
public @interface DiffOrder {
1517

18+
/**
19+
* @return a number order for the method.
20+
*/
1621
int value();
1722

1823
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
/**
99
* Maps a property on a mapping to be used on result.
1010
*
11-
* @author jonpereiradev@gmail.com
11+
* @author Jonathan Pereira
12+
* @since 1.0
1213
*/
1314
@Retention(RetentionPolicy.RUNTIME)
1415
@Target({ElementType.ANNOTATION_TYPE})
1516
public @interface DiffProperty {
1617

1718
/**
18-
* Identifier of the property.
19+
* @return identifier of the property.
1920
*/
2021
String key();
2122

2223
/**
23-
* Value of the property.
24+
* @return value of the property.
2425
*/
2526
String value();
2627

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
/**
1111
* Responsible to map a class and fields to be able to generate diffs.
1212
*
13-
* @author jonpereiradev@gmail.com
13+
* @author Jonathan Pereira
14+
* @since 1.0
15+
*
1416
* @see DiffInstanceBuilder
1517
* @see DiffMappingBuilder
1618
* @see DiffConfiguration

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/**
88
* Responsible for generate the configuration of the instance.
99
*
10+
* @author Jonathan Pereira
11+
* @since 1.0
12+
*
1013
* @see DiffBuilder
1114
* @see DiffInstanceBuilder
1215
* @see DiffMappingBuilder

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

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

11+
/**
12+
* Responsible for generate the configuration of the instance.
13+
*
14+
* @author Jonathan Pereira
15+
* @since 1.0
16+
*
17+
* @see DiffBuilder
18+
* @see DiffInstanceBuilder
19+
* @see DiffMappingBuilder
20+
*/
1121
final class DiffConfigurationImpl implements DiffConfiguration {
1222

1323
private final Map<String, DiffMetadata> metadatas;

0 commit comments

Comments
 (0)