Browse Source

Add an IndexOf method that returns the index of

the selected attribute in the enumeration.
The selected index is kept so that class extending
it can easily do a 1-1 mapping with values without
looping over the array again.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270597 13f79535-47bb-0310-9956-ffa450edef68
master
Stephane Bailliez 23 years ago
parent
commit
36acaa7bec
1 changed files with 31 additions and 9 deletions
  1. +31
    -9
      src/main/org/apache/tools/ant/types/EnumeratedAttribute.java

+ 31
- 9
src/main/org/apache/tools/ant/types/EnumeratedAttribute.java View File

@@ -67,8 +67,16 @@ import org.apache.tools.ant.BuildException;
*/ */
public abstract class EnumeratedAttribute { public abstract class EnumeratedAttribute {


/**
* The selected value in this enumeration.
*/
protected String value; protected String value;


/**
* the index of the selected value in the array.
*/
protected int index;

/** /**
* This is the only method a subclass needs to implement. * This is the only method a subclass needs to implement.
* *
@@ -76,15 +84,19 @@ public abstract class EnumeratedAttribute {
*/ */
public abstract String[] getValues(); public abstract String[] getValues();


public EnumeratedAttribute() {}
/** bean constructor */
public EnumeratedAttribute(){
}


/** /**
* Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}. * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
*/ */
public final void setValue(String value) throws BuildException { public final void setValue(String value) throws BuildException {
if (!containsValue(value)) {
throw new BuildException(value+" is not a legal value for this attribute");
int index = indexOfValue(value);
if (index == -1) {
throw new BuildException(value + " is not a legal value for this attribute");
} }
this.index = index;
this.value = value; this.value = value;
} }


@@ -92,17 +104,27 @@ public abstract class EnumeratedAttribute {
* Is this value included in the enumeration? * Is this value included in the enumeration?
*/ */
public final boolean containsValue(String value) { public final boolean containsValue(String value) {
return (indexOfValue(value) != -1);
}

/**
* 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()
*/
public final int indexOfValue(String value){
String[] values = getValues(); String[] values = getValues();
if (values == null || value == null) { if (values == null || value == null) {
return false;
return -1;
} }
for (int i=0; i<values.length; i++) {
if (value.equals(values[i])) {
return true;
for (int i = 0; i < values.length; i++){
if (value.equals(values[i])){
return i;
} }
} }
return false;
return -1;
} }


/** /**


Loading…
Cancel
Save