KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > validator > LongValidator


1 package org.apache.fulcrum.intake.validator;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Map JavaDoc;
58 import org.apache.fulcrum.ServiceException;
59
60 /**
61  * Validates numbers with the following constraints in addition to those
62  * listed in DefaultValidator.
63  *
64  * <table>
65  * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
66  * <tr><td>minValue</td><td>greater than Long.MIN_VALUE</td>
67  * <td>&nbsp;</td></tr>
68  * <tr><td>maxValue</td><td>less than Long.MAX_VALUE</td>
69  * <td>&nbsp;</td></tr>
70  * <tr><td>notANumberMessage</td><td>Some text</td>
71  * <td>Entry was not a valid number</td></tr>
72  * </table>
73  *
74  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
75  * @version $Id: LongValidator.java,v 1.1 2004/11/12 10:26:02 epugh Exp $
76  */

77 public class LongValidator
78     extends NumberValidator
79 {
80     private static String JavaDoc INVALID_NUMBER = "Entry was not a valid integer";
81
82     private long minValue;
83     private long maxValue;
84
85     public LongValidator(Map JavaDoc paramMap)
86         throws ServiceException
87     {
88         this();
89         init(paramMap);
90     }
91
92     public LongValidator()
93     {
94         // sets the default invalid number message
95
super();
96     }
97
98     protected void doInit(Map JavaDoc paramMap)
99     {
100         minValue = Long.MIN_VALUE;
101         maxValue = Long.MAX_VALUE;
102
103         Constraint constraint = (Constraint)paramMap.get("minValue");
104         if ( constraint != null )
105         {
106             String JavaDoc param = constraint.getValue();
107             minValue = Long.parseLong(param);
108             minValueMessage = constraint.getMessage();
109         }
110
111         constraint = (Constraint)paramMap.get("maxValue");
112         if ( constraint != null )
113         {
114             String JavaDoc param = constraint.getValue();
115             maxValue = Long.parseLong(param);
116             maxValueMessage = constraint.getMessage();
117         }
118     }
119
120     protected String JavaDoc getDefaultInvalidNumberMessage()
121     {
122         return INVALID_NUMBER;
123     }
124
125     /**
126      * Determine whether a testValue meets the criteria specified
127      * in the constraints defined for this validator
128      *
129      * @param testValue a <code>String</code> to be tested
130      * @exception ValidationException containing an error message if the
131      * testValue did not pass the validation tests.
132      */

133     protected void doAssertValidity(String JavaDoc testValue)
134         throws ValidationException
135     {
136         long i = 0;
137         try
138         {
139             i = Long.parseLong(testValue);
140         }
141         catch (RuntimeException JavaDoc e)
142         {
143             message = invalidNumberMessage;
144             throw new ValidationException(invalidNumberMessage);
145         }
146
147         if ( i < minValue )
148         {
149             message = minValueMessage;
150             throw new ValidationException(minValueMessage);
151         }
152         if ( i > maxValue )
153         {
154             message = maxValueMessage;
155             throw new ValidationException(maxValueMessage);
156         }
157     }
158
159
160     // ************************************************************
161
// ** Bean accessor methods **
162
// ************************************************************
163

164     /**
165      * Get the value of minValue.
166      * @return value of minValue.
167      */

168     public long getMinValue()
169     {
170         return minValue;
171     }
172
173     /**
174      * Set the value of minValue.
175      * @param v Value to assign to minValue.
176      */

177     public void setMinValue(long v)
178     {
179         this.minValue = v;
180     }
181
182     /**
183      * Get the value of maxValue.
184      * @return value of maxValue.
185      */

186     public long getMaxValue()
187     {
188         return maxValue;
189     }
190
191     /**
192      * Set the value of maxValue.
193      * @param v Value to assign to maxValue.
194      */

195     public void setMaxValue(long v)
196     {
197         this.maxValue = v;
198     }
199 }
200
Popular Tags