Skip to content

Commit 48d5075

Browse files
jonpereiradevJonathan Pereira
authored andcommitted
Including diff properties.
1 parent 916740b commit 48d5075

11 files changed

Lines changed: 108 additions & 18 deletions

File tree

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

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

3-
import com.github.jonpereiradev.diffobjects.builder.DiffConfigurationBuilder;
3+
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
44
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
55
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
66

7+
import java.util.Collections;
78
import java.util.LinkedList;
89
import java.util.List;
910
import java.util.Objects;
@@ -51,15 +52,17 @@ public static <T> boolean isEquals(T beforeState, T afterState) {
5152
* @param afterState objeto com as informaçnoes depois da alteração.
5253
* @return resultado do instance.
5354
*/
54-
public static <T> List<DiffResult<?>> diff(T beforeState, T afterState, DiffConfigurationBuilder configuration) {
55+
public static <T> List<DiffResult<?>> diff(T beforeState, T afterState, DiffConfiguration configuration) {
5556
Objects.requireNonNull(beforeState, "Before state is required.");
5657
Objects.requireNonNull(afterState, "After state is required.");
5758
Objects.requireNonNull(configuration, "Configuration is required.");
5859

5960
List<DiffResult<?>> results = new LinkedList<>();
6061

6162
for (DiffMetadata metadata : configuration.build()) {
62-
results.add(metadata.getStrategy().diff(beforeState, afterState, metadata));
63+
DiffResult<Object> diff = metadata.getStrategy().diff(beforeState, afterState, metadata);
64+
diff.setProperties(Collections.unmodifiableMap(metadata.getProperties()));
65+
results.add(diff);
6366
}
6467

6568
return results;
@@ -73,7 +76,7 @@ public static <T> List<DiffResult<?>> diff(T beforeState, T afterState, DiffConf
7376
* @param afterState objeto com as informações depois da alteração.
7477
* @return resultado do instance.
7578
*/
76-
public static <T> boolean isEquals(T beforeState, T afterState, DiffConfigurationBuilder configuration) {
79+
public static <T> boolean isEquals(T beforeState, T afterState, DiffConfiguration configuration) {
7780
Objects.requireNonNull(beforeState, "Before state is required.");
7881
Objects.requireNonNull(afterState, "After state is required.");
7982
Objects.requireNonNull(configuration, "Configuration is required.");

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

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

3+
import java.util.Map;
4+
35
/**
46
* @author jonpereiradev@gmail.com
57
*/
@@ -8,6 +10,7 @@ public class DiffResult<T> {
810
private T before;
911
private T after;
1012
private boolean equals;
13+
private Map<String, String> properties;
1114

1215
public DiffResult(T before, T after, boolean equals) {
1316
this.before = before;
@@ -26,4 +29,12 @@ public T getAfter() {
2629
public boolean isEquals() {
2730
return equals;
2831
}
32+
33+
public Map<String, String> getProperties() {
34+
return properties;
35+
}
36+
37+
public void setProperties(Map<String, String> properties) {
38+
this.properties = properties;
39+
}
2940
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
*/
2222
String value() default "";
2323

24+
/**
25+
* Aditional properties that will be on result for identification.
26+
*/
27+
DiffProperty[] properties() default {};
2428
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.jonpereiradev.diffobjects.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Maps a property on a mapping to be used on result.
10+
*
11+
* @author jonpereiradev@gmail.com
12+
*/
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target({ElementType.ANNOTATION_TYPE})
15+
public @interface DiffProperty {
16+
17+
/**
18+
* Identifier of the property.
19+
*/
20+
String key();
21+
22+
/**
23+
* Value of the property.
24+
*/
25+
String value();
26+
27+
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* @author jonpereiradev@gmail.com
1818
* @see DiffInstanceBuilder
1919
* @see DiffMappingBuilder
20-
* @see DiffConfigurationBuilder
20+
* @see DiffConfiguration
2121
*/
22-
public final class DiffBuilder implements DiffInstanceBuilder, DiffMappingBuilder, DiffConfigurationBuilder {
22+
public final class DiffBuilder implements DiffInstanceBuilder, DiffMappingBuilder, DiffConfiguration {
2323

2424
private final Class<?> classMap;
2525
private final List<DiffMetadata> metadatas;
@@ -120,11 +120,31 @@ public DiffMappingBuilder mapping(String field, String value) {
120120
throw new DiffException("Field \"" + field + "\" already mapped in this builder.");
121121
}
122122

123+
diffMetadata.getProperties().put("field", field);
124+
123125
metadatas.add(diffMetadata);
124126

125127
return this;
126128
}
127129

130+
/**
131+
* Define a property for the last mapping.
132+
*
133+
* @param key the identifier of the property.
134+
* @param value the value of the property.
135+
* @return the instance of this mapping.
136+
*/
137+
@Override
138+
public DiffMappingBuilder property(String key, String value) {
139+
if (metadatas.isEmpty()) {
140+
throw new DiffException("A mapping field is required to associate the property.");
141+
}
142+
143+
metadatas.get(metadatas.size() - 1).getProperties().put(key, value);
144+
145+
return this;
146+
}
147+
128148
/**
129149
* Returns to the instance instance to allow the fluent interface.
130150
*
@@ -141,7 +161,7 @@ public DiffInstanceBuilder instance() {
141161
* @return a configuration instance instance.
142162
*/
143163
@Override
144-
public DiffConfigurationBuilder configuration() {
164+
public DiffConfiguration configuration() {
145165
return this;
146166
}
147167

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffConfigurationBuilder.java renamed to src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @see DiffInstanceBuilder
1212
* @see DiffMappingBuilder
1313
*/
14-
public interface DiffConfigurationBuilder {
14+
public interface DiffConfiguration {
1515

1616
/**
1717
* Gets the configuration for the instance instance.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author jonpereiradev@gmail.com
77
* @see DiffBuilder
88
* @see DiffMappingBuilder
9-
* @see DiffConfigurationBuilder
9+
* @see DiffConfiguration
1010
*/
1111
public interface DiffInstanceBuilder {
1212

@@ -22,7 +22,7 @@ public interface DiffInstanceBuilder {
2222
*
2323
* @return a configuration instance instance.
2424
*/
25-
DiffConfigurationBuilder configuration();
25+
DiffConfiguration configuration();
2626

2727
}
2828

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author jonpereiradev@gmail.com
77
* @see DiffBuilder
88
* @see DiffInstanceBuilder
9-
* @see DiffConfigurationBuilder
9+
* @see DiffConfiguration
1010
*/
1111
public interface DiffMappingBuilder {
1212

@@ -28,7 +28,7 @@ public interface DiffMappingBuilder {
2828
* Maps the getter of the field for the class.
2929
*
3030
* @param field name of the field that will me used to find the getter method.
31-
* @return the instance of this mapping instance.
31+
* @return the instance of this mapping.
3232
*/
3333
DiffMappingBuilder mapping(String field);
3434

@@ -37,9 +37,17 @@ public interface DiffMappingBuilder {
3737
*
3838
* @param field name of the field that will me used to find the getter method.
3939
* @param value the nested property of the object to make the diff.
40-
* @return the instance of this mapping instance.
40+
* @return the instance of this mapping.
4141
*/
4242
DiffMappingBuilder mapping(String field, String value);
4343

44+
/**
45+
* Define a property for the last mapping.
46+
*
47+
* @param key the identifier of the property.
48+
* @param value the value of the property.
49+
* @return the instance of this mapping.
50+
*/
51+
DiffMappingBuilder property(String key, String value);
4452
}
4553

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.apache.commons.lang.StringUtils;
44

55
import java.lang.reflect.Method;
6+
import java.util.HashMap;
7+
import java.util.Map;
68
import java.util.Objects;
79

810
/**
@@ -14,11 +16,13 @@ public final class DiffMetadata implements Comparable<DiffMetadata> {
1416
private final String value;
1517
private final Method method;
1618
private final DiffStrategy strategy;
19+
private final Map<String, String> properties;
1720

1821
public DiffMetadata(String value, Method method, DiffStrategyType diffStrategyType) {
1922
this.value = value == null ? StringUtils.EMPTY : value;
2023
this.method = method;
2124
this.strategy = diffStrategyType == null ? DiffStrategyType.SINGLE.getStrategy() : diffStrategyType.getStrategy();
25+
this.properties = new HashMap<>();
2226
}
2327

2428
@Override
@@ -54,4 +58,8 @@ public Method getMethod() {
5458
public DiffStrategy getStrategy() {
5559
return strategy;
5660
}
61+
62+
public Map<String, String> getProperties() {
63+
return properties;
64+
}
5765
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import com.github.jonpereiradev.diffobjects.DiffException;
44
import com.github.jonpereiradev.diffobjects.annotation.DiffMapping;
55
import com.github.jonpereiradev.diffobjects.annotation.DiffMappings;
6+
import com.github.jonpereiradev.diffobjects.annotation.DiffProperty;
67
import com.github.jonpereiradev.diffobjects.builder.DiffBuilder;
7-
import com.github.jonpereiradev.diffobjects.builder.DiffConfigurationBuilder;
8+
import com.github.jonpereiradev.diffobjects.builder.DiffConfiguration;
89
import com.github.jonpereiradev.diffobjects.builder.DiffInstanceBuilder;
910
import org.apache.commons.lang.StringUtils;
1011
import org.apache.commons.lang.reflect.MethodUtils;
@@ -21,25 +22,33 @@
2122
*/
2223
public final class DiffReflections {
2324

24-
private static final Map<String, DiffConfigurationBuilder> CACHE_MAP = new ConcurrentHashMap<>();
25+
private static final Map<String, DiffConfiguration> CACHE_MAP = new ConcurrentHashMap<>();
2526

2627
/**
2728
* Map the methods of the object that has the annotations for diff and stores in cache.
2829
*
2930
* @param diffClass class that have the diff annotations.
3031
* @return the diff mappings of the class.
3132
*/
32-
public static DiffConfigurationBuilder mapAnnotations(Class<?> diffClass) {
33+
public static DiffConfiguration mapAnnotations(Class<?> diffClass) {
3334
if (!CACHE_MAP.containsKey(diffClass.getName())) {
3435
DiffInstanceBuilder builder = DiffBuilder.map(diffClass);
3536

3637
for (Method method : diffClass.getMethods()) {
3738
if (method.isAnnotationPresent(DiffMapping.class)) {
3839
DiffMapping diffMapping = method.getAnnotation(DiffMapping.class);
3940
builder.mapper().mapping(method.getName(), diffMapping.value());
41+
42+
for (DiffProperty diffProperty : diffMapping.properties()) {
43+
builder.mapper().property(diffProperty.key(), diffProperty.value());
44+
}
4045
} else if (method.isAnnotationPresent(DiffMappings.class)) {
4146
for (DiffMapping diffMapping : method.getAnnotation(DiffMappings.class).value()) {
4247
builder.mapper().mapping(method.getName(), diffMapping.value());
48+
49+
for (DiffProperty diffProperty : diffMapping.properties()) {
50+
builder.mapper().property(diffProperty.key(), diffProperty.value());
51+
}
4352
}
4453
}
4554
}

0 commit comments

Comments
 (0)