/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
private void validateTagUniqueness(Linker linker) { Multimap<Integer, EnumConstant> tagToConstant = LinkedHashMultimap.create(); for (EnumConstant constant : constants) { tagToConstant.put(constant.tag(), constant); } for (Map.Entry<Integer, Collection<EnumConstant>> entry : tagToConstant.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); error.append(String.format("multiple enum constants share tag %s:", entry.getKey())); int index = 1; for (EnumConstant constant : entry.getValue()) { error.append(String.format("\n %s. %s (%s)", index++, constant.name(), constant.location())); } linker.addError("%s", error); } } }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
for (EnumConstant constant : type.constants()) { String name = nameAllocator.get(constant); toValueBuilder.addStatement("if ($N.equals($N)) return $L", value, name, constant.tag()); for (EnumConstant constant : type.constants()) { String name = nameAllocator.get(constant); fromValueBuilder.addStatement("case $L: return $N", constant.tag(), name);
@Test public void duplicateEnumConstantTagWithAllowAliasTrueAllowed() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "enum Enum {\n" + " option allow_alias = true;\n" + " A = 1;\n" + " B = 1;\n" + "}\n") .schema(); EnumType enumType = (EnumType) schema.getType("Enum"); assertThat(enumType.constant("A").tag()).isEqualTo(1); assertThat(enumType.constant("B").tag()).isEqualTo(1); }
for (EnumConstant constant : type.constants()) { Object[] enumArgs = new Object[allOptionMembers.size() + 1]; enumArgs[0] = constant.tag(); for (int i = 0; i < allOptionMembers.size(); i++) { ProtoMember protoMember = allOptionMembers.get(i); if (seenTags.add(constant.tag())) { fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());
/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
private void validateTagUniqueness(Linker linker) { Multimap<Integer, EnumConstant> tagToConstant = LinkedHashMultimap.create(); for (EnumConstant constant : constants) { tagToConstant.put(constant.tag(), constant); } for (Map.Entry<Integer, Collection<EnumConstant>> entry : tagToConstant.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); error.append(String.format("multiple enum constants share tag %s:", entry.getKey())); int index = 1; for (EnumConstant constant : entry.getValue()) { error.append(String.format("\n %s. %s (%s)", index++, constant.name(), constant.location())); } linker.addError("%s", error); } } }
private void validateTagUniqueness(Linker linker) { Multimap<Integer, EnumConstant> tagToConstant = LinkedHashMultimap.create(); for (EnumConstant constant : constants) { tagToConstant.put(constant.tag(), constant); } for (Map.Entry<Integer, Collection<EnumConstant>> entry : tagToConstant.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); error.append(String.format("multiple enum constants share tag %s:", entry.getKey())); int index = 1; for (EnumConstant constant : entry.getValue()) { error.append(String.format("\n %s. %s (%s)", index++, constant.name(), constant.location())); } linker.addError("%s", error); } } }
private String getDescriptionFromEnumType(EnumType type) { String format = "* %d - %s %s\n"; StringBuffer sb = new StringBuffer(); sb.append(type.documentation()); sb.append("\n"); for (EnumConstant enumConstant : type.constants()) { sb.append(String.format(format, enumConstant.tag(), enumConstant.name(),enumConstant.documentation())); } return sb.toString(); }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
for (EnumConstant constant : type.constants()) { Object[] enumArgs = new Object[allOptionMembers.size() + 1]; enumArgs[0] = constant.tag(); for (int i = 0; i < allOptionMembers.size(); i++) { ProtoMember protoMember = allOptionMembers.get(i); if (seenTags.add(constant.tag())) { fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());
for (EnumConstant constant : type.constants()) { Object[] enumArgs = new Object[allOptionMembers.size() + 1]; enumArgs[0] = constant.tag(); for (int i = 0; i < allOptionMembers.size(); i++) { ProtoMember protoMember = allOptionMembers.get(i); if (seenTags.add(constant.tag())) { fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());