JavaSource.PRIVATE);
allInstances.setStatic(true);
allInstances.setFinal(true);
allInstances.addLine("new ", arrayType, "{", instanceList, "}");
JavaMethod getName = pSource.newJavaMethod("getName", String.class,
JavaSource.PUBLIC);
getName.newComment().addLine("The enumeration items name.");
getName.addLine("return ", name, ";");
JavaMethod getValue = pSource.newJavaMethod("getValue", String.class,
JavaSource.PUBLIC);
getValue.newComment().addLine("The enumeration items value.");
getValue.addLine("return ", value, ";");
JavaMethod getInstances = pSource.newJavaMethod("getInstances", arrayType,
JavaSource.PUBLIC);
getInstances.setStatic(true);
getInstances.addLine("return ", allInstances, ";");
JavaMethod getInstanceByName = pSource.newJavaMethod("getInstanceByName",
pSource.getQName(),
JavaSource.PUBLIC);
getInstanceByName.setStatic(true);
getInstanceByName.addParam(String.class, "pName");
JavaComment jc = getInstanceByName.newComment();
jc.addLine("Returns the item with the given name.</p>");
jc.addThrows(IllegalArgumentException.class.getName() +
" The name <code>pName</code> is invalid and no such item exists.");
getInstanceByName.addLine(String.class, " s = pName.intern();");
boolean first = true;
for (int i = 0; i < pItems.length; i++) {
Item item = pItems[i];
Object[] args = new Object[]{JavaSource.getQuoted(item.getName()), " == s"};
getInstanceByName.addIf(first, args);
getInstanceByName.addLine("return ", item.getName(), ";");
first = false;
}
getInstanceByName.addElse();
getInstanceByName.addLine("throw new ", IllegalArgumentException.class, "(",
JavaSource.getQuoted("Invalid name: "),
" + pName);");
getInstanceByName.addEndIf();
JavaMethod getInstanceByValue = pSource.newJavaMethod("getInstanceByValue",
pSource.getQName(),
JavaSource.PUBLIC);
getInstanceByValue.setStatic(true);
getInstanceByValue.addParam(String.class, "pValue");
jc = getInstanceByValue.newComment();
jc.addLine("Returns the item with the given value.</p>");
jc.addThrows(IllegalArgumentException.class.getName() +
" The name <code>pValue</code> is invalid and no such item exists.");
getInstanceByValue.addLine(String.class, " s = pValue.intern();");
first = true;
for (int i = 0; i < pItems.length; i++) {
Item item = pItems[i];
Object[] args = new Object[]{JavaSource.getQuoted(item.getValue()), " == s"};
getInstanceByValue.addIf(first, args);
getInstanceByValue.addLine("return ", item.getName(), ";");
first = false;
}
getInstanceByValue.addElse();
getInstanceByValue.addLine("throw new ", IllegalArgumentException.class, "(",
JavaSource.getQuoted("Invalid name: "),
" + pValue);");
getInstanceByValue.addEndIf();
if (isAddingEquals()) {
JavaMethod equals = pSource.newJavaMethod("equals", JavaQNameImpl.BOOLEAN,
JavaSource.PUBLIC);
equals.addParam(Object.class, "o");
equals.addIf("o == null || !(o instanceof ", pSource.getQName(), ")");
equals.addLine("return false;");
equals.addEndIf();
equals.addLine("return name.equals(((", pSource.getQName(), ") o).name);");
JavaMethod hashCode = pSource.newJavaMethod("hashCode", JavaQNameImpl.INT,
JavaSource.PUBLIC);
hashCode.addLine("return name.hashCode();");
}
}