class ReorderStepsUtils {

    void renameCreatedColumns(List<AppendStep> appendSteps) {

        final List<String> createdColumns = appendSteps
                .stream()
                .flatMap(step -> step.getDiff().getCreatedColumns().stream())
                .collect(Collectors.toList());

        if (createdColumns.isEmpty()) {
            return;
        }

        //retrieve the minimum index
        final int firstIndex = Integer.parseInt(createdColumns.stream().min(String::compareTo).get());

        // map old created column names to the new ones
        final DecimalFormat format = new DecimalFormat("0000");
        Map<String, String> rename = new HashMap<>();
        IntStream.range(0, createdColumns.size()).forEach(i -> {
            rename.put(createdColumns.get(i), format.format(i + firstIndex));
        });

        // walk over the list of append steps and change names (id) of created columns
        appendSteps.stream().forEach(step -> {
            // first for created columns
            List<String> renamedCreatedColumns = step.getDiff().getCreatedColumns().stream().map(s -> {
                if (rename.containsKey(s)) {
                    return rename.get(s);
                } else {
                    return s;
                }
            }).collect(Collectors.toList());

            // then within actions
            step.getDiff().setCreatedColumns(renamedCreatedColumns);
            for (Action action : step.getActions()) {
                final Map<String, String> parameters = action.getParameters();
                final String columnId = parameters.get(ImplicitParameters.COLUMN_ID.getKey());

                if (rename.containsKey(columnId)) {
                    parameters.put(ImplicitParameters.COLUMN_ID.getKey(), rename.get(columnId));
                }
            }
        });
    }

}