Top Description Fields Constructors Methods
com.opencsv.validators

public Class RowFunctionValidator

extends Object
implements RowValidator
Class Inheritance
All Implemented Interfaces
com.opencsv.validators.RowValidator
Imports
com.opencsv.exceptions.CsvValidationException, java.util.function.Function

This validator is best used to validate a specific property of the row - either about a specific element or information about the array itself.

An empty or null first row is considered invalid.

As with all row validators the assumption is you have control of the data and have skipped any initial empty lines AND that your validator checks the size or handles the IndexOutOfBoundsException.

There are several examples coded in the RowFunctionValidatorTest but here are a couple to give you the idea of the flexibility this validator offers.

private static final String[] GOOD_ROW = {"8675309", "Firstname", "M", "Lastname", "Dec 06, 1951"};
    private static final String[] BAD_ROW = {"not a number", "not capitialized", "not an initial", "Not Single word", "12/06/51"};
    private static final String[] LONG_ROW = {"8675309", "Firstname", "M", "Lastname", "Dec 06, 1951", "More data"};
    private static final String[] SHORT_ROW = {"8675309", "Firstname", "Lastname", "Dec 06, 1951"};

    private static final Function<String[], Boolean> THIRD_ELEMENT_IS_MIDDLE_INITIAL = (x) -> {
        return x.length > 2 && x[2].matches("^[A-Z]$");
    };

    private static final Function<String[], Boolean> ROW_MUST_HAVE_FIVE_ELEMENTS = (x) -> {
        return (x.length == 5);
    };

@Test
@DisplayName("Simple test to show checking an middle initial")
public void thirdElementIsMiddleInitial() {
        validator = new RowFunctionValidator(THIRD_ELEMENT_IS_MIDDLE_INITIAL, "The third element must be the middle initial.");

        assertTrue(validator.isValid(GOOD_ROW));
        assertFalse(validator.isValid(BAD_ROW));
    }

@Test
@DisplayName("The row must have a specific number of elements in order to be valid.")
public void numberOfElementsInARow() {
        validator = new RowFunctionValidator(ROW_MUST_HAVE_FIVE_ELEMENTS, "A Row can have only five elements.");

        assertTrue(validator.isValid(GOOD_ROW));
        assertFalse(validator.isValid(LONG_ROW));
        assertFalse(validator.isValid(SHORT_ROW));
    }
Author
Scott Conway
Since
5.0

Field Summary

Modifier and TypeField and Description
private String
private Function<String[], Boolean>

Constructor Summary

AccessConstructor and Description
public
RowFunctionValidator(Function<String[], Boolean>
- function to run against the Array of Strings.
testFunction
,
String
- message to be included in the CsvValidationException error message.
failureMessage
)

Default Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
isValid(String[] row)

Implements com.opencsv.validators.RowValidator.isValid.

Performs the validation check on the string and returns the result.

public void
validate(String[] row)

Implements com.opencsv.validators.RowValidator.validate.

Performs the validation check on the row (an array of Strings) and throws an exception if invalid.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

failureMessageback to summary
private String failureMessage
testFunctionback to summary
private Function<String[], Boolean> testFunction

Constructor Detail

RowFunctionValidatorback to summary
public RowFunctionValidator(Function<String[], Boolean> testFunction, String failureMessage)

Default Constructor.

Parameters
testFunction:Function<String[], Boolean>

- function to run against the Array of Strings.

failureMessage:String

- message to be included in the CsvValidationException error message.

Method Detail

isValidback to summary
public boolean isValid(String[] row)

Implements com.opencsv.validators.RowValidator.isValid.

Doc from com.opencsv.validators.RowValidator.isValid.

Performs the validation check on the string and returns the result. While not called directly in opencsv it is in the interface to provide an easy way to test if the validator is functioning properly.

Annotations
@Override
validateback to summary
public void validate(String[] row) throws CsvValidationException

Implements com.opencsv.validators.RowValidator.validate.

Doc from com.opencsv.validators.RowValidator.validate.

Performs the validation check on the row (an array of Strings) and throws an exception if invalid.

Annotations
@Override
Exceptions
CsvValidationException:
Thrown if invalid. Should contain a message describing the error.