01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.calyxo.forms.check;
17:
18: /**
19: * Check string length.
20: * A <code>null</code> value is treated as length <code>0</code>.
21: *
22: * @author Christoph Beck
23: */
24: public class LengthChecker extends SimpleChecker {
25: private int min;
26: private int max;
27:
28: /**
29: * Constructor
30: */
31: public LengthChecker() {
32: this (0, Integer.MAX_VALUE);
33: }
34:
35: /**
36: * Constructor
37: */
38: public LengthChecker(int min, int max) {
39: super ();
40:
41: setMin(min);
42: setMax(max);
43: }
44:
45: /* (non-Javadoc)
46: * @see de.odysseus.calyxo.forms.check.SimpleChecker#check(java.lang.Object)
47: */
48: public boolean check(Object value) {
49: int length = value == null ? 0 : value.toString().length();
50: return min <= length && length <= max;
51: }
52:
53: /**
54: * This checker is thread-save, so answer <code>true</code>
55: */
56: public boolean isSharable() {
57: return true;
58: }
59:
60: /**
61: * @return Returns the max.
62: */
63: public int getMax() {
64: return max;
65: }
66:
67: /**
68: * @param max The max to set.
69: */
70: public void setMax(int max) {
71: this .max = max;
72: }
73:
74: /**
75: * @return Returns the min.
76: */
77: public int getMin() {
78: return min;
79: }
80:
81: /**
82: * @param min The min to set.
83: */
84: public void setMin(int min) {
85: this .min = min;
86: }
87:
88: protected String paramsToString() {
89: return ", " + min + " <= length <= " + max;
90: }
91: }
|