01: /*
02: * Copyright 2003 The Apache Software Foundation.
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:
17: package velosurf.validation;
18:
19: import java.util.regex.Pattern;
20: import java.util.List;
21: import java.util.Locale;
22:
23: import velosurf.util.StringLists;
24:
25: /**
26: * <p>An enumeration constraint. Syntax is:</p>
27: * <pre>
28: * <<i>column</i> one-of="<i>value1,value2,value3...</i>"/>
29: * </pre>
30: *<p>Or:</p>
31: * <pre>
32: * <<i>column</i>>
33: * <one-of [message="<i>error-message</i>"]>
34: * <value><i>value1</i></value>
35: * <value><i>value2</i></value>
36: * <value><i>value3</i></value>
37: * ...
38: * </one-of>
39: * </<i>column</i>>
40: * </pre>
41: * <p>Note: this constraint is not meant to replace an internal SQL enumeration constraint clause in the database,
42: * since it cannot be made sure that complex updates will respect this constraint.</p>
43: *
44: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
45: */
46: public class OneOf extends FieldConstraint {
47:
48: private List values = null;
49:
50: /**
51: * Constructor.
52: * @param values the list of possible values
53: */
54: public OneOf(List values) {
55: this .values = values;
56: setMessage("field {0}: value '{1}' must be one of: "
57: + StringLists.join(this .values, ", "));
58: }
59:
60: /**
61: * Validate data against this constraint.
62: * @param data the data to be validated
63: * @return true if data is among the expected values
64: */
65: public boolean validate(Object data) {
66: return data == null || values.contains(data.toString());
67: }
68:
69: /**
70: * return a string representation for this constraint.
71: * @return string
72: */
73: public String toString() {
74: return "one of " + StringLists.join(values, ", ");
75: }
76: }
|