void (@NotNull PackagingElement<?> child) { this.update( () -> myAddFirstChild(child), (builder, packagingElementEntity) -> { MutableExternalEntityMapping<PackagingElement<?>> mapping = builder.getMutableExternalMapping("intellij.artifacts.packaging.elements"); CompositePackagingElementEntity entity = (CompositePackagingElementEntity)packagingElementEntity; List<Pair<PackagingElementEntity, PackagingElement<?>>> pairs = new ArrayList<>(ContainerUtil.map(entity.getChildren().iterator(), o -> { PackagingElement<?> data = mapping.getDataByEntity(o); if (data == null) { return new Pair<>(o, myPackagingElementInitializer.initialize(o, myProject, builder)); } return new Pair<>(o, data); })); PackagingElementEntity childEntity = (PackagingElementEntity)child.getOrAddEntity(builder, entity.getEntitySource(), myProject); pairs.add(0, new Pair<>(childEntity, child)); for (int i = 1; i < pairs.size(); i++) { Pair<PackagingElementEntity, PackagingElement<?>> pair = pairs.get(i); PackagingElement<?> element = pair.getSecond(); if (element.isEqualTo(child)) { if (element instanceof CompositePackagingElement<?>) { ((CompositePackagingElement<?>)child).addOrFindChildren(((CompositePackagingElement<?>)element).getChildren()); } pairs.remove(i); break; } } List<PackagingElementEntity> newChildren = ContainerUtil.map(pairs, o -> o.getFirst()); //noinspection unchecked builder.modifyEntity(CompositePackagingElementEntity.Builder.class, entity, o -> { //noinspection unchecked o.setChildren(newChildren); return Unit.INSTANCE; }); // Set storage for the new child setStorageForPackagingElement(child); } ); }