1 17 package org.apache.ws.jaxme.js.pattern; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 import org.apache.ws.jaxme.js.JavaComment; 26 import org.apache.ws.jaxme.js.JavaConstructor; 27 import org.apache.ws.jaxme.js.JavaField; 28 import org.apache.ws.jaxme.js.JavaInnerClass; 29 import org.apache.ws.jaxme.js.JavaMethod; 30 import org.apache.ws.jaxme.js.JavaQName; 31 import org.apache.ws.jaxme.js.JavaQNameImpl; 32 import org.apache.ws.jaxme.js.JavaSource; 33 import org.apache.ws.jaxme.js.JavaSourceFactory; 34 import org.apache.ws.jaxme.js.Util; 35 36 44 public class TypesafeEnumerationGenerator { 45 public static class Item { 46 private String name; 47 private String value; 48 49 52 public void setName(String pName) { 53 if (pName != null) { 54 Util.checkJavaIdentifier(pName); 55 } 56 name = pName; 57 } 58 59 61 public String getName() { 62 return name; 63 } 64 65 67 public void setValue(String pValue) { 68 value = pValue; 69 } 70 71 73 public String getValue() { 74 return value; 75 } 76 77 80 public void finish() { 81 if (getValue() == null) { 82 throw new IllegalStateException ("The enumeration items 'value' attribute is not set."); 83 } 84 if (getName() == null) { 85 try { 86 Util.asJavaIdentifier(getValue().toUpperCase()); 87 } catch (Exception e) { 88 throw new IllegalStateException ("The enumeration items 'name' attribute is not set " + 89 " and the value " + getValue() + 90 " cannot be converted into a valid Java identifier."); 91 } 92 } 93 } 94 } 95 96 private boolean isAddingEquals = true; 97 98 102 public void setAddingEquals(boolean pAddingEquals) { 103 isAddingEquals = pAddingEquals; 104 } 105 106 110 public boolean isAddingEquals() { 111 return isAddingEquals; 112 } 113 114 120 public JavaSource generate(JavaSourceFactory pFactory, JavaQName pTargetClass, 121 Item[] pItems) { 122 JavaSource result = pFactory.newJavaSource(pTargetClass, JavaSource.PUBLIC); 123 doGenerate(result, pItems); 124 return result; 125 } 126 127 134 public JavaInnerClass generate(JavaSource pSource, String pName, Item[] pItems) { 135 JavaInnerClass result = pSource.newJavaInnerClass(pName, JavaSource.PUBLIC); 136 result.setStatic(true); 137 doGenerate(result, pItems); 138 return result; 139 } 140 141 144 private void doGenerate(JavaSource pSource, Item[] pItems) { 145 Set names = new HashSet (); 146 Set values = new HashSet (); 147 for (int i = 0; i < pItems.length; i++) { 148 String itemName = pItems[i].getName(); 149 String itemValue = pItems[i].getValue(); 150 if (itemName == null) { 151 itemName = Util.asJavaIdentifier(itemValue.toUpperCase()); 152 pItems[i].setName(itemName); 153 } 154 if (names.contains(itemName)) { 155 throw new IllegalStateException ("The item name " + itemName + " is not unique."); 156 } 157 if (values.contains(itemValue)) { 158 throw new IllegalStateException ("The item value " + itemValue + " is not unique."); 159 } 160 names.add(itemName); 161 names.add(itemValue); 162 } 163 164 pSource.addImplements(Serializable .class); 165 JavaField name = pSource.newJavaField("name", String .class, JavaSource.PRIVATE); 166 name.setFinal(true); 167 JavaField value = pSource.newJavaField("value", String .class, JavaSource.PRIVATE); 168 name.setFinal(true); 169 JavaConstructor jcon = pSource.newJavaConstructor(JavaSource.PRIVATE); 170 jcon.addParam(String .class, "pName"); 171 jcon.addParam(String .class, "pValue"); 172 jcon.addLine("name = pName;"); 173 jcon.addLine("value = pValue;"); 174 List instanceList = new ArrayList (); 175 for (int i = 0; i < pItems.length; i++) { 176 Item item = pItems[i]; 177 String itemName = item.getName(); 178 String itemValue = item.getValue(); 179 JavaField instance = pSource.newJavaField(itemName, pSource.getQName(), 180 JavaSource.PUBLIC); 181 instance.newComment().addLine("The enumeration item with name " + itemName + 182 " and value " + itemValue + "."); 183 instance.setStatic(true); 184 instance.setFinal(true); 185 instance.addLine("new ", pSource.getQName(), "(", JavaSource.getQuoted(itemName), 186 ", ", JavaSource.getQuoted(itemValue), ")"); 187 188 if (i > 0) instanceList.add(", "); 189 instanceList.add(instance); 190 } 191 192 JavaQName arrayType = JavaQNameImpl.getArray(pSource.getQName()); 193 JavaField allInstances = pSource.newJavaField("allInstances", arrayType, 194 JavaSource.PRIVATE); 195 allInstances.setStatic(true); 196 allInstances.setFinal(true); 197 allInstances.addLine("new ", arrayType, "{", instanceList, "}"); 198 199 JavaMethod getName = pSource.newJavaMethod("getName", String .class, 200 JavaSource.PUBLIC); 201 getName.newComment().addLine("The enumeration items name."); 202 getName.addLine("return ", name, ";"); 203 204 JavaMethod getValue = pSource.newJavaMethod("getValue", String .class, 205 JavaSource.PUBLIC); 206 getValue.newComment().addLine("The enumeration items value."); 207 getValue.addLine("return ", value, ";"); 208 209 JavaMethod getInstances = pSource.newJavaMethod("getInstances", arrayType, 210 JavaSource.PUBLIC); 211 getInstances.setStatic(true); 212 getInstances.addLine("return ", allInstances, ";"); 213 214 JavaMethod getInstanceByName = pSource.newJavaMethod("getInstanceByName", 215 pSource.getQName(), 216 JavaSource.PUBLIC); 217 getInstanceByName.setStatic(true); 218 getInstanceByName.addParam(String .class, "pName"); 219 JavaComment jc = getInstanceByName.newComment(); 220 jc.addLine("Returns the item with the given name.</p>"); 221 jc.addThrows(IllegalArgumentException .class.getName() + 222 " The name <code>pName</code> is invalid and no such item exists."); 223 getInstanceByName.addLine(String .class, " s = pName.intern();"); 224 boolean first = true; 225 for (int i = 0; i < pItems.length; i++) { 226 Item item = pItems[i]; 227 Object [] args = new Object []{JavaSource.getQuoted(item.getName()), " == s"}; 228 getInstanceByName.addIf(first, args); 229 getInstanceByName.addLine("return ", item.getName(), ";"); 230 first = false; 231 } 232 getInstanceByName.addElse(); 233 getInstanceByName.addLine("throw new ", IllegalArgumentException .class, "(", 234 JavaSource.getQuoted("Invalid name: "), 235 " + pName);"); 236 getInstanceByName.addEndIf(); 237 238 JavaMethod getInstanceByValue = pSource.newJavaMethod("getInstanceByValue", 239 pSource.getQName(), 240 JavaSource.PUBLIC); 241 getInstanceByValue.setStatic(true); 242 getInstanceByValue.addParam(String .class, "pValue"); 243 jc = getInstanceByValue.newComment(); 244 jc.addLine("Returns the item with the given value.</p>"); 245 jc.addThrows(IllegalArgumentException .class.getName() + 246 " The name <code>pValue</code> is invalid and no such item exists."); 247 getInstanceByValue.addLine(String .class, " s = pValue.intern();"); 248 first = true; 249 for (int i = 0; i < pItems.length; i++) { 250 Item item = pItems[i]; 251 Object [] args = new Object []{JavaSource.getQuoted(item.getValue()), " == s"}; 252 getInstanceByValue.addIf(first, args); 253 getInstanceByValue.addLine("return ", item.getName(), ";"); 254 first = false; 255 } 256 getInstanceByValue.addElse(); 257 getInstanceByValue.addLine("throw new ", IllegalArgumentException .class, "(", 258 JavaSource.getQuoted("Invalid name: "), 259 " + pValue);"); 260 getInstanceByValue.addEndIf(); 261 262 if (isAddingEquals()) { 263 JavaMethod equals = pSource.newJavaMethod("equals", JavaQNameImpl.BOOLEAN, 264 JavaSource.PUBLIC); 265 equals.addParam(Object .class, "o"); 266 equals.addIf("o == null || !(o instanceof ", pSource.getQName(), ")"); 267 equals.addLine("return false;"); 268 equals.addEndIf(); 269 equals.addLine("return name.equals(((", pSource.getQName(), ") o).name);"); 270 271 JavaMethod hashCode = pSource.newJavaMethod("hashCode", JavaQNameImpl.INT, 272 JavaSource.PUBLIC); 273 hashCode.addLine("return name.hashCode();"); 274 } 275 } 276 } 277 | Popular Tags |