mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
little performance optimization (less method calls)
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
|
||||
package ninja.javafx.smartcsv.fx.table.model;
|
||||
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.concurrent.Service;
|
||||
@@ -38,6 +39,8 @@ import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The CSVModel is the client representation for the csv filepath.
|
||||
@@ -168,11 +171,16 @@ public class CSVModel {
|
||||
}
|
||||
}
|
||||
|
||||
for (int lineNumber = 0; lineNumber < rows.size(); lineNumber++) {
|
||||
int maxRows = rows.size();
|
||||
for (int lineNumber = 0; lineNumber < maxRows; lineNumber++) {
|
||||
CSVRow row = rows.get(lineNumber);
|
||||
row.setValidator(validator);
|
||||
for (String column : row.getColumns().keySet()) {
|
||||
CSVValue value = row.getColumns().get(column).getValue();
|
||||
|
||||
Map<String, ObjectProperty<CSVValue>> table = row.getColumns();
|
||||
Set<String> columns = table.keySet();
|
||||
|
||||
for (String column : columns) {
|
||||
CSVValue value = table.get(column).getValue();
|
||||
value.setValidator(validator);
|
||||
if (validator != null) {
|
||||
ValidationError validationError = validator.isValid(column, value.getValue(), lineNumber);
|
||||
@@ -188,6 +196,7 @@ public class CSVModel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Throwable t) {
|
||||
logger.error("validation error", t);
|
||||
}
|
||||
|
||||
@@ -73,32 +73,27 @@ public class ValidationConfiguration {
|
||||
}
|
||||
|
||||
public Integer getMaxLengthRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return doubleToInteger((Double)getValue(column, "maxlength"));
|
||||
Double value = (Double)getValue(column, "maxlength");
|
||||
return value != null ? doubleToInteger(value) : null;
|
||||
}
|
||||
|
||||
public String getDateRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)getValue(column, "date");
|
||||
}
|
||||
|
||||
public Boolean getAlphanumericRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (Boolean)getValue(column, "alphanumeric");
|
||||
}
|
||||
|
||||
public String getRegexpRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)getValue(column, "regexp");
|
||||
}
|
||||
|
||||
public List<String> getValueOfRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (List<String>)getValue(column, "value of");
|
||||
}
|
||||
|
||||
public String getGroovyRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)getValue(column, "groovy");
|
||||
}
|
||||
|
||||
@@ -160,18 +155,19 @@ public class ValidationConfiguration {
|
||||
}
|
||||
|
||||
private Object getValue(String column, String key) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return columnConfigurations.get(column).get(key);
|
||||
if (columnConfigurations != null) {
|
||||
Map rulesForColumn = columnConfigurations.get(column);
|
||||
if (rulesForColumn != null) {
|
||||
return columnConfigurations.get(column).get(key);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean noHeader() {
|
||||
return headerConfiguration == null;
|
||||
}
|
||||
|
||||
private boolean noRulesFor(String column) {
|
||||
return columnConfigurations == null || columnConfigurations.get(column) == null;
|
||||
}
|
||||
|
||||
private Integer doubleToInteger(Double value) {
|
||||
if (value == null) return null;
|
||||
return (int)Math.round(value);
|
||||
|
||||
@@ -111,7 +111,8 @@ public class Validator {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private void checkUniqueness(String column, String value, Integer lineNumber, ValidationError error) {
|
||||
if (validationConfig.getUniqueRuleFor(column) != null && validationConfig.getUniqueRuleFor(column)) {
|
||||
Boolean uniqueRule = validationConfig.getUniqueRuleFor(column);
|
||||
if (uniqueRule != null && uniqueRule) {
|
||||
HashMap<String, Integer> columnValueMap = uniquenessLookupTable.get(column);
|
||||
columnValueMap = getColumnValueMap(column, columnValueMap);
|
||||
Integer valueInLineNumber = columnValueMap.get(value);
|
||||
@@ -180,7 +181,8 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkBlankOrNull(String column, String value, ValidationError error) {
|
||||
if (validationConfig.getNotEmptyRuleFor(column) != null && validationConfig.getNotEmptyRuleFor(column)) {
|
||||
Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column);
|
||||
if (notEmptyRule != null && notEmptyRule) {
|
||||
if (isBlankOrNull(value)) {
|
||||
error.add("validation.message.not.empty");
|
||||
}
|
||||
@@ -188,7 +190,8 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkInteger(String column, String value, ValidationError error) {
|
||||
if (validationConfig.getIntegerRuleFor(column) != null && validationConfig.getIntegerRuleFor(column)) {
|
||||
Boolean integerRule = validationConfig.getIntegerRuleFor(column);
|
||||
if (integerRule != null && integerRule) {
|
||||
if (!isInt(value)) {
|
||||
error.add("validation.message.integer");
|
||||
}
|
||||
@@ -196,7 +199,8 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkDouble(String column, String value, ValidationError error) {
|
||||
if (validationConfig.getDoubleRuleFor(column) != null && validationConfig.getDoubleRuleFor(column)) {
|
||||
Boolean doubleRule = validationConfig.getDoubleRuleFor(column);
|
||||
if (doubleRule != null && doubleRule) {
|
||||
if (!isDouble(value)) {
|
||||
error.add("validation.message.double");
|
||||
}
|
||||
@@ -231,7 +235,8 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkAlphaNumeric(String column, String value, ValidationError error) {
|
||||
if (validationConfig.getAlphanumericRuleFor(column) != null && validationConfig.getAlphanumericRuleFor(column)) {
|
||||
Boolean alphaNumericRule = validationConfig.getAlphanumericRuleFor(column);
|
||||
if (alphaNumericRule != null && alphaNumericRule) {
|
||||
if (!matchRegexp(value, "[0-9a-zA-Z]*")) {
|
||||
error.add("validation.message.alphanumeric");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user