Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.validator.routines.LongValidator |
100% |
100% |

1 |  |
     /*
2 |  |
      * $Id: LongValidator.java 386637 2006-03-17 13:22:26Z niallp $
3 |  |
      * $Revision: 386637 $
4 |  |
      * $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
5 |  |
      *
6 |  |
      * ====================================================================
7 |  |
      * Copyright 2006 The Apache Software Foundation
8 |  |
      *
9 |  |
      * Licensed under the Apache License, Version 2.0 (the "License");
10 |  |
      * you may not use this file except in compliance with the License.
11 |  |
      * You may obtain a copy of the License at
12 |  |
      *
13 |  |
      *     http://www.apache.org/licenses/LICENSE-2.0
14 |  |
      *
15 |  |
      * Unless required by applicable law or agreed to in writing, software
16 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
17 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 |  |
      * See the License for the specific language governing permissions and
19 |  |
      * limitations under the License.
20 |  |
      */
21 |  |
     package org.apache.commons.validator.routines;
22 |  |
23 |  |
     import java.text.Format;
24 |  |
     import java.util.Locale;
25 |  |
26 |  |
     /**
27 |  |
      * <p><b>Long Validation</b> and Conversion routines (<code>java.lang.Long</code>).</p>
28 |  |
      *
29 |  |
      * <p>This validator provides a number of methods for
30 |  |
      *    validating/converting a <code>String</code> value to
31 |  |
      *    a <code>Long</code> using <code>java.text.NumberFormat</code>
32 |  |
      *    to parse either:</p>
33 |  |
      *    <ul>
34 |  |
      *       <li>using the default format for the default <code>Locale</code></li>
35 |  |
      *       <li>using a specified pattern with the default <code>Locale</code></li>
36 |  |
      *       <li>using the default format for a specified <code>Locale</code></li>
37 |  |
      *       <li>using a specified pattern with a specified <code>Locale</code></li>
38 |  |
      *    </ul>
39 |  |
      *
40 |  |
      * <p>Use one of the <code>isValid()</code> methods to just validate or
41 |  |
      *    one of the <code>validate()</code> methods to validate and receive a
42 |  |
      *    <i>converted</i> <code>Long</code> value.</p>
43 |  |
      *
44 |  |
      * <p>Once a value has been sucessfully converted the following
45 |  |
      *    methods can be used to perform minimum, maximum and range checks:</p>
46 |  |
      *    <ul>
47 |  |
      *       <li><code>minValue()</code> checks whether the value is greater
48 |  |
      *           than or equal to a specified minimum.</li>
49 |  |
      *       <li><code>maxValue()</code> checks whether the value is less
50 |  |
      *           than or equal to a specified maximum.</li>
51 |  |
      *       <li><code>isInRange()</code> checks whether the value is within
52 |  |
      *           a specified range of values.</li>
53 |  |
      *    </ul>
54 |  |
      *
55 |  |
      * <p>So that the same mechanism used for parsing an <i>input</i> value
56 |  |
      *    for validation can be used to format <i>output</i>, corresponding
57 |  |
      *    <code>format()</code> methods are also provided. That is you can
58 |  |
      *    format either:</p>
59 |  |
      *    <ul>
60 |  |
      *       <li>using a specified pattern</li>
61 |  |
      *       <li>using the format for a specified <code>Locale</code></li>
62 |  |
      *       <li>using the format for the <i>default</i> <code>Locale</code></li>
63 |  |
      *    </ul>
64 |  |
      *
65 |  |
      * @version $Revision: 386637 $ $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
66 |  |
      * @since Validator 1.3.0
67 |  |
      */
