![JAR search and dependency download from the Maven repository](/logo.png)
bt.bencoding.model.BaseBEObjectModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-bencoding Show documentation
Show all versions of bt-bencoding Show documentation
Library for parsing, encoding and validating bencoded documents in Java
package bt.bencoding.model;
import bt.bencoding.model.rule.Rule;
import java.util.List;
abstract class BaseBEObjectModel implements BEObjectModel {
private List rules;
BaseBEObjectModel(List rules) {
this.rules = rules;
}
@Override
public final ValidationResult validate(Object object) {
ValidationResult result;
if (object != null) {
// unwrap BEObjects
if (object instanceof BEObject) {
object = ((BEObject) object).getValue();
}
Class> javaType = TypesMapping.getJavaTypeForBEType(getType());
if (!javaType.isAssignableFrom(object.getClass())) {
result = new ValidationResult();
result.addMessage("Wrong type -- expected " + javaType.getName()
+ ", actual: " + object.getClass().getName());
return result;
}
}
result = validateObject(object);
// fail-fast if any rules failed
return result.isSuccess()? afterValidate(result, object) : result;
}
private ValidationResult validateObject(Object object) {
ValidationResult result = new ValidationResult();
rules.stream()
.filter(rule -> !rule.validate(object))
.map(Rule::getDescription)
.forEach(result::addMessage);
return result;
}
/**
* Contribute to the validation result.
*
* @since 1.0
*/
protected abstract ValidationResult afterValidate(ValidationResult validationResult, Object object);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy