From 36acaa7bec9d8d452bfd8a3158a60b1a99af7714 Mon Sep 17 00:00:00 2001 From: Stephane Bailliez Date: Sun, 6 Jan 2002 17:04:29 +0000 Subject: [PATCH] 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 --- .../tools/ant/types/EnumeratedAttribute.java | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java index 48ca02958..241f7544d 100644 --- a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java +++ b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java @@ -67,8 +67,16 @@ import org.apache.tools.ant.BuildException; */ 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. * @@ -76,15 +84,19 @@ public abstract class EnumeratedAttribute { */ public abstract String[] getValues(); - public EnumeratedAttribute() {} + /** bean constructor */ + public EnumeratedAttribute(){ + } /** * Invoked by {@link 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 @@ public abstract class EnumeratedAttribute { * 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