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: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import org.apache.tools.ant.BuildException;
022:
023: /**
024: * Helper class for attributes that can only take one of a fixed list
025: * of values.
026: *
027: * <p>See {@link org.apache.tools.ant.taskdefs.FixCRLF FixCRLF} for an
028: * example.
029: *
030: */
031: public abstract class EnumeratedAttribute {
032: // CheckStyle:VisibilityModifier OFF - bc
033: /**
034: * The selected value in this enumeration.
035: */
036: protected String value;
037:
038: // CheckStyle:VisibilityModifier ON
039:
040: /**
041: * the index of the selected value in the array.
042: */
043: private int index = -1;
044:
045: /**
046: * This is the only method a subclass needs to implement.
047: *
048: * @return an array holding all possible values of the enumeration.
049: * The order of elements must be fixed so that <tt>indexOfValue(String)</tt>
050: * always return the same index for the same value.
051: */
052: public abstract String[] getValues();
053:
054: /** bean constructor */
055: protected EnumeratedAttribute() {
056: }
057:
058: /**
059: * Factory method for instantiating EAs via API in a more
060: * developer friendly way.
061: * @param clazz Class, extending EA, which to instantiate
062: * @param value The value to set on that EA
063: * @return Configured EA
064: * @throws BuildException If the class could not be found or the value
065: * is not valid for the given EA-class.
066: * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=14831">
067: * http://issues.apache.org/bugzilla/show_bug.cgi?id=14831</a>
068: */
069: public static EnumeratedAttribute getInstance(
070: Class/*<? extends EnumeratedAttribute>*/clazz, String value)
071: throws BuildException {
072: if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) {
073: throw new BuildException(
074: "You have to provide a subclass from EnumeratedAttribut as clazz-parameter.");
075: }
076: EnumeratedAttribute ea = null;
077: try {
078: ea = (EnumeratedAttribute) clazz.newInstance();
079: } catch (Exception e) {
080: throw new BuildException(e);
081: }
082: ea.setValue(value);
083: return ea;
084: }
085:
086: /**
087: * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
088: * @param value the <code>String</code> value of the attribute
089: * @throws BuildException if the value is not valid for the attribute
090: */
091: public final void setValue(String value) throws BuildException {
092: int idx = indexOfValue(value);
093: if (idx == -1) {
094: throw new BuildException(value
095: + " is not a legal value for this attribute");
096: }
097: this .index = idx;
098: this .value = value;
099: }
100:
101: /**
102: * Is this value included in the enumeration?
103: * @param value the <code>String</code> value to look up
104: * @return true if the value is valid
105: */
106: public final boolean containsValue(String value) {
107: return (indexOfValue(value) != -1);
108: }
109:
110: /**
111: * get the index of a value in this enumeration.
112: * @param value the string value to look for.
113: * @return the index of the value in the array of strings
114: * or -1 if it cannot be found.
115: * @see #getValues()
116: */
117: public final int indexOfValue(String value) {
118: String[] values = getValues();
119: if (values == null || value == null) {
120: return -1;
121: }
122: for (int i = 0; i < values.length; i++) {
123: if (value.equals(values[i])) {
124: return i;
125: }
126: }
127: return -1;
128: }
129:
130: /**
131: * @return the selected value.
132: */
133: public final String getValue() {
134: return value;
135: }
136:
137: /**
138: * @return the index of the selected value in the array.
139: * @see #getValues()
140: */
141: public final int getIndex() {
142: return index;
143: }
144:
145: /**
146: * Convert the value to its string form.
147: *
148: * @return the string form of the value.
149: */
150: public String toString() {
151: return getValue();
152: }
153:
154: }
|