001package com.gigya.android.sdk.utils;
002
003import android.support.annotation.NonNull;
004import android.support.annotation.Nullable;
005
006import com.google.gson.Gson;
007
008import org.json.JSONArray;
009import org.json.JSONException;
010import org.json.JSONObject;
011
012import java.util.ArrayList;
013import java.util.HashMap;
014import java.util.HashSet;
015import java.util.Iterator;
016import java.util.List;
017import java.util.Map;
018import java.util.Set;
019
020
021public class ObjectUtils {
022
023    public static <T> T firstNonNull(@Nullable T first, @NonNull T second) {
024        return first != null ? first : second;
025    }
026
027    public static <T, V> boolean safeEquals(T first, V second) {
028        if (first != null && second != null) {
029            return first.equals(second);
030        }
031        return false;
032    }
033
034    public static <T,V> boolean nullAllowedEquals(T first, V second) {
035        if (first != null && second != null) {
036            return first.equals(second);
037        }
038        if (first == null && second == null) {
039            return true;
040        }
041        return false;
042    }
043
044    public static Map<String, Object> toMap(JSONObject object) throws JSONException {
045        Map<String, Object> map = new HashMap<>();
046
047        Iterator<String> keysItr = object.keys();
048        while (keysItr.hasNext()) {
049            String key = keysItr.next();
050            Object value = object.get(key);
051
052            if (value instanceof JSONArray) {
053                value = toList((JSONArray) value);
054            } else if (value instanceof JSONObject) {
055                value = toMap((JSONObject) value);
056            }
057            map.put(key, value);
058        }
059        return map;
060    }
061
062    private static List<Object> toList(JSONArray array) throws JSONException {
063        List<Object> list = new ArrayList<>();
064        for (int i = 0; i < array.length(); i++) {
065            Object value = array.get(i);
066            if (value instanceof JSONArray) {
067                value = toList((JSONArray) value);
068            } else if (value instanceof JSONObject) {
069                value = toMap((JSONObject) value);
070            }
071            list.add(value);
072        }
073        return list;
074    }
075
076    public static Map<String, Object> objectDifference(Map<String, Object> original, Map<String, Object> updated) {
077        Map<String, Object> result = new HashMap<>();
078        if (original == null || updated == null) {
079            return result;
080        }
081
082        // Check removed items.
083
084        Set<Map.Entry<String, Object>> filter = original.entrySet();
085        for (Map.Entry<String, Object> entry : updated.entrySet()) {
086            if (!filter.contains(entry)) {
087                result.put(entry.getKey(), entry.getValue());
088            }
089        }
090
091        for (Map.Entry<String, Object> item : result.entrySet()) {
092            final String key = item.getKey();
093            final Object value = item.getValue();
094            if (value instanceof Map) {
095                if (updated.get(key) != null && original.get(key) != null) {
096                    Map<String, Object> childResult = objectDifference((Map<String, Object>) original.get(key), (Map<String, Object>) updated.get(key));
097                    result.put(key, childResult);
098                }
099            }
100        }
101        return result;
102    }
103
104    public static List<String> mergeRemovingDuplicates(List<String> first, List<String> second) {
105        Set<String> set = new HashSet<>(first);
106        set.addAll(second);
107        return new ArrayList<>(set);
108    }
109
110    public static <T> T deepCopy(Gson gson, T obj, Class<T> clazz) {
111        final String json = gson.toJson(obj);
112        return gson.fromJson(json, clazz);
113    }
114
115    public static String commaConcat(String[] input) {
116        if (input.length > 0) {
117            StringBuilder concat = new StringBuilder();
118            for (String string : input) {
119                concat.append(string).append(",");
120            }
121
122            concat.deleteCharAt(concat.length() - 1);
123            return concat.toString();
124        }
125        return "";
126    }
127}