68 |  |
     public class LongValidator extends AbstractNumberValidator {
69 |  |
70 | 1 |
         private static final LongValidator VALIDATOR = new LongValidator();
71 |  |
72 |  |
         /**
73 |  |
          * Return a singleton instance of this validator.
74 |  |
          * @return A singleton instance of the LongValidator.
75 |  |
          */
76 |  |
         public static LongValidator getInstance() {
77 | 16 |
             return VALIDATOR;
78 |  |
         }
79 |  |
80 |  |
         /**
81 |  |
          * Construct a <i>strict</i> instance.
82 |  |
          */
83 |  |
         public LongValidator() {
84 | 12 |
             this(true, STANDARD_FORMAT);
85 | 12 |
         }
86 |  |
87 |  |
         /**
88 |  |
          * <p>Construct an instance with the specified strict setting
89 |  |
          *    and format type.</p>
90 |  |
          *
91 |  |
          * <p>The <code>formatType</code> specified what type of
92 |  |
          *    <code>NumberFormat</code> is created - valid types
93 |  |
          *    are:</p>
94 |  |
          *    <ul>
95 |  |
          *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
96 |  |
          *           <i>standard</i> number formats (the default).</li>
97 |  |
          *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
98 |  |
          *           <i>currency</i> number formats.</li>
99 |  |
          *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
100 |  |
          *           <i>percent</i> number formats (the default).</li>
101 |  |
          *    </ul>
102 |  |
          *
103 |  |
          * @param strict <code>true</code> if strict
104 |  |
          *        <code>Format</code> parsing should be used.
105 |  |
          * @param formatType The <code>NumberFormat</code> type to
106 |  |
          *        create for validation, default is STANDARD_FORMAT.
107 |  |
          */
108 |  |
         public LongValidator(boolean strict, int formatType) {
109 | 23 |
             super(strict, formatType, false);
110 | 23 |
         }
111 |  |
112 |  |
         /**
113 |  |
          * <p>Validate/convert a <code>Long</code> using the default
114 |  |
          *    <code>Locale</code>.
115 |  |
          *
116 |  |
          * @param value The value validation is being performed on.
117 |  |
          * @return The parsed <code>Long</code> if valid or <code>null</code>
118 |  |
          *  if invalid.
119 |  |
          */
120 |  |
         public Long validate(String value) {
121 | 2 |
             return (Long)parse(value, (String)null, (Locale)class="keyword">null);
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * <p>Validate/convert a <code>Long</code> using the
126 |  |
          *    specified <i>pattern</i>.
127 |  |
          *
128 |  |
          * @param value The value validation is being performed on.
129 |  |
          * @param pattern The pattern used to validate the value against.
130 |  |
          * @return The parsed <code>Long</code> if valid or <code>null</code> if invalid.
131 |  |
          */
132 |  |
         public Long validate(String value, String pattern) {
133 | 8 |
             return (Long)parse(value, pattern, (Locale)null);
134 |  |
         }
135 |  |
136 |  |
         /**
137 |  |
          * <p>Validate/convert a <code>Long</code> using the
138 |  |
          *    specified <code>Locale</code>.
139 |  |
          *
140 |  |
          * @param value The value validation is being performed on.
141 |  |
          * @param locale The locale to use for the number format, system default if null.
142 |  |
          * @return The parsed <code>Long</code> if valid or <code>null</code> if invalid.
143 |  |
          */
144 |  |
         public Long validate(String value, Locale locale) {
145 | 2 |
             return (Long)parse(value, (String)null, locale);
146 |  |
         }
147 |  |
148 |  |
         /**
149 |  |
          * <p>Validate/convert a <code>Long</code> using the
150 |  |
          *    specified pattern and/ or <code>Locale</code>.
151 |  |
          *
152 |  |
          * @param value The value validation is being performed on.
153 |  |
          * @param pattern The pattern used to validate the value against, or the
154 |  |
          *        default for the <code>Locale</code> if <code>null</code>.
155 |  |
          * @param locale The locale to use for the date format, system default if null.
156 |  |
          * @return The parsed <code>Long</code> if valid or <code>null</code> if invalid.
157 |  |
          */
158 |  |
         public Long validate(String value, String pattern, Locale locale) {
159 | 2 |
             return (Long)parse(value, pattern, locale);
160 |  |
         }
161 |  |
162 |  |
         /**
163 |  |
          * Check if the value is within a specified range.
164 |  |
          *
165 |  |
          * @param value The <code>Number</code> value to check.
166 |  |
          * @param min The minimum value of the range.
167 |  |
          * @param max The maximum value of the range.
168 |  |
          * @return <code>true</code> if the value is within the
169 |  |
          *         specified range.
170 |  |
          */
171 |  |
         public boolean isInRange(long value, class="keyword">long min, class="keyword">long max) {
172 | 5 |
             return (value >= min && value <= max);
173 |  |
         }
174 |  |
175 |  |
         /**
176 |  |
          * Check if the value is within a specified range.
177 |  |
          *
178 |  |
          * @param value The <code>Number</code> value to check.
179 |  |
          * @param min The minimum value of the range.
180 |  |
          * @param max The maximum value of the range.
181 |  |
          * @return <code>true</code> if the value is within the
182 |  |
          *         specified range.
183 |  |
          */
184 |  |
         public boolean isInRange(Long value, long min, class="keyword">long max) {
185 | 5 |
             return isInRange(value.longValue(), min, max);
186 |  |
         }
187 |  |
188 |  |
         /**
189 |  |
          * Check if the value is greater than or equal to a minimum.
190 |  |
          *
191 |  |
          * @param value The value validation is being performed on.
192 |  |
          * @param min The minimum value.
193 |  |
          * @return <code>true</code> if the value is greater than
194 |  |
          *         or equal to the minimum.
195 |  |
          */
196 |  |
         public boolean minValue(long value, class="keyword">long min) {
197 | 3 |
             return (value >= min);
198 |  |
         }
199 |  |
200 |  |
         /**
201 |  |
          * Check if the value is greater than or equal to a minimum.
202 |  |
          *
203 |  |
          * @param value The value validation is being performed on.
204 |  |
          * @param min The minimum value.
205 |  |
          * @return <code>true</code> if the value is greater than
206 |  |
          *         or equal to the minimum.
207 |  |
          */
208 |  |
         public boolean minValue(Long value, long min) {
209 | 3 |
             return minValue(value.longValue(), min);
210 |  |
         }
211 |  |
212 |  |
         /**
213 |  |
          * Check if the value is less than or equal to a maximum.
214 |  |
          *
215 |  |
          * @param value The value validation is being performed on.
216 |  |
          * @param max The maximum value.
217 |  |
          * @return <code>true</code> if the value is less than
218 |  |
          *         or equal to the maximum.
219 |  |
          */
220 |  |
         public boolean maxValue(long value, class="keyword">long max) {
221 | 3 |
             return (value <= max);
222 |  |
         }
223 |  |
224 |  |
         /**
225 |  |
          * Check if the value is less than or equal to a maximum.
226 |  |
          *
227 |  |
          * @param value The value validation is being performed on.
228 |  |
          * @param max The maximum value.
229 |  |
          * @return <code>true</code> if the value is less than
230 |  |
          *         or equal to the maximum.
231 |  |
          */
232 |  |
         public boolean maxValue(Long value, long max) {
233 | 3 |
             return maxValue(value.longValue(), max);
234 |  |
         }
235 |  |
236 |  |
         /**
237 |  |
          * Convert the parsed value to a <code>Long</code>.
238 |  |
          *
239 |  |
          * @param value The parsed <code>Number</code> object created.
240 |  |
          * @param formatter The Format used to parse the value with.
241 |  |
          * @return The parsed <code>Number</code> converted to a
242 |  |
          *         <code>Long</code>.
243 |  |
          */
244 |  |
         protected Object processParsedValue(Object value, Format formatter) {
245 |  |
246 | 49 |
             if (value instanceof Long) {
247 | 45 |
                 return value;
248 |  |
             } else {
249 | 4 |
                 return new Long(((Number)value).longValue());
250 |  |
             }
251 |  |
252 |  |
         }
253 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.