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 * http://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.cli2.validation;
18
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.apache.commons.cli2.resource.ResourceConstants;
24 import org.apache.commons.cli2.resource.ResourceHelper;
25
26 /**
27 * The <code>EnumValidator</code> validates the string argument
28 * values are valid.
29 *
30 * The following example shows how to limit the valid values
31 * for the color argument to 'red', 'green', or 'blue'.
32 *
33 * <pre>
34 * Set values = new HashSet();
35 * values.add("red");
36 * values.add("green");
37 * values.add("blue");
38 * ...
39 * ArgumentBuilder builder = new ArgumentBuilder();
40 * Argument color =
41 * builder.withName("color");
42 * .withValidator(new EnumValidator(values));
43 * </pre>
44 *
45 * @author John Keyes
46 */
47 public class EnumValidator implements Validator {
48 /** List of permitted values */
49 private Set validValues;
50
51 /**
52 * Creates a new EnumValidator for the specified values.
53 *
54 * @param values The list of permitted values
55 */
56 public EnumValidator(final Set values) {
57 setValidValues(values);
58 }
59
60 /**
61 * Validate the list of values against the list of permitted values.
62 *
63 * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
64 */
65 public void validate(final List values)
66 throws InvalidArgumentException {
67 for (final Iterator iter = values.iterator(); iter.hasNext();) {
68 final String value = (String) iter.next();
69
70 if (!this.validValues.contains(value)) {
71 throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,
72 new Object[] {
73 value,
74 getValuesAsString()
75 }));
76 }
77 }
78 }
79
80 /**
81 * Returns the permitted values in a comma separated String
82 *
83 * @return String formatted list of values
84 */
85 String getValuesAsString() {
86 final StringBuffer buff = new StringBuffer();
87
88 buff.append("[");
89
90 for (final Iterator iter = this.validValues.iterator(); iter.hasNext();) {
91 buff.append("'").append(iter.next()).append("'");
92
93 if (iter.hasNext()) {
94 buff.append(", ");
95 }
96 }
97
98 buff.append("]");
99
100 return buff.toString();
101 }
102
103 /**
104 * Returns the Set of valid argument values.
105 *
106 * @return Returns the Set of valid argument values.
107 */
108 public Set getValidValues() {
109 return validValues;
110 }
111
112 /**
113 * Specifies the Set of valid argument values.
114 *
115 * @param validValues The Set of valid argument values.
116 */
117 protected void setValidValues(Set validValues) {
118 this.validValues = validValues;
119 }
120 }