sbailliez 02/01/06 09:04:29
Modified: src/main/org/apache/tools/ant/types EnumeratedAttribute.java
Log:
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.
Revision Changes Path
1.3 +31 -9
jakarta-ant/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
Index: EnumeratedAttribute.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- EnumeratedAttribute.java 29 Nov 2000 16:11:33 -0000 1.2
+++ EnumeratedAttribute.java 6 Jan 2002 17:04:29 -0000 1.3
@@ -67,24 +67,36 @@
*/
public abstract class EnumeratedAttribute {
+ /**
+ * The selected value in this enumeration.
+ */
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.
*
* @return an array holding all possible values of the enumeration.
*/
public abstract String[] getValues();
- public EnumeratedAttribute() {}
+ /** bean constructor */
+ public EnumeratedAttribute(){
+ }
/**
* Invoked by [EMAIL PROTECTED] org.apache.tools.ant.IntrospectionHelper
IntrospectionHelper}.
*/
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;
}
@@ -92,17 +104,27 @@
* Is this value included in the enumeration?
*/
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();
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;
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>