KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MandatoryConstraint.java March 24, 2003, 5:17 PM
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
34 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure;
35 import com.sun.enterprise.tools.common.validation.util.BundleReader;
36
37 /**
38  * MandatoryConstraint is a {@link Constraint} to validate non-null objects.
39  * It implements <code>Constraint</code> interface and extends
40  * {@link ConstraintUtils} class.
41  * <code>match</code> method of this object returns empty
42  * <code>Collection</code> if the value/object being validated is non
43  * <code>null</code>; else it returns a <code>Collection</code> with a
44  * {@link ConstraintFailure} object in it. <code>ConstraintUtils</code> class
45  * provides utility methods for formating failure messages and a
46  * <code>print<method> method to print this object.
47  *
48  * @author Rajeshwar Patil
49  * @version %I%, %G%
50  */

51 public class MandatoryConstraint extends ConstraintUtils
52                 implements Constraint{
53     
54     /** Creates a new instance of <code>MandatoryConstraint</code>. */
55     public MandatoryConstraint() {
56     }
57
58
59     /**
60      * Validates the given value against this <code>Constraint</code>.
61      *
62      * @param value the value to be validated.
63      * @param name the element name, value of which is being validated.
64      * It is used only in case of <code>Constraint</code> failure, to construct
65      * the failure message.
66      *
67      * @return <code>Collection</code> the Collection of
68      * <code>ConstraintFailure</code> Objects. Collection is empty if
69      * there are no failures.
70      */

71     public Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
72         return match((Object JavaDoc)value, name);
73     }
74
75
76     /**
77      * Validates the given value against this <code>Constraint</code>.
78      *
79      * @param value the value to be validated.
80      * @param name the element name, value of which is being validated.
81      * It is used only in case of <code>Constraint</code> failure to construct
82      * the faiulure message.
83      *
84      * @return <code>Collection</code> the Collection of
85      * <code>ConstraintFailure</code> Objects. Collection is empty if
86      * there are no failures. This method will fail, if the given vlaue is null.
87      */

88     public java.util.Collection JavaDoc match(Object JavaDoc value, String JavaDoc name) {
89         Collection JavaDoc failed_constrained_list = new ArrayList JavaDoc();
90         if (null == value){
91             String JavaDoc failureMessage = formatFailureMessage(toString(), name);
92             failed_constrained_list.add(new ConstraintFailure(toString(), value,
93                 name, failureMessage, BundleReader.getValue(
94                     "MSG_MandatoryConstraint_Failure"))); //NOI18N
95
}
96         return failed_constrained_list;
97     }
98 }
99
Popular Tags