001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.validation;
018
019import java.util.Iterator;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.commons.cli2.resource.ResourceConstants;
024import org.apache.commons.cli2.resource.ResourceHelper;
025
026/**
027 * The <code>EnumValidator</code> validates the string argument
028 * values are valid.
029 *
030 * The following example shows how to limit the valid values
031 * for the color argument to 'red', 'green', or 'blue'.
032 *
033 * <pre>
034 * Set values = new HashSet();
035 * values.add("red");
036 * values.add("green");
037 * values.add("blue");
038 * ...
039 * ArgumentBuilder builder = new ArgumentBuilder();
040 * Argument color =
041 *     builder.withName("color");
042 *            .withValidator(new EnumValidator(values));
043 * </pre>
044 *
045 * @author John Keyes
046 */
047public class EnumValidator implements Validator {
048    /** List of permitted values */
049    private Set validValues;
050
051    /**
052     * Creates a new EnumValidator for the specified values.
053     *
054     * @param values The list of permitted values
055     */
056    public EnumValidator(final Set values) {
057        setValidValues(values);
058    }
059
060    /**
061     * Validate the list of values against the list of permitted values.
062     *
063     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
064     */
065    public void validate(final List values)
066        throws InvalidArgumentException {
067        for (final Iterator iter = values.iterator(); iter.hasNext();) {
068            final String value = (String) iter.next();
069
070            if (!this.validValues.contains(value)) {
071                throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,
072                                                                                                 new Object[] {
073                                                                                                     value,
074                                                                                                     getValuesAsString()
075                                                                                                 }));
076            }
077        }
078    }
079
080    /**
081     * Returns the permitted values in a comma separated String
082     *
083     * @return String formatted list of values
084     */
085    String getValuesAsString() {
086        final StringBuffer buff = new StringBuffer();
087
088        buff.append("[");
089
090        for (final Iterator iter = this.validValues.iterator(); iter.hasNext();) {
091            buff.append("'").append(iter.next()).append("'");
092
093            if (iter.hasNext()) {
094                buff.append(", ");
095            }
096        }
097
098        buff.append("]");
099
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}