KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > constraints > InConstraint


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * InConstraint.java April 1, 2003, 8:24 AM
26  *
27  */

28
29 package com.sun.enterprise.tools.common.validation.constraints;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.text.MessageFormat JavaDoc;
35
36 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure;
37 import com.sun.enterprise.tools.common.validation.util.BundleReader;
38
39
40 /**
41  * InConstraint is a {@link Constraint} to validate an Enumeration value.
42  * It implements <code>Constraint</code> interface and extends
43  * {@link ConstraintUtils} class.
44  * <code>match</code> method of this object returns empty
45  * <code>Collection</code> if the value being validated belongs to the
46  * enumeration represented by this object; else it returns a
47  * <code>Collection</code> with a {@link ConstraintFailure} object in it.
48  * <code>ConstraintUtils</code> class provides utility methods for formating
49  * failure messages and a <code>print<method> method to print this object.
50  *
51  * @author Rajeshwar Patil
52  * @version %I%, %G%
53  */

54 public class InConstraint extends ConstraintUtils implements Constraint {
55
56     /**
57      * An enumeration represented by this <code>Constraint</code>.
58      */

59     private Collection JavaDoc enumeration = null;
60
61
62     /** Creates a new instance of <code>InConstraint</code>. */
63     public InConstraint() {
64         enumeration = new ArrayList JavaDoc();
65     }
66
67
68     /** Creates a new instance of <code>InConstraint</code>. */
69     public InConstraint(Collection JavaDoc enumeration) {
70         this.enumeration = enumeration;
71     }
72
73
74     /** Creates a new instance of <code>InConstraint</code>. */
75     public InConstraint(String JavaDoc[] enumeration) {
76         this.enumeration = new ArrayList JavaDoc();
77         int size = enumeration.length;
78         for(int i=0; i<size; i++) {
79             this.enumeration.add(enumeration[i]);
80         }
81     }
82
83
84     /**
85      * Validates the given value against this <code>Constraint</code>.
86      *
87      * @param value the value to be validated
88      * @param name the element name, value of which is being validated.
89      * It is used only in case of <code>Constraint</code> failure, to
90      * construct the failure message.
91      *
92      * @return <code>Collection</code> the Collection of
93      * <code>ConstraintFailure</code> Objects. Collection is empty
94      * if there are no failures. This method will fail, only for the values
95      * that does not belong to an enumeration represented by this object.
96      */

97     public java.util.Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
98         Collection JavaDoc failed_constrained_list = new ArrayList JavaDoc();
99         if((value != null) && (value.length() != 0)) {
100             if(!enumeration.contains(value)){
101                 String JavaDoc failureMessage = formatFailureMessage(toString(),
102                     value, name);
103
104                 String JavaDoc format = BundleReader.getValue(
105                     "MSG_InConstraint_Failure"); //NOI18N
106
String JavaDoc set = ""; //NOI18N
107
Iterator JavaDoc iterator = enumeration.iterator();
108                 String JavaDoc member;
109                 while(iterator.hasNext()){
110                     member = (String JavaDoc)iterator.next();
111                     set = set + " " + member; //NOI18N
112
}
113
114                 Object JavaDoc[] arguments = new Object JavaDoc[]{set};
115
116                 String JavaDoc genericFailureMessage =
117                     MessageFormat.format(format, arguments);
118
119                 failed_constrained_list.add(new ConstraintFailure(toString(),
120                     value, name, failureMessage, genericFailureMessage));
121             }
122         }
123         return failed_constrained_list;
124     }
125
126
127     /**
128      * Sets the given <code>Collection</code> as the enumeration
129      * represented by this object.
130      *
131      * @param enumeration the <code>Collection</code> to be set
132      * as the enumeration represented by this object.
133      */

134     public void setEnumerationValue(Collection JavaDoc enumeration){
135         this.enumeration = enumeration;
136     }
137
138
139     /**
140      * Adds the given <code>value</code> to the enumeration
141      * represented by this object.
142      *
143      * @param value the value to be added to the enumeration
144      * represented by this object.
145      */

146     public void setEnumerationValue(String JavaDoc value){
147         enumeration.add(value);
148     }
149
150
151     /**
152      * Prints this <code>Constraint</code>.
153      */

154     void print() {
155         super.print();
156         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
157
Iterator JavaDoc iterator = enumeration.iterator();
158         String JavaDoc values = "";
159         while(iterator.hasNext()){
160             values = values + " " + (String JavaDoc)iterator.next(); //NOI18N
161
}
162
163         if(values != null){
164             Object JavaDoc[] arguments =
165                 new Object JavaDoc[]{"Enumeration Values", values}; //NOI18N
166
System.out.println(MessageFormat.format(format, arguments));
167         }
168     }
169 }
170
Popular Tags