3 import org
.lwes
.serializer
.StringParser
;
4 import org
.lwes
.util
.EncodedString
;
5 import org
.lwes
.util
.IPAddress
;
6 import org
.lwes
.util
.Log
;
8 import java
.math
.BigInteger
;
9 import java
.net
.InetAddress
;
12 * This class provides a base type for the base types in the event system. acts
13 * partially as an interface and partially to provide encapsulation of the
14 * TypeIDs used in serialization.
16 * It also provides a sizeof() type method called getByteSize() used to
17 * determine how many bytes must be used to serialize an object of the given
20 * @author Anthony Molinaro
22 public class BaseType
{
24 * The name of this type used in the ESF file
26 String typeName
= null;
29 * The type token used during serialization
31 byte typeToken
= TypeID
.UNDEFINED_TOKEN
;
34 * The object stored in this type
36 Object typeObject
= null;
39 * Is this guy required
41 private boolean required
;
44 * What is the size restriction on this guy
46 private int sizeRestriction
;
51 public BaseType(String typeName
, byte typeToken
) {
52 this(typeName
, typeToken
, null, false, -1);
55 public BaseType(String typeName
, byte typeToken
, Object typeObject
) {
56 this(typeName
, typeToken
, typeObject
, false, -1);
59 public BaseType(String typeName
,
63 int sizeRestriction
) {
64 this.required
= required
;
65 this.sizeRestriction
= sizeRestriction
;
66 this.typeName
= typeName
;
67 this.typeObject
= typeObject
;
68 this.typeToken
= typeToken
;
71 public void setTypeName(String typeName
) {
72 this.typeName
= typeName
;
75 public String
getTypeName() {
79 public void setTypeToken(byte typeToken
) {
80 this.typeToken
= typeToken
;
83 public byte getTypeToken() {
87 public void setTypeObject(Object typeObject
) {
88 this.typeObject
= typeObject
;
91 public Object
getTypeObject() {
95 public boolean isRequired() {
99 public void setRequired(boolean required
) {
100 this.required
= required
;
103 public Integer
getSizeRestriction() {
104 return sizeRestriction
;
107 public void setSizeRestriction(Integer sizeRestriction
) {
108 this.sizeRestriction
= sizeRestriction
;
111 public int getByteSize(short encoding
) throws NoSuchAttributeTypeException
{
114 case TypeID
.UINT16_TOKEN
:
117 case TypeID
.INT16_TOKEN
:
120 case TypeID
.UINT32_TOKEN
:
123 case TypeID
.INT32_TOKEN
:
126 case TypeID
.INT64_TOKEN
:
129 case TypeID
.UINT64_TOKEN
:
132 case TypeID
.STRING_TOKEN
:
133 String aString
= (String
) typeObject
;
134 /* add size of string plus two bytes for the length */
135 size
= EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]).length
+ 2;
137 case TypeID
.IPADDR_TOKEN
:
140 case TypeID
.BOOLEAN_TOKEN
:
143 case TypeID
.STRING_ARRAY_TOKEN
:
144 int count
= 2; // start with the length of the array
145 String
[] anArray
= (String
[]) typeObject
;
146 for (String s
: anArray
) {
147 count
+= EncodedString
.getBytes(s
, Event
.ENCODING_STRINGS
[encoding
]).length
+ 2;
151 case TypeID
.INT16_ARRAY_TOKEN
:
152 size
= ((short[]) typeObject
).length
*2+2;
154 case TypeID
.INT32_ARRAY_TOKEN
:
155 size
= ((int[]) typeObject
).length
*4+2;
157 case TypeID
.INT64_ARRAY_TOKEN
:
158 size
= ((long[]) typeObject
).length
*8+2;
160 case TypeID
.UINT16_ARRAY_TOKEN
:
161 size
= ((int[]) typeObject
).length
*4+2;
163 case TypeID
.UINT32_ARRAY_TOKEN
:
164 size
= ((long[]) typeObject
).length
*8+2;
166 case TypeID
.UINT64_ARRAY_TOKEN
:
167 size
= ((long[]) typeObject
).length
*8+2;
169 case TypeID
.BOOLEAN_ARRAY_TOKEN
:
170 size
= ((boolean[]) typeObject
).length
+2;
172 case TypeID
.BYTE_ARRAY_TOKEN
:
173 size
= ((byte[]) typeObject
).length
+2;
176 throw new NoSuchAttributeTypeException("Unknown size of BaseType " + typeName
);
181 public int bytesStoreSize(short encoding
) {
182 /* add size of data plus size of token denoting data type */
184 return getByteSize(encoding
) + 1;
186 catch (NoSuchAttributeTypeException e
) {
191 public Object
parseFromString(String string
) throws EventSystemException
{
192 Object toReturn
= null;
194 case TypeID
.UINT16_TOKEN
:
195 toReturn
= StringParser
.fromStringUINT16(string
);
197 case TypeID
.INT16_TOKEN
:
198 toReturn
= StringParser
.fromStringINT16(string
);
200 case TypeID
.UINT32_TOKEN
:
201 toReturn
= StringParser
.fromStringUINT32(string
);
203 case TypeID
.INT32_TOKEN
:
204 toReturn
= StringParser
.fromStringINT32(string
);
206 case TypeID
.INT64_TOKEN
:
207 toReturn
= StringParser
.fromStringINT64(string
);
209 case TypeID
.UINT64_TOKEN
:
210 toReturn
= StringParser
.fromStringUINT64(string
);
212 case TypeID
.STRING_TOKEN
:
213 toReturn
= StringParser
.fromStringSTRING(string
);
215 case TypeID
.IPADDR_TOKEN
:
216 toReturn
= StringParser
.fromStringIPADDR(string
);
218 case TypeID
.BOOLEAN_TOKEN
:
219 toReturn
= StringParser
.fromStringBOOLEAN(string
);
222 throw new NoSuchAttributeTypeException("Unknown size of BaseType "
228 public static BaseType
baseTypeFromObject(Object value
) {
229 if (value
instanceof String
) {
230 return new BaseType(TypeID
.STRING_STRING
, TypeID
.STRING_TOKEN
, value
);
232 if (value
instanceof Short
) {
233 return new BaseType(TypeID
.INT16_STRING
, TypeID
.INT16_TOKEN
, value
);
235 if (value
instanceof Integer
) {
236 return new BaseType(TypeID
.INT32_STRING
, TypeID
.INT32_TOKEN
, value
);
238 if (value
instanceof Long
) {
239 return new BaseType(TypeID
.INT64_STRING
, TypeID
.INT64_TOKEN
, value
);
241 if (value
instanceof InetAddress
|| value
instanceof IPAddress
) {
242 return new BaseType(TypeID
.IPADDR_STRING
, TypeID
.IPADDR_TOKEN
, value
);
244 if (value
instanceof BigInteger
) {
245 return new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, value
);
248 Log
.warning("unaccounted for object class: "+value
.getClass().getName());
253 public BaseType
cloneBaseType() {
254 BaseType newBaseType
= new BaseType(getTypeName(), getTypeToken(),
259 public String
toString() {
260 return typeObject
.toString();