diff --git a/src/main/java/spoon/support/visitor/equals/CloneHelper.java b/src/main/java/spoon/support/visitor/equals/CloneHelper.java index ec3df4b4b9c..f2ff4d6c753 100644 --- a/src/main/java/spoon/support/visitor/equals/CloneHelper.java +++ b/src/main/java/spoon/support/visitor/equals/CloneHelper.java @@ -55,7 +55,7 @@ public Collection clone(Collection elements) { } Collection others = new ArrayList<>(); for (T element : elements) { - others.add(clone(element)); + addClone(others, element); } return others; } @@ -69,7 +69,7 @@ public List clone(List elements) { } List others = new ArrayList<>(); for (T element : elements) { - others.add(clone(element)); + addClone(others, element); } return others; } @@ -100,7 +100,7 @@ public Set clone(Set elements) { Set others = createRightSet(elements); for (T element : elements) { - others.add(clone(element)); + addClone(others, element); } return others; } @@ -111,8 +111,27 @@ public Map clone(Map elements) { } Map others = new HashMap<>(); for (Map.Entry tEntry : elements.entrySet()) { - others.put(tEntry.getKey(), clone(tEntry.getValue())); + addClone(others, tEntry.getKey(), tEntry.getValue()); } return others; } + + /** + * clones a element and adds it's clone as value into targetCollection + * @param targetCollection - the collection which will receive a clone of element + * @param element to be cloned element + */ + protected void addClone(Collection targetCollection, T element) { + targetCollection.add(clone(element)); + } + + /** + * clones a value and adds it's clone as value into targetMap under key + * @param targetMap - the Map which will receive a clone of value + * @param key the target key, which has to be used to add cloned value into targetMap + * @param value to be cloned element + */ + protected void addClone(Map targetMap, String key, T value) { + targetMap.put(key, clone(value)); + } }