FileDocCategorySizeDatePackage
RangeConstraint.javaAPI DocGlassfish v2 API8594Fri May 04 22:35:00 BST 2007com.sun.enterprise.tools.common.validation.constraints

RangeConstraint

public class RangeConstraint extends ConstraintUtils implements Constraint
RangeConstraint 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.
author
Rajeshwar Patil
version
%I%, %G%

Fields Summary
private Double
startValue
A start value of the range represented by this Constraint.
private Double
endValue
An 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 voidaddFailure(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.Collectionmatch(java.lang.String value, java.lang.String name)
Validates the given value against this Constraint.

param
value the value to be validated.
param
name the element name, value of which is being validated. It is used only in case of Constraint failure, to construct the failure message.
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures. This method will fail, if the given value is non-numeric or its numeric & does not belong to the range represented by this object.

        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 voidprint()
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 voidsetRangeEnd(java.lang.String value)
Sets the end value of the range represented by this object.

param
value the value to be set as 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 voidsetRangeEnd(java.lang.Double value)
Sets the end value of the range represented by this object.

param
value the value to be set as the end value of the range represented by this object.

        endValue = value;
    
public voidsetRangeStart(java.lang.String value)
Sets the start value of the range represented by this object.

param
value the value to be set as 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 voidsetRangeStart(java.lang.Double value)
Sets the start value of the range represented by this object.

param
value the value to be set as the start value of the range represented by this object.

           startValue = value;