3 import org
.lwes
.serializer
.StringParser
;
4 import org
.lwes
.util
.EncodedString
;
7 * This class provides a base type for the base types in the event system. acts
8 * partially as an interface and partially to provide encapsulation of the
9 * TypeIDs used in serialization.
11 * It also provides a sizeof() type method called getByteSize() used to
12 * determine how many bytes must be used to serialize an object of the given
15 * @author Anthony Molinaro
17 public class BaseType
{
19 * The name of this type used in the ESF file
21 String typeName
= null;
24 * The type token used during serialization
26 byte typeToken
= TypeID
.UNDEFINED_TOKEN
;
29 * The object stored in this type
31 Object typeObject
= null;
36 public BaseType(String typeName
, byte typeToken
) {
37 this.typeName
= typeName
;
38 this.typeToken
= typeToken
;
41 public BaseType(String typeName
, byte typeToken
, Object typeObject
) {
42 this.typeName
= typeName
;
43 this.typeToken
= typeToken
;
44 this.typeObject
= typeObject
;
47 public void setTypeName(String typeName
) {
48 this.typeName
= typeName
;
51 public String
getTypeName() {
55 public void setTypeToken(byte typeToken
) {
56 this.typeToken
= typeToken
;
59 public byte getTypeToken() {
63 public void setTypeObject(Object typeObject
) {
64 this.typeObject
= typeObject
;
67 public Object
getTypeObject() {
71 public int getByteSize(short encoding
) throws NoSuchAttributeTypeException
{
74 case TypeID
.UINT16_TOKEN
:
77 case TypeID
.INT16_TOKEN
:
80 case TypeID
.UINT32_TOKEN
:
83 case TypeID
.INT32_TOKEN
:
86 case TypeID
.INT64_TOKEN
:
89 case TypeID
.UINT64_TOKEN
:
92 case TypeID
.STRING_TOKEN
:
93 String aString
= (String
) typeObject
;
94 /* add size of string plus two bytes for the length */
95 size
= EncodedString
.getBytes(aString
,
96 Event
.ENCODING_STRINGS
[encoding
]).length
+ 2;
98 case TypeID
.IPADDR_TOKEN
:
101 case TypeID
.BOOLEAN_TOKEN
:
105 throw new NoSuchAttributeTypeException("Unknown size of BaseType "
111 public int bytesStoreSize(short encoding
) {
112 /* add size of data plus size of token denoting data type */
114 return getByteSize(encoding
) + 1;
115 } catch (NoSuchAttributeTypeException e
) {
120 public Object
parseFromString(String string
) throws EventSystemException
{
121 Object toReturn
= null;
123 case TypeID
.UINT16_TOKEN
:
124 toReturn
= StringParser
.fromStringUINT16(string
);
126 case TypeID
.INT16_TOKEN
:
127 toReturn
= StringParser
.fromStringINT16(string
);
129 case TypeID
.UINT32_TOKEN
:
130 toReturn
= StringParser
.fromStringUINT32(string
);
132 case TypeID
.INT32_TOKEN
:
133 toReturn
= StringParser
.fromStringINT32(string
);
135 case TypeID
.INT64_TOKEN
:
136 toReturn
= StringParser
.fromStringINT64(string
);
138 case TypeID
.UINT64_TOKEN
:
139 toReturn
= StringParser
.fromStringUINT64(string
);
141 case TypeID
.STRING_TOKEN
:
142 toReturn
= StringParser
.fromStringSTRING(string
);
144 case TypeID
.IPADDR_TOKEN
:
145 toReturn
= StringParser
.fromStringIPADDR(string
);
147 case TypeID
.BOOLEAN_TOKEN
:
148 toReturn
= StringParser
.fromStringBOOLEAN(string
);
151 throw new NoSuchAttributeTypeException("Unknown size of BaseType "
157 public BaseType
cloneBaseType() {
158 BaseType newBaseType
= new BaseType(getTypeName(), getTypeToken(),
163 public String
toString() {
164 return typeObject
.toString();