RangeConstraintpublic class RangeConstraint extends ConstraintUtils implements ConstraintRangeConstraint is a {@link Constraint} to validate a
numeric element against the given range.
It implements Constraint interface and extends
{@link ConstraintUtils} class.
match method of this object returns an empty
Collection if the value being validated belongs to the range
represented by this Constraint ; else it returns a
Collection with a {@link ConstraintFailure} object in it.
ConstraintUtils class provides utility methods for formating
failure messages and a print method to print this object. |
Fields Summary |
---|
private Double | startValueA start value of the range represented by
this Constraint . | private Double | endValueAn end value of the range represented by
this Constraint . |
Constructors Summary |
---|
public RangeConstraint()Creates a new instance of RangeConstraint
startValue = null;
endValue = null;
| public RangeConstraint(String startValue, String endValue)Creates a new instance of RangeConstraint .
try {
this.startValue = new Double(startValue);
this.endValue = new Double(endValue);
} catch(NumberFormatException e) {
String format =
BundleReader.getValue("Error_failed_to_create"); //NOI18N
Object[] arguments =
new Object[]{"RangeConstaint"}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
}
|
Methods Summary |
---|
private void | addFailure(java.util.Collection failed_constrained_list, java.lang.String name, java.lang.String value)
String failureMessage = formatFailureMessage(toString(), value, name);
String format = BundleReader.getValue(
"MSG_RangeConstraint_Failure"); //NOI18N
String range = startValue + " - " + endValue;
Object[] arguments = new Object[]{range};
String genericFailureMessage =
MessageFormat.format(format, arguments);
failed_constrained_list.add(new ConstraintFailure(toString(),
value, name, failureMessage, genericFailureMessage));
| public java.util.Collection | match(java.lang.String value, java.lang.String name)Validates the given value against this Constraint .
ArrayList failed_constrained_list = new ArrayList();
if((startValue == null) || (endValue == null)) {
return failed_constrained_list;
}
if((value != null) && (value.length() != 0)){
try {
Double val = new Double(value);
if((val.compareTo(startValue) < 0) ||
(val.compareTo(endValue) > 0)){
addFailure(failed_constrained_list, name, value);
}
} catch(NumberFormatException e) {
addFailure(failed_constrained_list, name, value); }
}
return failed_constrained_list;
| public void | print()Prints this Constraint .
super.print();
String format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
Object[] arguments =
new Object[]{"Range Start", startValue}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
arguments = new Object[]{"Range End", endValue}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
| public void | setRangeEnd(java.lang.String value)Sets the end value of the range represented by this object.
try {
endValue = new Double(value);
} catch(NumberFormatException e) {
String format =
BundleReader.getValue("Error_failed_to_set"); //NOI18N
Object[] arguments =
new Object[]{this.toString(), "Range End"}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
}
| public void | setRangeEnd(java.lang.Double value)Sets the end value of the range represented by this object.
endValue = value;
| public void | setRangeStart(java.lang.String value)Sets the start value of the range represented by this object.
try {
startValue = new Double(value);
} catch(NumberFormatException e) {
String format =
BundleReader.getValue("Error_failed_to_set"); //NOI18N
Object[] arguments =
new Object[]{this.toString(), "Range Start"}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
}
| public void | setRangeStart(java.lang.Double value)Sets the start value of the range represented by this object.
startValue = value;
|
|