in activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/AltJavaGenerator.java [2034:2131]
private void generateEnum(EnumDescriptor ed) {
String uname = uCamel(ed.getName());
String staticOption = "static ";
if( multipleFiles && ed.getParent()==null ) {
staticOption="";
}
// TODO Auto-generated method stub
p();
p("public "+staticOption+"enum " +uname + " {");
indent();
p();
int counter=0;
for (EnumFieldDescriptor field : ed.getFields().values()) {
boolean last = counter+1 == ed.getFields().size();
p(field.getName()+"(\""+field.getName()+"\", "+field.getValue()+")"+(last?";":","));
counter++;
}
p();
p("private final String name;");
p("private final int value;");
p();
p("private "+uname+"(String name, int value) {");
p(" this.name = name;");
p(" this.value = value;");
p("}");
p();
p("public final int getNumber() {");
p(" return value;");
p("}");
p();
p("public final String toString() {");
p(" return name;");
p("}");
p();
p("public static "+uname+" valueOf(int value) {");
p(" switch (value) {");
// It's possible to define multiple ENUM fields with the same value..
// we only want to put the first one into the switch statement.
HashSet<Integer> values = new HashSet<Integer>();
for (EnumFieldDescriptor field : ed.getFields().values()) {
if( !values.contains(field.getValue()) ) {
p(" case "+field.getValue()+":");
p(" return "+field.getName()+";");
values.add(field.getValue());
}
}
p(" default:");
p(" return null;");
p(" }");
p("}");
p();
String createMessage = getOption(ed.getOptions(), "java_create_message", null);
if( "true".equals(createMessage) ) {
p("public interface "+uname+"Creatable {");
indent();
p(""+uname+" to"+uname+"();");
unindent();
p("}");
p();
p("public "+uname+"Creatable createBean() {");
indent();
p("switch (this) {");
indent();
for (EnumFieldDescriptor field : ed.getFields().values()) {
p("case "+field.getName()+":");
String type = field.getAssociatedType().getName();
p(" return new "+javaRelatedType(type, "Bean")+"();");
}
p("default:");
p(" return null;");
unindent();
p("}");
unindent();
p("}");
p();
generateParseDelegate(ed, "parseUnframed", "org.apache.activemq.protobuf.Buffer", "org.apache.activemq.protobuf.InvalidProtocolBufferException");
generateParseDelegate(ed, "parseFramed", "org.apache.activemq.protobuf.Buffer", "org.apache.activemq.protobuf.InvalidProtocolBufferException");
generateParseDelegate(ed, "parseUnframed", "byte[]", "org.apache.activemq.protobuf.InvalidProtocolBufferException");
generateParseDelegate(ed, "parseFramed", "byte[]", "org.apache.activemq.protobuf.InvalidProtocolBufferException");
generateParseDelegate(ed, "parseFramed", "org.apache.activemq.protobuf.CodedInputStream", "org.apache.activemq.protobuf.InvalidProtocolBufferException, java.io.IOException");
generateParseDelegate(ed, "parseFramed", "java.io.InputStream", "org.apache.activemq.protobuf.InvalidProtocolBufferException, java.io.IOException");
}
unindent();
p("}");
p();
}