/*
 * Copyright (c) 2013 Cinnober Financial Technology AB, Stockholm,
 * Sweden. All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Cinnober Financial Technology AB, Stockholm, Sweden. You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Cinnober.
 *
 * Cinnober makes no representations or warranties about the suitability
 * of the software, either expressed or implied, including, but not limited
 * to, the implied warranties of merchantibility, fitness for a particular
 * purpose, or non-infringement. Cinnober shall not be liable for any
 * damages suffered by licensee as a result of using, modifying, or
 * distributing this software or its derivatives.
 */
package com.cinnober.msgcodec.blink;

import java.io.IOException;

import com.cinnober.msgcodec.Factory;
import com.cinnober.msgcodec.GroupDef;

/** The static group instruction can encode and decode the fields of a group.
 * Any dynamic behaviour lies outside this class, e.g. lookup of <em>which</em>
 * static group to encode/decode.
 * 
 * @author mikael.brannstrom
 */
@SuppressWarnings("rawtypes")
class StaticGroupInstruction {
    protected final GroupDef groupDef;
    protected final FieldInstruction[] fieldInstructions;
    protected final Factory factory;
    protected final StaticGroupInstruction superGroup;
    public StaticGroupInstruction(GroupDef groupDef, StaticGroupInstruction superGroup) {
        this.groupDef = groupDef;
        this.fieldInstructions = new FieldInstruction[groupDef.getFields().size()];
        this.factory = groupDef.getFactory();
        this.superGroup = superGroup;
    }
    void initFieldInstruction(int index, FieldInstruction fieldInstruction) {
        fieldInstructions[index] = fieldInstruction;
    }
    /** Write the group id to the specified stream. Only done for dynamic groups.
     * 
     * @param out
     * @throws IOException
     */
    public void encodeGroupId(BlinkOutputStream out) throws IOException {
        out.writeUInt32(groupDef.getId());
    }
    /** Write the fields of the group, including any super groups.
     * 
     * @param group the group to be encoded
     * @param out
     * @throws IOException
     */
    public void encodeGroup(Object group, BlinkOutputStream out) throws IOException {
        if (superGroup != null) {
            superGroup.encodeGroup(group, out);
        }
        for (int i = 0; i < fieldInstructions.length; i++) {
            fieldInstructions[i].encodeField(group, out);
        }
    }
    /** Read the fields of a group, including any super groups.
     * 
     * @param in
     * @return the group, not null
     * @throws IOException
     */
    public Object decodeGroup(BlinkInputStream in) throws IOException {
        Object group = factory.newInstance();
        decodeGroup(group, in);
        return group;
    }
    /** Read the fields of a group, including any super groups.
     * 
     * @param group the group where field values should be stored.
     * @param in
     * @throws IOException
     */
    private void decodeGroup(Object group, BlinkInputStream in) throws IOException {
        if (superGroup != null) {
            superGroup.decodeGroup(group, in);
        }
        for (int i = 0; i < fieldInstructions.length; i++) {
            fieldInstructions[i].decodeField(group, in);
        }
    }
}
@@ -1150,18 +1150,17 @@
 code.%0A *
- 
 %0A
+
  * @auth
@@ -1949,33 +1949,32 @@
 c groups.%0A     *
- 
 %0A     * @param o
@@ -2097,40 +2097,205 @@
     
-out.writeUInt32(groupDef.getId()
+int id = groupDef.getId();%0A        if (id == -1) %7B%0A            throw new IOException(%22Missing group id, cannot encode as dynamic group: %22 + groupDef.getName());%0A        %7D%0A        out.writeUInt32(id
 );%0A 
@@ -2364,33 +2364,32 @@
 r groups.%0A     *
- 
 %0A     * @param g
@@ -2845,33 +2845,32 @@
 r groups.%0A     *
- 
 %0A     * @param i
@@ -3183,23 +3183,22 @@
 groups.%0A
+
      *
- 
 %0A     * 

Validate groupDef id in blink before encoding it as a dynamic group.