| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EnumValidator |
|
| 2.0;2 |
| 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 | 3 | public EnumValidator(final Set values) { |
| 57 | 3 | setValidValues(values); |
| 58 | 3 | } |
| 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 | 3 | for (final Iterator iter = values.iterator(); iter.hasNext();) { |
| 68 | 5 | final String value = (String) iter.next(); |
| 69 | ||
| 70 | 5 | if (!this.validValues.contains(value)) { |
| 71 | 1 | throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE, |
| 72 | new Object[] { | |
| 73 | value, | |
| 74 | getValuesAsString() | |
| 75 | })); | |
| 76 | } | |
| 77 | 4 | } |
| 78 | 2 | } |
| 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 | 2 | final StringBuffer buff = new StringBuffer(); |
| 87 | ||
| 88 | 2 | buff.append("["); |
| 89 | ||
| 90 | 2 | for (final Iterator iter = this.validValues.iterator(); iter.hasNext();) { |
| 91 | 6 | buff.append("'").append(iter.next()).append("'"); |
| 92 | ||
| 93 | 6 | if (iter.hasNext()) { |
| 94 | 4 | buff.append(", "); |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | 2 | buff.append("]"); |
| 99 | ||
| 100 | 2 | 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 | 1 | 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 | 3 | this.validValues = validValues; |
| 119 | 3 | } |
| 120 | } |