writeGeneralCodec method

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

Writes the custom codec to indent.

Implementation

@override
void writeGeneralCodec(
  JavaOptions generatorOptions,
  Root root,
  Indent indent, {
  required String dartPackageName,
}) {
  final List<EnumeratedType> enumeratedTypes =
      getEnumeratedTypes(root).toList();

  void writeEncodeLogic(EnumeratedType customType) {
    final String encodeString =
        customType.type == CustomTypes.customClass ? 'toList()' : 'index';
    final String nullCheck = customType.type == CustomTypes.customEnum
        ? 'value == null ? null : '
        : '';
    final String valueString = customType.enumeration < maximumCodecFieldKey
        ? '$nullCheck((${customType.name}) value).$encodeString'
        : 'wrap.toList()';
    final int enumeration = customType.enumeration < maximumCodecFieldKey
        ? customType.enumeration
        : maximumCodecFieldKey;

    indent.add('if (value instanceof ${customType.name}) ');
    indent.addScoped('{', '} else ', () {
      if (customType.enumeration >= maximumCodecFieldKey) {
        indent
            .writeln('$_overflowClassName wrap = new $_overflowClassName();');
        indent.writeln(
            'wrap.setType(${customType.enumeration - maximumCodecFieldKey});');
        indent.writeln(
            'wrap.setWrapped($nullCheck((${customType.name}) value).$encodeString);');
      }
      indent.writeln('stream.write($enumeration);');
      indent.writeln('writeValue(stream, $valueString);');
    }, addTrailingNewline: false);
  }

  void writeDecodeLogic(EnumeratedType customType) {
    indent.write('case (byte) ${customType.enumeration}:');
    if (customType.type == CustomTypes.customClass) {
      indent.newln();
      indent.nest(1, () {
        indent.writeln(
            'return ${customType.name}.fromList((ArrayList<Object>) readValue(buffer));');
      });
    } else if (customType.type == CustomTypes.customEnum) {
      indent.addScoped(' {', '}', () {
        indent.writeln('Object value = readValue(buffer);');
        indent
            .writeln('return ${_intToEnum('value', customType.name, true)};');
      });
    }
  }

  final EnumeratedType overflowClass = EnumeratedType(
      _overflowClassName, maximumCodecFieldKey, CustomTypes.customClass);

  if (root.requiresOverflowClass) {
    _writeCodecOverflowUtilities(
      generatorOptions,
      root,
      indent,
      enumeratedTypes,
      dartPackageName: dartPackageName,
    );
  }
  indent.newln();
  indent.write(
      'private static class $_codecName extends StandardMessageCodec ');
  indent.addScoped('{', '}', () {
    indent.writeln(
        'public static final $_codecName INSTANCE = new $_codecName();');
    indent.newln();
    indent.writeln('private $_codecName() {}');
    indent.newln();
    indent.writeln('@Override');
    indent.writeScoped(
        'protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {',
        '}', () {
      indent.writeScoped('switch (type) {', '}', () {
        for (final EnumeratedType customType in enumeratedTypes) {
          if (customType.enumeration < maximumCodecFieldKey) {
            writeDecodeLogic(customType);
          }
        }
        if (root.requiresOverflowClass) {
          writeDecodeLogic(overflowClass);
        }
        indent.writeln('default:');
        indent.nest(1, () {
          indent.writeln('return super.readValueOfType(type, buffer);');
        });
      });
    });
    indent.newln();
    indent.writeln('@Override');
    indent.write(
        'protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) ');
    indent.addScoped('{', '}', () {
      indent.write('');
      enumeratedTypes.forEach(writeEncodeLogic);
      indent.addScoped('{', '}', () {
        indent.writeln('super.writeValue(stream, value);');
      });
    });
  });
  indent.newln();
}