LineValidatorAggregator.java

  1. package com.opencsv.validators;

  2. import com.opencsv.exceptions.CsvValidationException;

  3. import java.util.ArrayList;
  4. import java.util.List;

  5. /**
  6.  * The aggregator's purpose is to collect multiple {@link LineValidator}s and
  7.  * run them against a single line.
  8.  * This way complex validations can be performed.
  9.  *
  10.  * @author Scott Conway
  11.  * @since 5.0
  12.  */
  13. public class LineValidatorAggregator {
  14.     private static final int CAPACITY = 512;
  15.     private static final int MULTIPLIER = 3;
  16.     private List<LineValidator> validators = new ArrayList<>();

  17.     /**
  18.      * Default constructor.
  19.      */
  20.     public LineValidatorAggregator() {
  21.     }

  22.     /**
  23.      * Add an validator to the aggregator.
  24.      *
  25.      * @param validator Validator to be added.
  26.      */
  27.     public void addValidator(LineValidator validator) {
  28.         if (validator != null) {
  29.             validators.add(validator);
  30.         }
  31.     }

  32.     /**
  33.      * Runs all LineValidators' {@link LineValidator#isValid(String)} method against the line.
  34.      * This is a short circuit: as soon as one validator returns {@code false}
  35.      * then {@code false} is returned.
  36.      *
  37.      * @param line String to be validated.
  38.      * @return {@code true} if all validators'
  39.      *   {@link LineValidator#isValid(String)} methods return {@code true},
  40.      *   {@code false} otherwise.
  41.      */
  42.     public boolean isValid(final String line) {
  43.         return validators.stream().allMatch(v -> v.isValid(line));
  44.     }

  45.     /**
  46.      * Runs all LineValidators validate commands and if the string is invalid then it combines all the validation error
  47.      * messages in a single CsvValidationException.
  48.      *
  49.      * @param line String to be validated
  50.      * @throws CsvValidationException Thrown if the string is invalid
  51.      */
  52.     public void validate(String line) throws CsvValidationException {
  53.         if (validators.isEmpty()) {
  54.           return;
  55.         }

  56.         StringBuilder combinedExceptionMessage = null;

  57.         for (LineValidator validator : validators) {
  58.             try {
  59.                 validator.validate(line);
  60.             } catch (CsvValidationException ex) {
  61.                 if (combinedExceptionMessage == null) {
  62.                     int length = (ex.getMessage().length() + 2) * MULTIPLIER;
  63.                     combinedExceptionMessage = new StringBuilder(Math.max(length, CAPACITY));
  64.                 }
  65.                 combinedExceptionMessage.append(ex.getMessage()).append("\n");
  66.             }
  67.         }

  68.         if (combinedExceptionMessage != null && combinedExceptionMessage.length() > 0) {
  69.             throw new CsvValidationException(combinedExceptionMessage.toString());
  70.         }
  71.     }

  72.     /**
  73.      * Setter created for unit test.
  74.      *
  75.      * @param validators - list of validators to use.
  76.      */
  77.     void setValidators(List<LineValidator> validators) {
  78.         this.validators = validators;
  79.     }
  80. }