1 /*
2 Copyright (c) 2003, Vilmantas Baranauskas
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 * The names of its contributors may not be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 package org.sovt.impl;
29
30 import org.sovt.ValidationResult;
31
32 import java.text.MessageFormat;
33
34 /***
35 * This validator checks length of the string value. "toString()" method is
36 * invoked on the object first. "null" objects are skipped.
37 *
38 * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
39 */
40 public final class LengthValidator extends AbstractValidator
41 implements Cloneable {
42
43 /***
44 * Minimum length of value.
45 */
46 private int min;
47
48 /***
49 * Maximum length of value.
50 */
51 private int max;
52
53 //-- constructors ----------------------------------------------------------
54 /***
55 * Creates instance of LengthValidator.
56 */
57 public LengthValidator() {
58 }
59
60 /***
61 * Just returns super.clone(). There is no need to do a deep-cloning.
62 *
63 * @return clone of itself.
64 * @throws CloneNotSupportedException
65 */
66 public Object clone() throws CloneNotSupportedException {
67 return super.clone();
68 }
69
70 /***
71 * Returns maximum length of value.
72 *
73 * @return maximum length of value.
74 */
75 public int getMax() {
76 return max;
77 }
78
79 /***
80 * Returns minimum length of value.
81 *
82 * @return minimum length of value.
83 */
84 public int getMin() {
85 return min;
86 }
87
88 /***
89 * Sets maximum length of value.
90 *
91 * @param pMax maximum length of value.
92 */
93 public void setMax(int pMax) {
94 max = pMax;
95 }
96
97 /***
98 * Sets minimumlength of value.
99 *
100 * @param pMin minimum length of value.
101 */
102 public void setMin(int pMin) {
103 min = pMin;
104 }
105
106 /***
107 * Validates if object string value is in specified range.
108 *
109 * @param object Object to validate
110 * @param result Container for validation results.
111 */
112 public void validate(Object object, ValidationResult result) {
113 if (result.isValid()) {
114 String str = object.toString();
115 if (str.length() < min || str.length() > max) {
116 // validation failed
117 result.fail(MessageFormat.format(
118 getMessage(),
119 new Object[]{
120 new Integer(min),
121 new Integer(max)
122 }
123 ));
124 }
125 }
126 }
127 }