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

me.excel.tools.validator.sheet.RequireFieldValidator Maven / Gradle / Ivy

The newest version!
package me.excel.tools.validator.sheet;

import me.excel.tools.model.excel.ExcelCell;
import me.excel.tools.model.excel.ExcelRow;
import me.excel.tools.model.excel.ExcelSheet;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * 
 * required field validator
 *
 * all validators matches by field,
 * if field lost means all the ({@link me.excel.tools.validator.cell.CellValidator} and {@link me.excel.tools.setter.FieldValueSetter}) of this field will skip.
 * this validator useful to detect if excel files contains all the fields you want handle.
 *
 * eg: class A has fields [A, B...].
 * if you want modify A, B, supplied the {@link RequireFieldValidator#requireFields} as [A, B]. when the excel files lost A or B,
 * this validator will get false.
 * 
* Created by hanwen on 4/26/16. */ public class RequireFieldValidator implements SheetValidator { private List requireFields = new ArrayList<>(); private String errorMessage; public RequireFieldValidator(String... requireFields) { Collections.addAll(this.requireFields, requireFields); this.errorMessage = "不包含所有要求的字段"; } public RequireFieldValidator(String errorMessage, String... requireFields) { Collections.addAll(this.requireFields, requireFields); this.errorMessage = errorMessage; } @Override public String getErrorMessage() { return errorMessage; } @Override public List getMessageOnCells(ExcelSheet excelSheet) { return Collections.singletonList(excelSheet.getFirstRow().getFirstCell()); } @Override public boolean validate(ExcelSheet excelSheet) { ExcelRow fieldRow = excelSheet.getRow(2); List fields = new ArrayList<>(); for (ExcelCell fieldCell : fieldRow.getCells()) { fields.add(fieldCell.getValue()); } return CollectionUtils.subtract(requireFields, fields).isEmpty(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy