/** * Returns a deep clone of the given dictionary. A deep clone will attempt to clone the keys and * values (deeply) of this dictionary as well as the dictionary itself. * * @param dict the dictionary to clone * @param onlyCollections if true, only collections in this dictionary will be cloned, not * individual values * @return a deep clone of dict */ public static <K, V> NSDictionary<K, V> deepClone( NSDictionary<K, V> dict, boolean onlyCollections) { NSMutableDictionary<K, V> clonedDict = null; if (dict != null) { clonedDict = dict.mutableClone(); for (K key : dict.allKeys()) { V value = dict.objectForKey(key); K cloneKey = ERXUtilities.deepClone(key, onlyCollections); V cloneValue = ERXUtilities.deepClone(value, onlyCollections); if (cloneKey != key) { clonedDict.removeObjectForKey(key); if (cloneValue != null) { clonedDict.setObjectForKey(cloneValue, cloneKey); } } else if (cloneValue != null) { if (cloneValue != value) { clonedDict.setObjectForKey(cloneValue, cloneKey); } } else { clonedDict.removeObjectForKey(key); } } } return clonedDict; }
/** * Returns a deep clone of the given set. A deep clone will attempt to clone the values of this * set as well as the set itself. * * @param <T> class of set elements * @param set the set to clone * @param onlyCollections if true, only collections in this array will be cloned, not individual * values * @return a deep clone of set */ public static <T> NSSet<T> deepClone(NSSet<T> set, boolean onlyCollections) { if (set == null) { return null; } NSMutableSet<T> clonedSet = set.mutableClone(); for (T value : set) { T clonedValue = ERXUtilities.deepClone(value, onlyCollections); if (clonedValue != null) { if (clonedValue != value) { clonedSet.remove(value); clonedSet.add(clonedValue); } } else { clonedSet.remove(value); } } return clonedSet; }