//========================================================================
//Copyright 2007-2009 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.dyuproject.protostuff;

import java.io.IOException;

/**
 * Ser/deser test object that wraps an object {@link HasBar} without any schema.
 *
 * @author David Yu
 * @created Nov 13, 2009
 */
public final class HasHasBar implements Message<HasHasBar>, Schema<HasHasBar>
{
    
    private String name;
    private HasBar hasBar;
    
    public HasHasBar()
    {
        
    }

    public HasHasBar(String name, HasBar hasBar)
    {
        this.name = name;
        this.hasBar = hasBar;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the hasBar
     */
    public HasBar getHasBar()
    {
        return hasBar;
    }

    /**
     * @param hasBar the hasBar to set
     */
    public void setHasBar(HasBar hasBar)
    {
        this.hasBar = hasBar;
    }

    public Schema<HasHasBar> cachedSchema()
    {
        return this;
    }

    public String getFieldName(int number)
    {
        return String.valueOf(number);
    }

    public int getFieldNumber(String name)
    {
        return Integer.parseInt(name);
    }

    public boolean isInitialized(HasHasBar message)
    {
        return message.hasBar != null;
    }

    public HasHasBar newMessage()
    {
        return new HasHasBar();
    }
    

    public void mergeFrom(Input input, HasHasBar message) throws IOException
    {
        while(true)
        {
            int number = input.readFieldNumber(this);
            switch(number)
            {
                case 0:
                    return;
                case 1:
                    message.name = input.readString();
                    break;
                case 2:
                    message.hasBar = input.readObject(HasBar.class);
                    break;
                default:
                    input.handleUnknownField(number, this);
            }
        }
    }

    public void writeTo(Output output, HasHasBar message) throws IOException
    {
        output.writeString(1, message.name);
        output.writeObject(2, message.hasBar, HasBar.class);
    }
    
    

}

@@ -888,16 +888,117 @@
 eption;%0D
+%0Aimport java.io.ObjectInputStream;%0D%0Aimport java.io.ObjectOutputStream;%0D%0Aimport java.io.Serializable;%0D
 %0A%0D%0A/**%0D%0A
@@ -1212,16 +1212,30 @@
 sHasBar%3E
+, Serializable
 %0D%0A%7B%0D%0A   
@@ -3290,19 +3290,582 @@
 %0D%0A    %0D%0A    
+private void readObject(ObjectInputStream in) throws IOException%0D%0A    %7B%0D%0A        int length = in.readInt();%0D%0A        byte%5B%5D data = new byte%5Blength%5D;%0D%0A        for(int offset = 0; length %3E 0; length -= offset)%0D%0A            offset = in.read(data, offset, length);%0D%0A        %0D%0A        in.close();%0D%0A        IOUtil.mergeFrom(data, this);%0D%0A    %7D%0D%0A    %0D%0A    private void writeObject(ObjectOutputStream out) throws IOException%0D%0A    %7B%0D%0A        byte%5B%5D data = IOUtil.toByteArray(this);%0D%0A        out.writeInt(data.length);%0D%0A        out.write(data);%0D%0A        out.close();%0D%0A    %7D
 %0D%0A%0D%0A%7D%0D%0A

implement serializable