1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.validator;
18
19 import java.util.Date;
20 import java.util.Locale;
21
22 import org.apache.commons.validator.util.ValidatorUtils;
23
24 /**
25 * Contains validation methods for different unit tests.
26 */
27 public class GenericTypeValidatorImpl {
28
29 /**
30 * Checks if the field can be successfully converted to a {@code byte}.
31 *
32 * @param bean The value validation is being performed on.
33 * @param field the field to use
34 * @return boolean If the field can be successfully converted to a {@code byte} {@code true} is returned. Otherwise {@code false}.
35 */
36 public static Byte validateByte(final Object bean, final Field field) {
37 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
38
39 return GenericTypeValidator.formatByte(value);
40 }
41
42 /**
43 * Checks if the field can be successfully converted to a {@code byte}.
44 *
45 * @param bean The value validation is being performed on.
46 * @param field the field to use
47 * @return boolean If the field can be successfully converted to a {@code byte} {@code true} is returned. Otherwise {@code false}.
48 */
49 public static Byte validateByte(final Object bean, final Field field, final Locale locale) {
50 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
51
52 return GenericTypeValidator.formatByte(value, locale);
53 }
54
55 /**
56 * Checks if the field can be successfully converted to a {@code date}.
57 *
58 * @param bean The value validation is being performed on.
59 * @param field the field to use
60 * @return boolean If the field can be successfully converted to a {@code date} {@code true} is returned. Otherwise {@code false}.
61 */
62 public static Date validateDate(final Object bean, final Field field) {
63 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
64 final String datePattern = field.getVarValue("datePattern");
65 final String datePatternStrict = field.getVarValue("datePatternStrict");
66
67 Date result = null;
68 if (datePattern != null && !datePattern.isEmpty()) {
69 result = GenericTypeValidator.formatDate(value, datePattern, false);
70 } else if (datePatternStrict != null && !datePatternStrict.isEmpty()) {
71 result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
72 }
73
74 return result;
75 }
76
77 /**
78 * Checks if the field can be successfully converted to a {@code date}.
79 *
80 * @param bean The value validation is being performed on.
81 * @param field the field to use
82 * @return boolean If the field can be successfully converted to a {@code date} {@code true} is returned. Otherwise {@code false}.
83 */
84 public static Date validateDate(final Object bean, final Field field, final Locale locale) {
85 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
86
87 return GenericTypeValidator.formatDate(value, locale);
88 }
89
90 /**
91 * Checks if the field can be successfully converted to a {@code double}.
92 *
93 * @param bean The value validation is being performed on.
94 * @param field the field to use
95 * @return boolean If the field can be successfully converted to a {@code double} {@code true} is returned. Otherwise {@code false}.
96 */
97 public static Double validateDouble(final Object bean, final Field field) {
98 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
99
100 return GenericTypeValidator.formatDouble(value);
101 }
102
103 /**
104 * Checks if the field can be successfully converted to a {@code double}.
105 *
106 * @param bean The value validation is being performed on.
107 * @param field the field to use
108 * @return boolean If the field can be successfully converted to a {@code double} {@code true} is returned. Otherwise {@code false}.
109 */
110 public static Double validateDouble(final Object bean, final Field field, final Locale locale) {
111 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
112
113 return GenericTypeValidator.formatDouble(value, locale);
114 }
115
116 /**
117 * Checks if the field can be successfully converted to a {@code float}.
118 *
119 * @param bean The value validation is being performed on.
120 * @param field the field to use
121 * @return boolean If the field can be successfully converted to a {@code float} {@code true} is returned. Otherwise {@code false}.
122 */
123 public static Float validateFloat(final Object bean, final Field field) {
124 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
125
126 return GenericTypeValidator.formatFloat(value);
127 }
128
129 /**
130 * Checks if the field can be successfully converted to a {@code float}.
131 *
132 * @param bean The value validation is being performed on.
133 * @param field the field to use
134 * @return boolean If the field can be successfully converted to a {@code float} {@code true} is returned. Otherwise {@code false}.
135 */
136 public static Float validateFloat(final Object bean, final Field field, final Locale locale) {
137 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
138
139 return GenericTypeValidator.formatFloat(value, locale);
140 }
141
142 /**
143 * Checks if the field can be successfully converted to a {@code int}.
144 *
145 * @param bean The value validation is being performed on.
146 * @param field the field to use
147 * @return boolean If the field can be successfully converted to a {@code int} {@code true} is returned. Otherwise {@code false}.
148 */
149 public static Integer validateInt(final Object bean, final Field field) {
150 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
151
152 return GenericTypeValidator.formatInt(value);
153 }
154
155 /**
156 * Checks if the field can be successfully converted to a {@code int}.
157 *
158 * @param bean The value validation is being performed on.
159 * @param field the field to use
160 * @return boolean If the field can be successfully converted to a {@code int} {@code true} is returned. Otherwise {@code false}.
161 */
162 public static Integer validateInt(final Object bean, final Field field, final Locale locale) {
163 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
164
165 return GenericTypeValidator.formatInt(value, locale);
166 }
167
168 /**
169 * Checks if the field can be successfully converted to a {@code long}.
170 *
171 * @param bean The value validation is being performed on.
172 * @param field the field to use
173 * @return boolean If the field can be successfully converted to a {@code long} {@code true} is returned. Otherwise {@code false}.
174 */
175 public static Long validateLong(final Object bean, final Field field) {
176 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
177
178 return GenericTypeValidator.formatLong(value);
179 }
180
181 /**
182 * Checks if the field can be successfully converted to a {@code long}.
183 *
184 * @param bean The value validation is being performed on.
185 * @param field the field to use
186 * @return boolean If the field can be successfully converted to a {@code long} {@code true} is returned. Otherwise {@code false}.
187 */
188 public static Long validateLong(final Object bean, final Field field, final Locale locale) {
189 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
190
191 return GenericTypeValidator.formatLong(value, locale);
192 }
193
194 /**
195 * Checks if the field can be successfully converted to a {@code short}.
196 *
197 * @param bean The value validation is being performed on.
198 * @param field the field to use
199 * @return boolean If the field can be successfully converted to a {@code short} {@code true} is returned. Otherwise {@code false}.
200 */
201 public static Short validateShort(final Object bean, final Field field) {
202 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
203
204 return GenericTypeValidator.formatShort(value);
205 }
206
207 /**
208 * Checks if the field can be successfully converted to a {@code short}.
209 *
210 * @param bean The value validation is being performed on.
211 * @param field the field to use
212 * @return boolean If the field can be successfully converted to a {@code short} {@code true} is returned. Otherwise {@code false}.
213 */
214 public static Short validateShort(final Object bean, final Field field, final Locale locale) {
215 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
216
217 return GenericTypeValidator.formatShort(value, locale);
218 }
219 }