writeEnum method

  1. @override
void writeEnum(
  1. JavaOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Enum anEnum, {
  5. required String dartPackageName,
})
override

Writes a single Enum to indent. This is needed in most generators.

Implementation

@override
void writeEnum(
  JavaOptions generatorOptions,
  Root root,
  Indent indent,
  Enum anEnum, {
  required String dartPackageName,
}) {
  String camelToSnake(String camelCase) {
    final RegExp regex = RegExp('([a-z])([A-Z]+)');
    return camelCase
        .replaceAllMapped(regex, (Match m) => '${m[1]}_${m[2]}')
        .toUpperCase();
  }

  indent.newln();
  addDocumentationComments(
      indent, anEnum.documentationComments, _docCommentSpec);

  indent.write('public enum ${anEnum.name} ');
  indent.addScoped('{', '}', () {
    enumerate(anEnum.members, (int index, final EnumMember member) {
      addDocumentationComments(
          indent, member.documentationComments, _docCommentSpec);
      indent.writeln(
          '${camelToSnake(member.name)}($index)${index == anEnum.members.length - 1 ? ';' : ','}');
    });
    indent.newln();
    // This uses default access (package-private), because private causes
    // SyntheticAccessor warnings in the serialization code.
    indent.writeln('final int index;');
    indent.newln();
    indent.write('private ${anEnum.name}(final int index) ');
    indent.addScoped('{', '}', () {
      indent.writeln('this.index = index;');
    });
  });
}