private <S extends Shape> Collection<Shape> createUpdatedShapes(
            Model model,
            Collection<Shape> removed,
            Function<Shape, Optional<S>> containerShapeMapper,
            Function<Map.Entry<S, List<MemberShape>>, S> entryMapperAndFactory
    ) {
        Map<S, List<MemberShape>> containerMemberMap = new HashMap<>();
        for (Shape shape : removed) {
            if (!shape.isMemberShape()) {
                continue;
            }

            MemberShape member = (MemberShape) shape;
            Optional<S> container = model.getShape(member.getContainer()).flatMap(containerShapeMapper);
            if (container.isPresent()) {
                containerMemberMap.computeIfAbsent(container.get(), k -> new ArrayList<>()).add(member);
            }
        }

        Collection<Shape> updatedShapes = new ArrayList<>();
        for (Map.Entry<S, List<MemberShape>> entry : containerMemberMap.entrySet()) {
            updatedShapes.add(entryMapperAndFactory.apply(entry));
        }
        return updatedShapes;
    }
Finds structure and union shapes that have had members removed,
converts them to a builder, and then returns new versions of those
shapes without the members.

<p>The removal of members is done using a single transformation of a
shape. For example, if multiple members are removed from a single
structure shape, the structure shape is rebuilt a single time. This is
done by first grouping the members into a map of {@code S} to a list
of members that were removed, allowing the grouped members to be
removed in a single pass.

@param model Model used to get the container.
@param removed The collection of shapes that were removed.
@param containerShapeMapper A function that accepts a shape and tries
 to convert it to {@code S}.
@param entryMapperAndFactory A function that takes {@code S} and create
 a new version without the members.
@param <S> The shape type being transformed.
@return Returns a list of shapes that need to be modified in the model.
smithy-model/src/main/java/software/amazon/smithy/model/transform/plugins/CleanStructureAndUnionMembers.java