void (@NotNull CompositePackagingElement<?> parent) { List<PackagingElement<?>> prevChildren = new ArrayList<>(); List<PackagingElement<?>> toRemove = new ArrayList<>(); for (PackagingElement<?> child : parent.getChildren()) { if (child instanceof CompositePackagingElement<?>) { removeDuplicates((CompositePackagingElement<?>)child); } boolean merged = false; for (PackagingElement<?> prevChild : prevChildren) { if (child.isEqualTo(prevChild)) { if (child instanceof CompositePackagingElement<?>) { for (PackagingElement<?> childElement : ((CompositePackagingElement<?>)child).getChildren()) { ((CompositePackagingElement<?>)prevChild).addOrFindChild(childElement); } } merged = true; break; } } if (merged) { toRemove.add(child); } else { prevChildren.add(child); } } for (PackagingElement<?> child : toRemove) { parent.removeChild(child); } }