1 18 19 package org.apache.tools.ant.types; 20 21 import org.apache.tools.ant.BuildException; 22 23 31 public abstract class EnumeratedAttribute { 32 36 protected String value; 37 38 40 43 private int index = -1; 44 45 52 public abstract String [] getValues(); 53 54 55 protected EnumeratedAttribute() { 56 } 57 58 69 public static EnumeratedAttribute getInstance( 70 Class clazz, 71 String value) throws BuildException { 72 if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) { 73 throw new BuildException( 74 "You have to provide a subclass from EnumeratedAttribut as clazz-parameter."); 75 } 76 EnumeratedAttribute ea = null; 77 try { 78 ea = (EnumeratedAttribute) clazz.newInstance(); 79 } catch (Exception e) { 80 throw new BuildException(e); 81 } 82 ea.setValue(value); 83 return ea; 84 } 85 86 91 public final void setValue(String value) throws BuildException { 92 int idx = indexOfValue(value); 93 if (idx == -1) { 94 throw new BuildException(value + " is not a legal value for this attribute"); 95 } 96 this.index = idx; 97 this.value = value; 98 } 99 100 105 public final boolean containsValue(String value) { 106 return (indexOfValue(value) != -1); 107 } 108 109 116 public final int indexOfValue(String value) { 117 String [] values = getValues(); 118 if (values == null || value == null) { 119 return -1; 120 } 121 for (int i = 0; i < values.length; i++) { 122 if (value.equals(values[i])) { 123 return i; 124 } 125 } 126 return -1; 127 } 128 129 132 public final String getValue() { 133 return value; 134 } 135 136 140 public final int getIndex() { 141 return index; 142 } 143 144 149 public String toString() { 150 return getValue(); 151 } 152 153 } 154 | Popular Tags |