private CharSequence generateDecoderFlyweightCode(
        final FieldPrecedenceModel fieldPrecedenceModel,
        final String className,
        final Token token)
    {
        final String headerClassName = formatClassName(ir.headerStructure().tokens().get(0).applicableTypeName());

        final StringBuilder methods = new StringBuilder();

        methods.append(generateDecoderWrapListener(fieldPrecedenceModel, "    "));

        methods.append("    public ").append(className).append(" wrap(\n")
            .append("        final ").append(readOnlyBuffer).append(" buffer,\n")
            .append("        final int offset,\n")
            .append("        final int actingBlockLength,\n")
            .append("        final int actingVersion)\n")
            .append("    {\n")
            .append("        if (buffer != this.buffer)\n")
            .append("        {\n")
            .append("            this.buffer = buffer;\n")
            .append("        }\n")
            .append("        this.offset = offset;\n")
            .append("        this.actingBlockLength = actingBlockLength;\n")
            .append("        this.actingVersion = actingVersion;\n")
            .append("        limit(offset + actingBlockLength);\n\n")
            .append(generateAccessOrderListenerCall(fieldPrecedenceModel, "        ", "onWrap", "actingVersion"))
            .append("        return this;\n")
            .append("    }\n\n");

        methods.append("    public ").append(className).append(" wrapAndApplyHeader(\n")
            .append("        final ").append(readOnlyBuffer).append(" buffer,\n")
            .append("        final int offset,\n")
            .append("        final ").append(headerClassName).append("Decoder headerDecoder)\n")
            .append("    {\n")
            .append("        headerDecoder.wrap(buffer, offset);\n\n")
            .append("        final int templateId = headerDecoder.templateId();\n")
            .append("        if (TEMPLATE_ID != templateId)\n")
            .append("        {\n")
            .append("            throw new IllegalStateException(\"Invalid TEMPLATE_ID: \" + templateId);\n")
            .append("        }\n\n")
            .append("        return wrap(\n")
            .append("            buffer,\n")
            .append("            offset + ").append(headerClassName).append("Decoder.ENCODED_LENGTH,\n")
            .append("            headerDecoder.blockLength(),\n")
            .append("            headerDecoder.version());\n")
            .append("    }\n\n");

        methods.append("    public ").append(className).append(" sbeRewind()\n")
            .append("    {\n")
            .append("        return wrap(buffer, offset, actingBlockLength, actingVersion);\n")
            .append("    }\n\n");

        methods.append("    public int sbeDecodedLength()\n")
            .append("    {\n")
            .append("        final int currentLimit = limit();\n");

        if (null != fieldPrecedenceModel)
        {
            methods.append("        final int currentCodecState = codecState();\n");
        }

        methods
            .append("        sbeSkip();\n")
            .append("        final int decodedLength = encodedLength();\n")
            .append("        limit(currentLimit);\n\n");

        if (null != fieldPrecedenceModel)
        {
            methods.append("        if (").append(precedenceChecksFlagName).append(")\n")
                .append("        {\n")
                .append("            codecState(currentCodecState);\n")
                .append("        }\n\n");
        }

        methods.append("        return decodedLength;\n")
            .append("    }\n\n");

        methods.append("    public int actingVersion()\n")
            .append("    {\n")
            .append("        return actingVersion;\n")
            .append("    }\n\n");

        return generateFlyweightCode(DECODER, className, token, methods.toString(), readOnlyBuffer);
    }
\n" +
                "     * Field presence indication. Can be optional, required, or constant.\n" +
                "
sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java