Skip to content

Commit c2bb618

Browse files
Live Preview
1 parent 679ba10 commit c2bb618

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/main/java/com/contentstack/sdk/SuperEntry.java renamed to src/main/java/com/contentstack/sdk/Utils.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import com.google.gson.JsonElement;
44
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
65

6+
import java.lang.reflect.Field;
77
import java.util.Map;
88

9-
// SuperEntry is used as the common class that caould be consumed by Entry and Query
10-
public class SuperEntry {
11-
9+
public class Utils {
1210

1311
/**
1412
* Merge "source" into "target". If fields have equal name, merge them recursively. Null values in source will
@@ -43,15 +41,24 @@ public static JsonObject deepMerge(JsonObject source, JsonObject target) {
4341
}
4442

4543

46-
/**
47-
* simple test
48-
*/
49-
public static void main(String[] args) {
50-
JsonParser parser = new JsonParser();
51-
JsonObject sourse = parser.parse("{offer: {issue1: null, issue2: null}, accept: true, reject: null}").getAsJsonObject();
52-
JsonObject target = parser.parse("{offer: {issue2: value2}, reject: false}").getAsJsonObject();
53-
System.out.println(deepMerge(sourse, target));
44+
public <T> T merge(T local, T remote) throws IllegalAccessException, InstantiationException {
45+
Class<?> clazz = local.getClass();
46+
Object merged = clazz.newInstance();
47+
for (Field field : clazz.getDeclaredFields()) {
48+
field.setAccessible(true);
49+
Object localValue = field.get(local);
50+
Object remoteValue = field.get(remote);
51+
if (localValue != null) {
52+
switch (localValue.getClass().getSimpleName()) {
53+
case "Default":
54+
case "Detail":
55+
field.set(merged, this.merge(localValue, remoteValue));
56+
break;
57+
default:
58+
field.set(merged, (remoteValue != null) ? remoteValue : localValue);
59+
}
60+
}
61+
}
62+
return (T) merged;
5463
}
55-
56-
5764
}

0 commit comments

Comments
 (0)