io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder Maven / Gradle / Ivy
package io.fabric8.kubernetes.api.model;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.ValidationException;
import java.util.Set;
import io.fabric8.kubernetes.api.model.resource.Quantity;
import java.util.HashMap;
import java.util.Map;
import io.fabric8.common.Builder;
public class ResourceRequirementsBuilder extends ResourceRequirementsFluent implements Builder{
ResourceRequirementsFluent> fluent;
public ResourceRequirementsBuilder(){
this(new ResourceRequirements());
}
public ResourceRequirementsBuilder( ResourceRequirementsFluent> fluent ){
this(fluent, new ResourceRequirements());
}
public ResourceRequirementsBuilder( ResourceRequirementsFluent> fluent , ResourceRequirements instance ){
this.fluent = fluent; fluent.withLimits(instance.getLimits()); fluent.withRequests(instance.getRequests());
}
public ResourceRequirementsBuilder( ResourceRequirements instance ){
this.fluent = this; this.withLimits(instance.getLimits()); this.withRequests(instance.getRequests());
}
public ResourceRequirements build(){
ResourceRequirements buildable = new ResourceRequirements(fluent.getLimits(),fluent.getRequests());
validate(buildable);
return buildable;
}
private void validate(T item) {
try {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set> violations = validator.validate(item);
if (!violations.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("Constraint Violations:\n");
for (ConstraintViolation violation : violations) {
sb.append("\t").append(violation.getRootBeanClass().getSimpleName()).append(" ").append(violation.getPropertyPath()).append(":").append(violation.getMessage()).append("\n");
}
throw new IllegalStateException(sb.toString());
}
} catch(ValidationException e) {
//ignore
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy