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

InConstraint

public class InConstraint extends ConstraintUtils implements Constraint
InConstraint is a {@link Constraint} to validate an Enumeration value. It implements Constraint interface and extends {@link ConstraintUtils} class. match method of this object returns empty Collection if the value being validated belongs to the enumeration represented by this object; 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 Collection
enumeration
An enumeration represented by this Constraint.
Constructors Summary
public InConstraint()
Creates a new instance of InConstraint.



           
      
        enumeration = new ArrayList();
    
public InConstraint(Collection enumeration)
Creates a new instance of InConstraint.

        this.enumeration = enumeration;
    
public InConstraint(String[] enumeration)
Creates a new instance of InConstraint.

        this.enumeration = new ArrayList();
        int size = enumeration.length;
        for(int i=0; i<size; i++) {
            this.enumeration.add(enumeration[i]);
        }
    
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
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, only for the values that does not belong to an enumeration represented by this object.

        Collection failed_constrained_list = new ArrayList();
        if((value != null) && (value.length() != 0)) {
            if(!enumeration.contains(value)){
                String failureMessage = formatFailureMessage(toString(),
                    value, name);

                String format = BundleReader.getValue(
                    "MSG_InConstraint_Failure");                        //NOI18N
                String set = "";                                        //NOI18N
                Iterator iterator = enumeration.iterator();
                String member;
                while(iterator.hasNext()){
                    member = (String)iterator.next();
                    set = set + " " + member;                           //NOI18N
                }

                Object[] arguments = new Object[]{set};

                String genericFailureMessage = 
                    MessageFormat.format(format, arguments);

                failed_constrained_list.add(new ConstraintFailure(toString(),
                    value, name, failureMessage, genericFailureMessage));
            }
        }
        return failed_constrained_list;
    
voidprint()
Prints this Constraint.

        super.print();
        String format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
        Iterator iterator = enumeration.iterator();
        String values = "";
        while(iterator.hasNext()){
            values = values + "  " + (String)iterator.next();           //NOI18N
        }

        if(values != null){
            Object[] arguments = 
                new Object[]{"Enumeration Values", values};             //NOI18N
            System.out.println(MessageFormat.format(format, arguments));
        }
    
public voidsetEnumerationValue(java.util.Collection enumeration)
Sets the given Collection as the enumeration represented by this object.

param
enumeration the Collection to be set as the enumeration represented by this object.

        this.enumeration = enumeration;
    
public voidsetEnumerationValue(java.lang.String value)
Adds the given value to the enumeration represented by this object.

param
value the value to be added to the enumeration represented by this object.

        enumeration.add(value);