2015-05-28 13:19:25 +02:00
|
|
|
package org.gcube.accounting.datamodel.validators;
|
|
|
|
|
2015-06-05 11:43:53 +02:00
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
2015-06-05 12:54:26 +02:00
|
|
|
import org.gcube.accounting.exception.InvalidValueException;
|
2015-06-05 11:43:53 +02:00
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2015-06-03 11:57:38 +02:00
|
|
|
public class ValidLongValidator implements FieldValidator<ValidLong>{
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2015-06-03 11:57:38 +02:00
|
|
|
public Class<ValidLong> annotation() {
|
|
|
|
return ValidLong.class;
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 11:43:53 +02:00
|
|
|
public boolean isValid(Serializable toValidate) {
|
2015-06-05 12:54:26 +02:00
|
|
|
try {
|
|
|
|
validate(toValidate);
|
|
|
|
} catch (InvalidValueException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
2015-05-28 13:19:25 +02:00
|
|
|
if(toValidate instanceof Long){
|
2015-06-05 12:54:26 +02:00
|
|
|
return toValidate;
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|
2015-06-05 11:06:17 +02:00
|
|
|
Long longObj = Long.getLong((String) toValidate);
|
|
|
|
if(longObj!=null){
|
2015-06-05 12:54:26 +02:00
|
|
|
return longObj;
|
2015-06-05 11:06:17 +02:00
|
|
|
}
|
2015-05-28 13:19:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 12:54:26 +02:00
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
public String getErrorSuffix() {
|
2015-06-03 11:57:38 +02:00
|
|
|
return String.format("not instace of %s", Long.class.getSimpleName());
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|