FileDocCategorySizeDatePackage
EnumeratedAttribute.javaAPI DocApache Ant 1.704861Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types

EnumeratedAttribute

public abstract class EnumeratedAttribute extends Object
Helper class for attributes that can only take one of a fixed list of values.

See {@link org.apache.tools.ant.taskdefs.FixCRLF FixCRLF} for an example.

Fields Summary
protected String
value
The selected value in this enumeration.
private int
index
the index of the selected value in the array.
Constructors Summary
protected EnumeratedAttribute()
bean constructor


                                                
       

       
      
    
Methods Summary
public final booleancontainsValue(java.lang.String value)
Is this value included in the enumeration?

param
value the String value to look up
return
true if the value is valid

        return (indexOfValue(value) != -1);
    
public final intgetIndex()

return
the index of the selected value in the array.
see
#getValues()

        return index;
    
public static org.apache.tools.ant.types.EnumeratedAttributegetInstance(java.lang.Class clazz, java.lang.String value)
Factory method for instantiating EAs via API in a more developer friendly way.

param
clazz Class, extending EA, which to instantiate
param
value The value to set on that EA
return
Configured EA
throws
BuildException If the class could not be found or the value is not valid for the given EA-class.
see
http://issues.apache.org/bugzilla/show_bug.cgi?id=14831

        if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) {
            throw new BuildException(
                "You have to provide a subclass from EnumeratedAttribut as clazz-parameter.");
        }
        EnumeratedAttribute ea = null;
        try {
            ea = (EnumeratedAttribute) clazz.newInstance();
        } catch (Exception e) {
            throw new BuildException(e);
        }
        ea.setValue(value);
        return ea;
    
public final java.lang.StringgetValue()

return
the selected value.

        return value;
    
public abstract java.lang.String[]getValues()
This is the only method a subclass needs to implement.

return
an array holding all possible values of the enumeration. The order of elements must be fixed so that indexOfValue(String) always return the same index for the same value.

public final intindexOfValue(java.lang.String value)
get the index of a value in this enumeration.

param
value the string value to look for.
return
the index of the value in the array of strings or -1 if it cannot be found.
see
#getValues()

        String[] values = getValues();
        if (values == null || value == null) {
            return -1;
        }
        for (int i = 0; i < values.length; i++) {
            if (value.equals(values[i])) {
                return i;
            }
        }
        return -1;
    
public final voidsetValue(java.lang.String value)
Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.

param
value the String value of the attribute
throws
BuildException if the value is not valid for the attribute

        int idx = indexOfValue(value);
        if (idx == -1) {
            throw new BuildException(value + " is not a legal value for this attribute");
        }
        this.index = idx;
        this.value = value;
    
public java.lang.StringtoString()
Convert the value to its string form.

return
the string form of the value.

        return getValue();