FileDocCategorySizeDatePackage
CardinalConstraint.javaAPI DocGlassfish v2 API8596Fri May 04 22:34:58 BST 2007com.sun.enterprise.tools.common.validation.constraints

CardinalConstraint

public class CardinalConstraint extends ConstraintUtils implements Constraint
CardinalConstraint is a {@link Constraint} to validate the structure. It implements Constraint interface and extends {@link ConstraintUtils} class. match method of this object returns empty Collection if the value being validated conforms to the structure specified 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 int
cardinal
The structure represented by this Constraint.
Constructors Summary
public CardinalConstraint()
Creates a new instance of CardinalConstraint



           
      
        cardinal = Constants.MANDATORY_ELEMENT;
    
public CardinalConstraint(int cardinal)
Creates a new instance of CardinalConstraint.

        this.cardinal = cardinal;
    
public CardinalConstraint(String cardinal)
Creates a new instance of CardinalConstraint.

        try {
           this.cardinal = Integer.parseInt(cardinal);
        } catch(NumberFormatException e) {
            String format = 
                BundleReader.getValue("Error_failed_to_create");        //NOI18N
            Object[] arguments = 
                new Object[]{"CardinalConstraint"};                     //NOI18N
            System.out.println(MessageFormat.format(format, arguments));
        }
    
Methods Summary
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. This value must be of appropiate type, that is , either an Object or an array of Objects, in consistence with the Cardinal represented by this Object.
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 Constraint will fail if the value does not conform to the structure represented by this object.

        return match((Object)value, name);
    
public java.util.Collectionmatch(java.lang.Object value, java.lang.String name)
Validates the given value against this Constraint.

param
value the value to be validated. This value must be of appropriate type, that is , either an Object or an array of Objects, in consistence with the Cardinal represented by this Object.
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 value does not conform to the structure represented by this object.

        Collection failed_constrained_list = 
                new ArrayList();

        if(Constants.MANDATORY_ARRAY == cardinal) {
            Object[]  values = null;
            try {
                values =  (Object[])value;
            } catch(ClassCastException exception){
                System.out.println(BundleReader.getValue(
                   "Error_expects_argument_one_to_be_an_array_of_Objects"));//NOI18N
            }

            if((null == values) || (values.length  <  1)){
                String failureMessage = formatFailureMessage(toString(), name);
                failed_constrained_list.add(new ConstraintFailure(toString(),
                    null, name, failureMessage, BundleReader.getValue(
                        "MSG_CardinalConstraint_Failure")));            //NOI18N
            }
        } else {
            if(Constants.MANDATORY_ELEMENT == cardinal){
                if (null == value){
                    String failureMessage =
                        formatFailureMessage(toString(), name);
                    failed_constrained_list.add(
                        new ConstraintFailure(toString(), null, name,
                            failureMessage, BundleReader.getValue(
                                "MSG_CardinalConstraint_Failure")));    //NOI18N
                }
            }
        }
        return failed_constrained_list;
    
public voidprint()
Prints this Constraint.

        super.print();
        String format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
        Object[] arguments = 
            new Object[]{"Cardinal", String.valueOf(cardinal)};         //NOI18N
        System.out.println(MessageFormat.format(format, arguments));
    
public voidsetCardinal(int cardinal)
Sets the given cardinal as the structure represented by this object.

param
cardinal the cardinal to be set as the structure represented by this object.

        this.cardinal = cardinal;
    
public voidsetCardinal(java.lang.String cardinal)
Sets the given cardinal as the structure represented by this object.

param
cardinal the cardinal to be set as the structure represented by this object.

        try {
           this.cardinal = Integer.parseInt(cardinal);
        } catch(NumberFormatException e) {
            String format = 
                BundleReader.getValue("Error_failed_to_set");           //NOI18N
            Object[] arguments = 
                new Object[]{this.toString(), "Cardinal"};              //NOI18N

            System.out.println(MessageFormat.format(format, arguments));
        }