<commit_before>// Copyright © 2012 <NAME> <www.solita.fi>
// This software is released under the MIT License.
// The license text is at http://opensource.org/licenses/MIT

package fi.solita.jsonmigraine;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.*;

import java.util.List;

public class UpgradeOrderDecider {

    private final UpgraderInvoker invoker;
    private final UpgraderProvider provider;

    public UpgradeOrderDecider(UpgraderInvoker invoker, UpgraderProvider provider) {
        this.invoker = invoker;
        this.provider = provider;
    }

    public JsonNode upgrade(JsonNode data, DataVersions from, HowToUpgrade how) {
        for (UpgradeStep step : how.steps) {
            Class<?> dataType = step.getDataType();
            List<String> path = step.getPath();
            if (dataType.isArray()) {
                dataType = dataType.getComponentType();
            }
            int dataVersion = from.getVersion(dataType);
            Upgrader upgrader = provider.getUpgrader(dataType);
            if (path.isEmpty()) {
                data = upgrade(data, dataVersion, upgrader);
            } else {
                String fieldName = path.get(0);
                data = upgradeField((ObjectNode) data, dataVersion, upgrader, fieldName);
            }
        }
        return data;
    }

    private ObjectNode upgradeField(ObjectNode container, int dataVersion, Upgrader upgrader, String fieldName) {
        JsonNode original = container.get(fieldName);
        try {

            JsonNode upgraded;
            if (original instanceof ArrayNode) {
                upgraded = upgradeArray((ArrayNode) original, dataVersion, upgrader);
            } else {
                upgraded = upgrade(original, dataVersion, upgrader); // XXX: not tested
            }

            container.put(fieldName, upgraded);  // XXX: not tested
        } catch (ValueRemovedException e) {
            container.remove(fieldName);
        }
        return container;
    }

    private ArrayNode upgradeArray(ArrayNode values, int dataVersion, Upgrader upgrader) {
        ArrayNode results = JsonNodeFactory.instance.arrayNode();
        for (JsonNode value : values) {
            try {
                JsonNode upgraded = upgrade(value, dataVersion, upgrader);
                results.add(upgraded);
            } catch (ValueRemovedException e) {
                // removed
            }
        }
        return results;
    }

    private JsonNode upgrade(JsonNode data, int dataVersion, Upgrader upgrader) {
        int latestVersion = upgrader.version();
        if (dataVersion > latestVersion) {
            throw new IllegalArgumentException("The data is newer than the upgrader. " +
                    "The data had version " + dataVersion + ", but the upgrader had version " + latestVersion + ".");
        }
        while (dataVersion < latestVersion) {
            data = invoker.upgrade(data, dataVersion, upgrader);
            dataVersion++;
        }
        return data;
    }
}
<commit_msg>Refactored<commit_after>// Copyright © 2012 <NAME> <www.solita.fi>
// This software is released under the MIT License.
// The license text is at http://opensource.org/licenses/MIT

package fi.solita.jsonmigraine;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.*;

import java.util.List;

public class UpgradeOrderDecider {

    private final UpgraderInvoker invoker;
    private final UpgraderProvider provider;

    public UpgradeOrderDecider(UpgraderInvoker invoker, UpgraderProvider provider) {
        this.invoker = invoker;
        this.provider = provider;
    }

    public JsonNode upgrade(JsonNode data, DataVersions from, HowToUpgrade how) {
        for (UpgradeStep step : how.steps) {
            Class<?> dataType = step.getDataType();
            List<String> path = step.getPath();
            String fieldName = path.isEmpty() ? null : path.get(0);
            if (dataType.isArray()) {
                dataType = dataType.getComponentType();
            }
            int dataVersion = from.getVersion(dataType);
            Upgrader upgrader = provider.getUpgrader(dataType);

            if (fieldName == null) {
                data = upgradeWholeObject(data, dataVersion, upgrader);
            } else if (step.getDataType().isArray()) {
                data = upgradeArrayField((ObjectNode) data, fieldName, dataVersion, upgrader);
            } else {
                data = upgradeField((ObjectNode) data, fieldName, dataVersion, upgrader);
            }
        }
        return data;
    }

    private ObjectNode upgradeField(ObjectNode container, String fieldName, int dataVersion, Upgrader upgrader) {
        JsonNode original = container.get(fieldName);
        try {
            JsonNode upgraded = upgradeWholeObject(original, dataVersion, upgrader);
            container.put(fieldName, upgraded);
        } catch (ValueRemovedException e) {
            container.remove(fieldName);
        }
        return container;
    }

    private JsonNode upgradeArrayField(ObjectNode container, String fieldName, int dataVersion, Upgrader upgrader) {
        JsonNode original = container.get(fieldName);
        if (!original.isNull()) {
            JsonNode upgraded = upgradeArray((ArrayNode) original, dataVersion, upgrader);
            container.put(fieldName, upgraded);
        }
        return container;
    }

    private ArrayNode upgradeArray(ArrayNode values, int dataVersion, Upgrader upgrader) {
        ArrayNode results = JsonNodeFactory.instance.arrayNode();
        for (JsonNode original : values) {
            try {
                JsonNode upgraded = upgradeWholeObject(original, dataVersion, upgrader);
                results.add(upgraded);
            } catch (ValueRemovedException e) {
                // removed; don't add to results
            }
        }
        return results;
    }

    private JsonNode upgradeWholeObject(JsonNode data, int dataVersion, Upgrader upgrader) {
        int latestVersion = upgrader.version();
        if (dataVersion > latestVersion) {
            throw new IllegalArgumentException("The data is newer than the upgrader. " +
                    "The data had version " + dataVersion + ", but the upgrader had version " + latestVersion + ".");
        }
        while (dataVersion < latestVersion) {
            data = invoker.upgrade(data, dataVersion, upgrader);
            dataVersion++;
        }
        return data;
    }
}

{'fraction_non_alphanumeric': 0.09039200614911606, 'fraction_numerical': 0.0015372790161414297, 'mean_word_length': 2.2595190380761525, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5010827', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1648, 'n_words': 444}