protected void computeDiff()

in src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java [63:136]


    protected void computeDiff(Extension previousExtension, Extension currentExtension, Feature target) {
        boolean replace = false;

        switch (previousExtension.getType()) {
            case ARTIFACTS:
                Extension targetExtension = new Extension(previousExtension.getType(), previousExtension.getName(), previousExtension.getState());

                for (Artifact previous : previousExtension.getArtifacts()) {
                    Artifact current = currentExtension.getArtifacts().getSame(previous.getId());

                    boolean add = false;

                    if (current == null || (add = !previous.getId().equals(current.getId()))) {
                        target.getPrototype().getArtifactExtensionRemovals()
                                             .computeIfAbsent(previousExtension.getName(), k -> new LinkedList<ArtifactId>())
                                             .add(previous.getId());
                    }

                    if (add) {
                        targetExtension.getArtifacts().add(current);
                    }
                }

                for (Artifact current : currentExtension.getArtifacts()) {
                    Artifact previous = previousExtension.getArtifacts().getSame(current.getId());

                    if (previous == null) {
                        targetExtension.getArtifacts().add(current);
                    }
                }

                if (!targetExtension.getArtifacts().isEmpty()) {
                    target.getExtensions().add(targetExtension);
                }

                break;

            case TEXT:
                if (!previousExtension.getText().equals(currentExtension.getText())) {
                    replace = true;
                }
                break;

            case JSON:
                String previousJSON = previousExtension.getJSON();
                String currentJSON = currentExtension.getJSON();

                try {
                    JsonValue previousNode = parseJSON(previousJSON);
                    JsonValue currentNode = parseJSON(currentJSON); 

                    if (!previousNode.equals(currentNode)) {
                        replace = true;
                    }
                } catch (Throwable t) {
                    // should not happen
                    throw new RuntimeException("A JSON parse error occurred while parsing previous '"
                                               + previousJSON
                                               + "' and current '"
                                               + currentJSON
                                               + "', see nested errors:", t);
                }
                break;

            // it doesn't happen
            default:
                break;
        }

        if (replace) {
            target.getPrototype().getExtensionRemovals().add(currentExtension.getName());
            target.getExtensions().add(currentExtension);
        }
    }