org.isda.cdm.validation.datarule.NumberRepresentationsForRootClass Maven / Gradle / Ivy
package org.isda.cdm.validation.datarule;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.annotations.RosettaDataRule;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.expression.ComparisonResult;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.validation.ValidationResult;
import com.rosetta.model.lib.validation.ValidationResult.ValidationType;
import com.rosetta.model.lib.validation.Validator;
import org.isda.cdm.NumberRepresentations;
import org.isda.cdm.StringRep;
import static com.rosetta.model.lib.expression.ExpressionOperators.*;
/**
* @version test
*/
@RosettaDataRule("NumberRepresentationsForRootClass")
@ImplementedBy(NumberRepresentationsForRootClass.Default.class)
public interface NumberRepresentationsForRootClass extends Validator {
String NAME = "NumberRepresentationsForRootClass";
String DEFINITION = "if integer = 1 then string -> val = 'one'";
ValidationResult validate(RosettaPath path, NumberRepresentations numberRepresentations);
class Default implements NumberRepresentationsForRootClass {
@Override
public ValidationResult validate(RosettaPath path, NumberRepresentations numberRepresentations) {
ComparisonResult result = executeDataRule(numberRepresentations);
if (result.get()) {
return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "NumberRepresentations", path, DEFINITION);
}
String failureMessage = result.getError();
if (failureMessage == null || failureMessage.contains("Null") || failureMessage == "") {
failureMessage = "Condition has failed.";
}
return ValidationResult.failure(NAME, ValidationType.DATA_RULE, "NumberRepresentations", path, DEFINITION, failureMessage);
}
private ComparisonResult executeDataRule(NumberRepresentations numberRepresentations) {
try {
if (areEqual(MapperS.of(numberRepresentations).map("getInteger", _numberRepresentations -> _numberRepresentations.getInteger()), MapperS.of(1), CardinalityOperator.All).getOrDefault(false)) {
return areEqual(MapperS.of(numberRepresentations).map("getString", _numberRepresentations -> _numberRepresentations.getString()).map("getVal", stringRep -> stringRep.getVal()), MapperS.of("one"), CardinalityOperator.All);
}
return ComparisonResult.successEmptyOperand("");
}
catch (Exception ex) {
return ComparisonResult.failure(ex.getMessage());
}
}
}
@SuppressWarnings("unused")
class NoOp implements NumberRepresentationsForRootClass {
@Override
public ValidationResult validate(RosettaPath path, NumberRepresentations numberRepresentations) {
return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "NumberRepresentations", path, DEFINITION);
}
}
}