package com.dd.model.protocol;
public class Value implements IProtocolBase
{
String valueString;
T value;
DataTypeEnum valueType;
public Value()
{
}
public Value(T value)
{
this.value = value;
valueString = value.toString();
}
private DataTypeEnum getValueType(T value)
{
DataTypeEnum result = null;
String valueType = value.getClass().toString();
int hashcode = valueType.hashCode();
switch (hashcode)
{
case 1335156652:// "class java.lang.Boolean".hashCode()
result = DataTypeEnum.BOOL;
break;
case -1603904083:// ("class java.lang.Character").hashCode()
result = DataTypeEnum.INT8;
break;
case 575539456:// "class java.lang.Short").hashCode()
result = DataTypeEnum.INT16;
break;
case -1066470206:// "class java.lang.Integer").hashCode()
result = DataTypeEnum.INT32;
break;
case 563652320:// ("class java.lang.Float").hashCode()
result = DataTypeEnum.FLOAT;
break;
default:
result = DataTypeEnum.STRING;
}
return result;
}
@Override
public int getTotalLen()
{
int result = 0;
DataTypeEnum type = null;
type = getValueType(value);
if (type == DataTypeEnum.BOOL)
result = 1;
else if (type == DataTypeEnum.INT8)
result = 1;
else if (type == DataTypeEnum.INT16)
result = 2;
else if (type == DataTypeEnum.INT32)
result = 4;
else if (type == DataTypeEnum.FLOAT)
result = 4;
else if (type == DataTypeEnum.STRING)
result = valueString.length();
return result;
}
@Override
public int getSerialTotalLen()
{
int result = 0;
DataTypeEnum type = null;
type = getValueType(value);
if (type == DataTypeEnum.BOOL)
result = 1*2;
else if (type == DataTypeEnum.INT8)
result = 1*2;
else if (type == DataTypeEnum.INT16)
result = 2*2;
else if (type == DataTypeEnum.INT32)
result = 4*2;
else if (type == DataTypeEnum.FLOAT)
result = 4*2;
else if (type == DataTypeEnum.STRING)
result = valueString.length();
return result;
}
@Override
public byte[] serial()
{
byte result[] = null;
DataTypeEnum type = null;
type = getValueType(value);
if (type == DataTypeEnum.BOOL)
{
if ((boolean) value == true)
{
result = new byte[1];
result[0] = 1;
} else
{
result = new byte[1];
result[0] = 0;
}
} else if (type == DataTypeEnum.INT8)
{
result = new byte[1];
result[0] = (byte) value;
} else if (type == DataTypeEnum.INT16)
{
result = new byte[2];
result[0] = (byte) ((short) value >> 8);
result[1] = (byte) ((short) value);
} else if (type == DataTypeEnum.INT32)
{
result = new byte[4];
result[0] = (byte) ((int) value >> 24);
result[1] = (byte) ((int) value >> 16);
result[2] = (byte) ((int) value >> 8);
result[3] = (byte) ((int) value);
} else if (type == DataTypeEnum.FLOAT)
{
int tempValue = Float.floatToIntBits((float) value);
result = new byte[4];
result[0] = (byte) ((int) tempValue >> 24);
result[1] = (byte) ((int) tempValue >> 16);
result[2] = (byte) ((int) tempValue >> 8);
result[3] = (byte) ((int) tempValue);
} else if (type == DataTypeEnum.STRING)
{
result = valueString.getBytes();
}
return result;
// if (valueString == null)
// return null;
// else
// return valueString.getBytes();
}
public String toString()
{
return valueString;
}
public T getValue()
{
return value;
}
public void setValue(T value)
{
this.value = value;
}
}
阅读(2843) | 评论(0) | 转发(0) |