All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.everit.json.schema.CollectingFailureReporter Maven / Gradle / Ivy

Go to download

Implementation of the JSON Schema Core Draft v4 specification built with the org.json API

There is a newer version: 1.14.4
Show newest version
package org.everit.json.schema;

import java.util.ArrayList;
import java.util.List;

class CollectingFailureReporter extends ValidationFailureReporter {

    private List failures = new ArrayList<>(1);

    CollectingFailureReporter(Schema schema) {
        super(schema);
    }

    @Override
    public void failure(ValidationException exc) {
        failures.add(exc);
    }

    public void validationFinished() {
        ValidationException.throwFor(schema, failures);
    }

    public ValidationException inContextOfSchema(Schema schema, Runnable task) {
        int failureCountBefore = failures.size();
        super.inContextOfSchema(schema, task);
        int failureCountAfter = failures.size(), newFailureCount = failureCountAfter - failureCountBefore;
        if (newFailureCount == 0) {
            return null;
        } else if (newFailureCount == 1) {
            return failures.remove(failures.size() - 1);
        } else {
            List newFailures = new ArrayList<>(failures.subList(failureCountBefore, failures.size()));
            int toBeRemoved = newFailureCount, lastIndex = failureCountAfter;
            while (toBeRemoved-- > 0) {
                failures.remove(--lastIndex);
            }
            return ValidationException.createWrappingException(schema, newFailures);
        }
    }

    @Override
    Object getState() {
        return failureCount();
    }

    @Override
    boolean isChanged(Object oldState) {
        return !oldState.equals(failureCount());
    }

    int failureCount() {
        return failures.size();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy