/** 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; }
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; } }
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); } } }
@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; }
private CodeBlock defaultValue(Field field) { Object defaultValue = field.getDefault(); if (defaultValue == null && isEnum(field.type())) { defaultValue = enumDefault(field.type()).name(); } if (field.type().isScalar() || defaultValue != null) { return fieldInitializer(field.type(), defaultValue); } throw new IllegalStateException("Field " + field + " cannot have default value"); }
private void markRoots(Type type) { ProtoType protoType = type.type(); if (identifierSet.includes(protoType)) { marks.root(protoType); queue.add(protoType); } else { if (type instanceof MessageType) { for (Field field : ((MessageType) type).fieldsAndOneOfFields()) { markRoots(ProtoMember.get(protoType, field.name())); } } else if (type instanceof EnumType) { for (EnumConstant enumConstant : ((EnumType) type).constants()) { markRoots(ProtoMember.get(protoType, enumConstant.name())); } } else { throw new AssertionError(); } } for (Type nested : type.nestedTypes()) { markRoots(nested); } }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }
@Override public NameAllocator load(Type type) throws Exception { NameAllocator nameAllocator = new NameAllocator(); if (type instanceof MessageType) { nameAllocator.newName("serialVersionUID", "serialVersionUID"); nameAllocator.newName("ADAPTER", "ADAPTER"); nameAllocator.newName("MESSAGE_OPTIONS", "MESSAGE_OPTIONS"); if (emitAndroid) { nameAllocator.newName("CREATOR", "CREATOR"); } ImmutableList<Field> fieldsAndOneOfFields = ((MessageType) type).fieldsAndOneOfFields(); Set<String> collidingNames = collidingFieldNames(fieldsAndOneOfFields); for (Field field : fieldsAndOneOfFields) { String suggestion = collidingNames.contains(field.name()) ? field.qualifiedName() : field.name(); nameAllocator.newName(suggestion, field); } } else if (type instanceof EnumType) { nameAllocator.newName("value", "value"); nameAllocator.newName("i", "i"); nameAllocator.newName("reader", "reader"); nameAllocator.newName("writer", "writer"); for (EnumConstant constant : ((EnumType) type).constants()) { nameAllocator.newName(constant.name(), constant); } } return nameAllocator; } });
/** 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 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; }
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 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 Object decode(ProtoReader reader) throws IOException { Integer value = ProtoAdapter.UINT32.decode(reader); EnumConstant constant = enumType.constant(value); return constant != null ? constant.name() : 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; }
@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; }
private CodeBlock defaultValue(Field field) { Object defaultValue = field.getDefault(); if (defaultValue == null && isEnum(field.type())) { defaultValue = enumDefault(field.type()).name(); } if (field.type().isScalar() || defaultValue != null) { return fieldInitializer(field.type(), defaultValue); } throw new IllegalStateException("Field " + field + " cannot have default value"); }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }