for (ProtoMember protoMember : constant.options().map().keySet()) { Field optionField = schema.getField(protoMember); if (allOptionFieldsBuilder.add(protoMember)) { 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); Field field = schema.getField(protoMember); Object fieldValue = constant.options().map().get(protoMember); enumArgs[i + 1] = fieldValue != null ? fieldInitializer(field.type(), fieldValue) if (!constant.documentation().isEmpty()) { constantBuilder.addJavadoc("$L\n", sanitizeJavadoc(constant.documentation())); if ("true".equals(constant.options().get(ENUM_DEPRECATED))) { constantBuilder.addAnnotation(Deprecated.class); builder.addEnumConstant(constant.name(), constantBuilder.build()); if (seenTags.add(constant.tag())) { fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());
EnumConstant retainAll(Schema schema, MarkSet markSet) { return new EnumConstant(location, name, tag, documentation, options.retainAll(schema, markSet)); }
static ImmutableList<EnumConstant> fromElements(ImmutableList<EnumConstantElement> elements) { ImmutableList.Builder<EnumConstant> constants = ImmutableList.builder(); for (EnumConstantElement element : elements) { constants.add(fromElement(element)); } return constants.build(); }
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 Type retainAll(Schema schema, MarkSet markSet) { // If this type is not retained, prune it. if (!markSet.contains(protoType)) return null; ImmutableList.Builder<EnumConstant> retainedConstants = ImmutableList.builder(); for (EnumConstant constant : constants) { if (markSet.contains(ProtoMember.get(protoType, constant.name()))) { retainedConstants.add(constant.retainAll(schema, markSet)); } } EnumType result = new EnumType(protoType, location, documentation, name, retainedConstants.build(), options.retainAll(schema, markSet)); result.allowAlias = allowAlias; return result; }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
FieldSpec.Builder fieldBuilder = FieldSpec.builder(javaType, name) .addModifiers(PROTECTED, FINAL); if (!constant.documentation().isEmpty()) { fieldBuilder.addJavadoc("$L\n", sanitizeJavadoc(constant.documentation())); if ("true".equals(constant.options().get(ENUM_DEPRECATED))) { fieldBuilder.addAnnotation(Deprecated.class); 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);
/** Returns the constant named {@code name}, or null if this enum has no such constant. */ public EnumConstant constant(String name) { for (EnumConstant constant : constants()) { if (constant.name().equals(name)) { 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 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(); }
EnumConstant constant = ((EnumType) type).constant(member); if (constant != null) { markOptions(constant.options()); continue;
@Override void linkOptions(Linker linker) { options.link(linker); for (EnumConstant constant : constants) { constant.linkOptions(linker); } allowAlias = options.get(ALLOW_ALIAS); }
static EnumType fromElement(ProtoType protoType, EnumElement enumElement) { ImmutableList<EnumConstant> constants = EnumConstant.fromElements(enumElement.constants()); Options options = new Options(Options.ENUM_OPTIONS, enumElement.options()); return new EnumType(protoType, enumElement.location(), enumElement.documentation(), enumElement.name(), constants, options); }
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 Object decode(ProtoReader reader) throws IOException { Integer value = ProtoAdapter.UINT32.decode(reader); EnumConstant constant = enumType.constant(value); return constant != null ? constant.name() : 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); } }
@Override Type retainAll(Schema schema, MarkSet markSet) { // If this type is not retained, prune it. if (!markSet.contains(protoType)) return null; ImmutableList.Builder<EnumConstant> retainedConstants = ImmutableList.builder(); for (EnumConstant constant : constants) { if (markSet.contains(ProtoMember.get(protoType, constant.name()))) { retainedConstants.add(constant.retainAll(schema, markSet)); } } EnumType result = new EnumType(protoType, location, documentation, name, retainedConstants.build(), options.retainAll(schema, markSet)); result.allowAlias = allowAlias; return result; }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }