use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.
the class FieldDefinition method validate.
public Object validate(Object value) {
final String fieldName = getName();
value = toFieldValueException(value);
if (value == null) {
if (isRequired()) {
throw new IllegalArgumentException(fieldName + " is required");
}
} else {
final RecordDefinition recordDefinition = getRecordDefinition();
final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(fieldName);
if (codeTable == null) {
final int maxLength = getLength();
if (value instanceof Number) {
final Number number = (Number) value;
final BigDecimal bigNumber = new BigDecimal(number.toString());
final int length = bigNumber.precision();
if (maxLength > 0) {
if (length > maxLength) {
throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
}
}
final int scale = bigNumber.scale();
final int maxScale = getScale();
if (maxScale > 0) {
if (scale > maxScale) {
throw new IllegalArgumentException(fieldName + "=" + value + " scale " + scale + " > " + maxScale);
}
}
final Number minValue = getMinValue();
if (minValue != null) {
if (NumericComparator.numericCompare(number, minValue) < 0) {
throw new IllegalArgumentException(fieldName + "=" + value + " > " + minValue);
}
}
final Number maxValue = getMaxValue();
if (maxValue != null) {
if (NumericComparator.numericCompare(number, maxValue) > 0) {
throw new IllegalArgumentException(fieldName + "=" + value + " < " + maxValue);
}
}
} else if (value instanceof String) {
final String string = (String) value;
final int length = string.length();
if (maxLength > 0) {
if (length > maxLength) {
throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
}
}
} else if (value instanceof Geometry) {
final Geometry geometry = (Geometry) value;
final IsValidOp validOp = new IsValidOp(geometry, false);
if (!validOp.isValid()) {
final String errors = Strings.toString(validOp.getErrors());
throw new IllegalArgumentException("Geometry not valid: " + errors);
}
}
if (!this.allowedValues.isEmpty()) {
if (!this.allowedValues.containsKey(value)) {
throw new IllegalArgumentException(fieldName + "=" + value + " not in (" + Strings.toString(",", this.allowedValues) + ")");
}
}
} else {
final Identifier id = codeTable.getIdentifier(value);
if (id == null) {
String codeTableName;
if (codeTable instanceof CodeTableProperty) {
@SuppressWarnings("resource") final CodeTableProperty property = (CodeTableProperty) codeTable;
codeTableName = property.getTypeName();
} else {
codeTableName = codeTable.toString();
}
throw new IllegalArgumentException("Unable to find code for '" + value + "' in " + codeTableName);
}
}
}
return value;
}
use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.
the class ValidClosedRingTest method checkIsValid.
private void checkIsValid(final Geometry geom, final boolean expected) {
final IsValidOp validator = new IsValidOp(geom);
final boolean isValid = validator.isValid();
assertTrue(isValid == expected);
}
use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.
the class ValidationFunctions method invalidGeoms.
public static Geometry invalidGeoms(final Geometry g) {
final List invalidGeoms = new ArrayList();
for (int i = 0; i < g.getGeometryCount(); i++) {
final Geometry geom = g.getGeometry(i);
final IsValidOp ivop = new IsValidOp(geom);
final GeometryValidationError err = ivop.getValidationError();
if (err != null) {
invalidGeoms.add(geom);
}
}
return g.getGeometryFactory().buildGeometry(invalidGeoms);
}
use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.
the class ValidationFunctions method invalidLocations.
/**
* Validates all geometries in a collection independently.
* Errors are returned as points at the invalid location
*
* @param g
* @return the invalid locations, if any
*/
public static Geometry invalidLocations(final Geometry g) {
final List invalidLoc = new ArrayList();
for (int i = 0; i < g.getGeometryCount(); i++) {
final Geometry geom = g.getGeometry(i);
final IsValidOp ivop = new IsValidOp(geom);
final GeometryValidationError err = ivop.getValidationError();
if (err != null) {
invalidLoc.add(g.getGeometryFactory().point(err.getErrorPoint()));
}
}
return g.getGeometryFactory().buildGeometry(invalidLoc);
}
Aggregations