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

org.jsapar.error.ThresholdCollectingErrorConsumer Maven / Gradle / Ivy

Go to download

The JSaPar package is a java library that provides a parser for flat and CSV (Comma Separated Values) files.

The newest version!
package org.jsapar.error;

import org.jsapar.parse.CollectingConsumer;

import java.util.List;

/**
 * This error event listener records errors until it reaches a maximum number. Any errors that occurs after maximum
 * number has been reached will cause a {@link MaxErrorsExceededException} instead.
 */
public class ThresholdCollectingErrorConsumer extends CollectingConsumer {
    private final int maxNumberOfErrors;

    /**
     * Creates an error consumer where the error list needs to be fetched with
     * {@link CollectingConsumer#getCollected()} afterwards.
     *
     * @param maxNumberOfErrors The maximum number of errors allowed to be recorded
     */
    public ThresholdCollectingErrorConsumer(int maxNumberOfErrors) {
        this.maxNumberOfErrors = maxNumberOfErrors;
    }

    /**
     * Creates an error event listener that adds errors to the supplied list
     *
     * @param maxNumberOfErrors The maximum number of errors allowed to be recorded
     * @param errors            The list that errors will be added to.
     */
    public ThresholdCollectingErrorConsumer(int maxNumberOfErrors, List errors) {
        super(errors);
        this.maxNumberOfErrors = maxNumberOfErrors;
    }

    /**
     * Called when there is an error while parsing input or composing output. This implementation saves the errors in
     * a member list until maximum number has been reached. The list is protected by a semaphore/synchronized block in
     * case more than one thread is writing
     * to the same list. The list itself is used as the semaphore.
     * Any errors that occurs after maximum
     * number has been reached will cause a {@link MaxErrorsExceededException} to be thrown.
     *
     * @param error The the error information.
     */
    @Override
    public void accept(JSaParException error) {
        synchronized(this) {
            super.accept(error);
            if (this.size() > maxNumberOfErrors)
                throw new MaxErrorsExceededException(error, getCollected());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy