Class Checks
Problem) to the ValidationResult.
The text of the problem comes from PlatformMessages, and the label of the field comes
from FeatureLabels. For example, notNull(EObject, EStructuralFeature) adds the
problem "Field is not set (Speed)" for a field without a value.
By default, problems have the Problem.Severity.ERROR severity and no tab. To change this, use a
copy of Checks:
warnings()returns a copy that adds warnings (Problem.Severity.WARNING);onTab(String)returns a copy that adds problems with the title of a property panel tab.
A copy adds problems to the same ValidationResult, and the original Checks does
not change. You can combine copies:checks.warnings().onTab(tab).
Each field check returns true if the value is correct. If the value is wrong, the check
returns false and adds a problem. Use the result to skip the next checks of the same
object.
Example:
ValidationResult result = new ValidationResult();
Checks checks = new Checks(result, labels);
checks.notNull(truck, MyPackage.Literals.TRUCK__TYPE);
checks.onTab(MyMessages.messages().TAB_GENERAL).positive(truckType, MyPackage.Literals.TRUCK_TYPE__SPEED);
checks.warnings().reportIf(scenario.getTrucks().isEmpty(), scenario, MyMessages.messages().NO_TRUCKS);
-
Constructor Summary
ConstructorsConstructorDescriptionChecks(ValidationResult result, FeatureLabels labels) Creates checks that add errors (Problem.Severity.ERROR) without a tab. -
Method Summary
Modifier and TypeMethodDescriptionbooleandateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature) Checks that the begin date is not later than the end date.booleandateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature, LocalDateTime defaultBegin, LocalDateTime defaultEnd) Checks the range in the same way asdateRange(EObject, EStructuralFeature, EStructuralFeature).booleandistribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, boolean isRequired) Checks a field with a random distribution (RealDistribution), for example the duration of an operation.booleandistribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, Predicate<org.apache.commons.math3.distribution.RealDistribution> isInvalid, boolean isRequired) Checks the distribution in the same way asdistribution(EObject, EStructuralFeature, boolean).booleangreaterThan(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, double bound) Checks that the numeric value of the field is greater thanbound.booleanlatitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a latitude from -90 to 90, bounds included.booleanlongitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a longitude from -180 to 180, bounds included.booleannegative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is less than zero.booleannonEmptyDateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature) Checks that the begin date is strictly earlier than the end date.booleannonNegative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is not less than zero.booleannotEmpty(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that a list field is not empty.booleannotNull(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the field has a value.Returns a copy that adds problems with the title of a property panel tab.booleanpercent(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a percent from 0 to 100, bounds included.booleanpositive(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is greater than zero.voidAdds a ready problem as is.voidAdds a problem with a ready text, with the severity and the tab of thisChecks.voidAdds a problem ifhasProblemistrue.warnings()Returns a copy that adds warnings (Problem.Severity.WARNING).
-
Constructor Details
-
Checks
Creates checks that add errors (Problem.Severity.ERROR) without a tab.- Parameters:
result- the result where the checks add problemslabels- the labels of fields for the texts of problems
-
-
Method Details
-
warnings
Returns a copy that adds warnings (Problem.Severity.WARNING). The tab stays the same. Exception: thedistributionchecks select the severity of their problems themselves.- Returns:
- a new
Checkswith the same result
-
onTab
Returns a copy that adds problems with the title of a property panel tab. The severity stays the same. After a double click on such a problem, the property panel opens this tab.- Parameters:
tab- the title of the tab, as in the property panel;nullfor no tab- Returns:
- a new
Checkswith the same result
-
report
Adds a problem with a ready text, with the severity and the tab of thisChecks. Use this method for checks that are not among the standard ones.- Parameters:
object- the model object of the problem;nullif the problem is about the whole scenariomessage- the text for the user; the method adds it as is
-
reportIf
- Parameters:
hasProblem-trueif there is a problemobject- the model object of the problem;nullif the problem is about the whole scenariomessage- the text for the user
-
report
Adds a ready problem as is. The method does not apply the severity and the tab of thisChecks, because the problem already has them. Use this method for subclasses ofProblemwith extra data.- Parameters:
problem- the problem to add
-
notNull
public boolean notNull(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the field has a value. If there is no value, adds the problem "Field is not set (label)".Use this method for fields with one value. For a list field it always returns
true, because EMF returns an empty list for it instead ofnull. For lists, usenotEmpty(EObject, EStructuralFeature).- Parameters:
object- the object to checkfeature- the field of the object- Returns:
trueif the field has a value
-
notEmpty
public boolean notEmpty(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that a list field is not empty. If the list is empty, adds the problem "List is empty (label)".The field must be a list. For a field with one value, the method throws
ClassCastException.- Parameters:
object- the object to checkfeature- the list field of the object- Returns:
trueif the list has at least one element
-
positive
public boolean positive(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is greater than zero. If not, adds the problem "Value should be a positive number (label)".If there is no value (
null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returnstrue.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif the value exists and fits
-
negative
public boolean negative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is less than zero. If not, adds the problem "Value should be a negative number (label)".If there is no value (
null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returnstrue.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif the value exists and fits
-
nonNegative
public boolean nonNegative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the numeric value of the field is not less than zero. If not, adds the problem "Value should be a non-negative number (label)".If there is no value (
null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returnstrue.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif the value exists and fits
-
greaterThan
public boolean greaterThan(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, double bound) Checks that the numeric value of the field is greater thanbound. If not, adds the problem "Value (label) should be more than bound".If there is no value, or the field does not contain a number, the method also adds this problem. The text shows the bound as a
double, for example5.0.- Parameters:
object- the object to checkfeature- the numeric field of the objectbound- the lower bound; the bound itself does not fit- Returns:
trueif the value is greater than the bound
-
latitude
public boolean latitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a latitude from -90 to 90, bounds included. If not, adds the problem "Value should be from -90 to 90 (label)".If there is no value, or the field does not contain a number, the method does not check it and returns
true.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif there is no value or the value is in the range
-
longitude
public boolean longitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a longitude from -180 to 180, bounds included. If not, adds the problem "Value should be from -180 to 180 (label)".If there is no value, or the field does not contain a number, the method does not check it and returns
true.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif there is no value or the value is in the range
-
percent
public boolean percent(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature) Checks that the value of the field is a percent from 0 to 100, bounds included. If not, adds the problem "Value should be from 0 to 100 (label)".If there is no value, or the field does not contain a number, the method does not check it and returns
true.- Parameters:
object- the object to checkfeature- the numeric field of the object- Returns:
trueif there is no value or the value is in the range
-
dateRange
public boolean dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature) Checks that the begin date is not later than the end date. Equal dates are allowed. If the begin is later than the end, adds the problem "Inconsistent duration (begin - end)". The text shows only the dates, without the labels of the fields.If one of the dates is missing, the method does not check the range. Both fields must have the
LocalDateTimetype, otherwise the method throwsClassCastException.- Parameters:
object- the object to checkbeginFeature- the field of the begin dateendFeature- the field of the end date- Returns:
trueif the range is correct or one of the dates is missing
-
dateRange
public boolean dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature, LocalDateTime defaultBegin, LocalDateTime defaultEnd) Checks the range in the same way asdateRange(EObject, EStructuralFeature, EStructuralFeature). If a date field is empty, the method uses the default date, for example the begin or the end date of the scenario.- Parameters:
object- the object to checkbeginFeature- the field of the begin dateendFeature- the field of the end datedefaultBegin- the begin if the begin field is empty; can benulldefaultEnd- the end if the end field is empty; can benull- Returns:
trueif the range is correct or one of the dates is missing
-
nonEmptyDateRange
public boolean nonEmptyDateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature) Checks that the begin date is strictly earlier than the end date. UnlikedateRange(EObject, EStructuralFeature, EStructuralFeature), equal dates are also a problem. If one of the dates is missing, the method does not check the range.- Parameters:
object- the object to checkbeginFeature- the field of the begin dateendFeature- the field of the end date- Returns:
trueif the begin is earlier than the end or one of the dates is missing
-
distribution
public boolean distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, boolean isRequired) Checks a field with a random distribution (RealDistribution), for example the duration of an operation. The distribution must not give negative values.The method adds up to two problems:
- a warning (
Problem.Severity.WARNING) if this is a plain normal distribution and not aTruncatedNormalDistribution; - an error (
Problem.Severity.ERROR) "Value should be a non-negative number (label)" if the lower bound of the distribution is less than zero.
The lower bound of a plain normal distribution is minus infinity. So for such a distribution the method adds both problems.
The severity of these problems does not depend on
warnings(). The tab fromonTab(String)applies.If there is no value, the method adds the error "Field is not set (label)" only if
isRequiredistrue.- Parameters:
object- the object to checkfeature- the field with the distributionisRequired-trueif the field is required- Returns:
trueif there are no problems
- a warning (
-
distribution
public boolean distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, Predicate<org.apache.commons.math3.distribution.RealDistribution> isInvalid, boolean isRequired) Checks the distribution in the same way asdistribution(EObject, EStructuralFeature, boolean). The difference: the error comes from the custom conditionisInvalid, for example "the mean is not greater than zero".The text of the error is always "Value should be a non-negative number". If the condition checks something else, the text can be different from the condition.
- Parameters:
object- the object to checkfeature- the field with the distributionisInvalid- the condition of the error:trueif the distribution does not fitisRequired-trueif the field is required- Returns:
trueif there are no problems
-