33import com .github .jonpereiradev .diffobjects .builder .DiffConfiguration ;
44import com .github .jonpereiradev .diffobjects .strategy .DiffMetadata ;
55import com .github .jonpereiradev .diffobjects .strategy .DiffReflections ;
6+ import com .github .jonpereiradev .diffobjects .strategy .DiffStrategy ;
7+ import java .util .ArrayList ;
68
79import java .util .Collections ;
8- import java .util .LinkedList ;
910import java .util .List ;
1011import 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 */
1519public 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 ;
0 commit comments