/* Copyright (C) 2009  CSE,IIT Bombay  http://www.cse.iitb.ac.in

This file is part of the ConStore open source storage facility for concept-nets.

ConStore is free software and distributed under the 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License;
you can copy, distribute and transmit the work
with the work attribution in the manner specified by the author or licensor.
You may not use this work for commercial purposes and may not alter, 
transform, or build upon this work.

Please refer the legal code of the license, available at
http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode

ConStore is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  */

package iitb.con.util;

import java.nio.ByteBuffer;

/**
 * Serializer util to converts the wrapper class primitive object types to bytes and vice-versa.
 *  
 * @author Prathab K
 *
 */
public class Serializer {
    
    /**
     * Converts the given primitive object type to bytes
     * @param buf ByteBuffer where the converted bytes are placed
     * @param value primitive object type
     */
    public static void toBytes(ByteBuffer buf, Object value) {
        if(value != null) {
            if(value instanceof String) {
                buf.putInt(((String)value).length());
                buf.put(((String)value).getBytes());
            }else if(value instanceof Integer) {
                buf.putInt((Integer)value);
            }else if(value instanceof Float) {
                buf.putFloat((Float)value);
            }else if(value instanceof Double) {
                buf.putDouble((Double)value);
            }else if(value instanceof Boolean) {
                if(((Boolean)value).booleanValue()) 
                    buf.put((byte)1);
                else
                    buf.put((byte)0);
            }
        }
    }
    
    /**
     * Converts the given bytes to primitive object type 
     * @param buf ByteBuffer where the bytes are placed
     * @param value primitive object type
     * @return primitive object type
     */
    public static Object toObject(ByteBuffer buf, Object value) {
        if(value != null) {
            if(value instanceof String) {
                short size = buf.getShort();
                byte[] tempBuf = new byte[size];
                buf.get(tempBuf);
                return new String(tempBuf);
            }else if(value instanceof Integer) {
                return new Integer(buf.getInt());
            }else if(value instanceof Float) {
                return new Float(buf.getFloat());
            }else if(value instanceof Double) {
                return new Double(buf.getDouble());
            }else if(value instanceof Boolean) {
                if (buf.get() == 0)
                    return new Boolean(false);
                else
                    return new Boolean(true);
            }
        }
        return null;
    }

}