package org.gcube.accounting.datamodel.validators; import java.io.Serializable; import org.gcube.accounting.datamodel.UsageRecord.OperationResult; import org.gcube.accounting.datamodel.annotations.ValidOperationResult; import org.gcube.accounting.exception.InvalidValueException; public class ValidOperationResultValidator implements FieldValidator{ public Class annotation() { return ValidOperationResult.class; } public boolean isValid(Serializable toValidate) { try { validate(toValidate); } catch (InvalidValueException e) { return false; } return true; } public Serializable validate(Serializable toValidate) throws InvalidValueException { if(toValidate instanceof OperationResult){ return toValidate; } Integer integerObj = Integer.getInteger((String) toValidate); if(integerObj!=null){ return integerObj; } throw new InvalidValueException(getErrorSuffix()); } public String getErrorSuffix() { return String.format("not instace of %s", OperationResult.class.getSimpleName()); } }