package com.twitter.elephantbird.proto.codegen;

import com.twitter.elephantbird.mapreduce.io.ProtobufWritable;
import com.twitter.elephantbird.proto.util.FormattingStringBuffer;
import com.twitter.elephantbird.util.TypeRef;

public class ProtobufWritableGenerator extends ProtoCodeGenerator {

  @Override
  public String getFilename() {
    return String.format("%s/mapreduce/io/Protobuf%sWritable.java",
        packageName_.replaceAll("\\.", "/"), descriptorProto_.getName());
  }

  @Override
  public String generateCode() {
    FormattingStringBuffer sb = new FormattingStringBuffer();

    sb.append("package %s.mapreduce.io;", packageName_).endl().endl();

    sb.append("import %s.%s.%s;", packageName_, protoFilename_, descriptorProto_.getName()).endl();
    sb.append("import %s;", ProtobufWritable.class.getName()).endl();
    sb.append("import %s;", TypeRef.class.getName()).endl().endl();

    sb.append("public class Protobuf%sWritable extends ProtobufWritable<%s> {", descriptorProto_.getName(), descriptorProto_.getName(), descriptorProto_.getName()).endl();
    sb.append("  public Protobuf%sWritable() {", descriptorProto_.getName()).endl();
    sb.append("    super(new TypeRef<%s>(){});", descriptorProto_.getName()).endl();
    sb.append("  }").endl();
    sb.append("}").endl();
    sb.endl();

    return sb.toString();
  }
}

@@ -973,24 +973,32 @@
 able%3C%25s%3E %7B%22,
+%0A       
  descriptorP
@@ -1269,32 +1269,276 @@
 (%22  %7D%22).endl();%0A
+%0A    sb.append(%22  public Protobuf%25sWritable(%25s m) %7B%22,%0A        descriptorProto_.getName(), descriptorProto_.getName()).endl();%0A    sb.append(%22    super(m, new TypeRef%3C%25s%3E()%7B%7D);%22, descriptorProto_.getName()).endl();%0A    sb.append(%22  %7D%22).endl();%0A%0A
     sb.append(%22%7D

add a ProtobufWritable constructor from a specific message -- useful for testing.