1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.lwes
.serializer
.StringParser
;
18 import org
.lwes
.util
.EncodedString
;
19 import org
.lwes
.util
.IPAddress
;
21 import java
.math
.BigInteger
;
22 import java
.net
.InetAddress
;
23 import java
.util
.List
;
26 * This class provides a base type for the base types in the event system. acts
27 * partially as an interface and partially to provide encapsulation of the
28 * TypeIDs used in serialization.
30 * It also provides a sizeof() type method called getByteSize() used to
31 * determine how many bytes must be used to serialize an object of the given
34 * @author Anthony Molinaro
36 public class BaseType
{
38 private static transient Log log
= LogFactory
.getLog(BaseType
.class);
41 * The name of this type used in the ESF file
43 String typeName
= null;
46 * The type token used during serialization
48 byte typeToken
= TypeID
.UNDEFINED_TOKEN
;
51 * The object stored in this type
53 Object typeObject
= null;
56 * Is this guy required
58 private boolean required
;
61 * What is the size restriction on this guy
63 private int sizeRestriction
;
66 * What the default value for this guy should be.
68 private Object defaultValue
;
73 public BaseType(String typeName
, byte typeToken
) {
74 this(typeName
, typeToken
, null, false, -1);
77 public BaseType(String typeName
, byte typeToken
, Object typeObject
) {
78 this(typeName
, typeToken
, typeObject
, false, -1);
81 public BaseType(String typeName
,
85 int sizeRestriction
) {
86 this(typeName
, typeToken
, typeObject
, required
, sizeRestriction
, null);
89 public BaseType(String typeName
,
94 Object defaultValue
) {
95 this.required
= required
;
96 this.sizeRestriction
= sizeRestriction
;
97 this.typeName
= typeName
;
98 this.typeObject
= typeObject
;
99 this.typeToken
= typeToken
;
100 this.defaultValue
= defaultValue
;
103 public Object
getDefaultValue() {
107 public void setDefaultValue(Object defaultValue
) {
108 this.defaultValue
= defaultValue
;
111 public void setTypeName(String typeName
) {
112 this.typeName
= typeName
;
115 public String
getTypeName() {
119 public void setTypeToken(byte typeToken
) {
120 this.typeToken
= typeToken
;
123 public byte getTypeToken() {
127 public void setTypeObject(Object typeObject
) {
128 this.typeObject
= typeObject
;
131 public Object
getTypeObject() {
135 public boolean isRequired() {
139 public void setRequired(boolean required
) {
140 this.required
= required
;
143 public Integer
getSizeRestriction() {
144 return sizeRestriction
;
147 public void setSizeRestriction(Integer sizeRestriction
) {
148 this.sizeRestriction
= sizeRestriction
;
151 public int getByteSize(short encoding
) throws NoSuchAttributeTypeException
{
154 case TypeID
.UINT16_TOKEN
:
157 case TypeID
.INT16_TOKEN
:
160 case TypeID
.UINT32_TOKEN
:
163 case TypeID
.INT32_TOKEN
:
166 case TypeID
.INT64_TOKEN
:
169 case TypeID
.UINT64_TOKEN
:
172 case TypeID
.FLOAT_TOKEN
:
175 case TypeID
.DOUBLE_TOKEN
:
178 case TypeID
.STRING_TOKEN
:
179 String aString
= (String
) typeObject
;
180 /* add size of string plus two bytes for the length */
181 size
= EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]).length
+ 2;
183 case TypeID
.IPADDR_TOKEN
:
186 case TypeID
.IPV4_TOKEN
:
189 case TypeID
.BOOLEAN_TOKEN
:
192 case TypeID
.STRING_ARRAY_TOKEN
:
193 int count
= 2; // start with the length of the array
194 List
<String
> anArray
= (List
) typeObject
;
195 for (String s
: anArray
) {
196 count
+= EncodedString
.getBytes(s
, Event
.ENCODING_STRINGS
[encoding
]).length
+ 2;
200 case TypeID
.INT16_ARRAY_TOKEN
:
201 size
= ((List
) typeObject
).size() * 2 + 2;
203 case TypeID
.INT32_ARRAY_TOKEN
:
204 size
= ((List
) typeObject
).size() * 4 + 2;
206 case TypeID
.INT64_ARRAY_TOKEN
:
207 size
= ((List
) typeObject
).size() * 8 + 2;
209 case TypeID
.UINT16_ARRAY_TOKEN
:
210 size
= ((List
) typeObject
).size() * 4 + 2;
212 case TypeID
.UINT32_ARRAY_TOKEN
:
213 size
= ((List
) typeObject
).size() * 8 + 2;
215 case TypeID
.UINT64_ARRAY_TOKEN
:
216 size
= ((List
) typeObject
).size() * 8 + 2;
218 case TypeID
.BOOLEAN_ARRAY_TOKEN
:
219 size
= ((List
) typeObject
).size() + 2;
221 case TypeID
.BYTE_ARRAY_TOKEN
:
222 size
= ((List
) typeObject
).size() + 2;
224 case TypeID
.DOUBLE_ARRAY_TOKEN
:
225 size
= ((List
) typeObject
).size() * 8 + 2;
227 case TypeID
.FLOAT_ARRAY_TOKEN
:
228 size
= ((List
) typeObject
).size() * 4 + 2;
230 case TypeID
.IPV4_ARRAY_TOKEN
:
231 size
= ((List
) typeObject
).size() * 4 + 2;
234 throw new NoSuchAttributeTypeException("Unknown size of BaseType " + typeName
);
239 public int bytesStoreSize(short encoding
) throws NoSuchAttributeTypeException
{
240 /* add size of data plus size of token denoting data type */
241 return getByteSize(encoding
) + 1;
244 public Object
parseFromString(String string
) throws EventSystemException
{
245 Object toReturn
= null;
247 case TypeID
.UINT16_TOKEN
:
248 toReturn
= StringParser
.fromStringUINT16(string
);
250 case TypeID
.INT16_TOKEN
:
251 toReturn
= StringParser
.fromStringINT16(string
);
253 case TypeID
.UINT32_TOKEN
:
254 toReturn
= StringParser
.fromStringUINT32(string
);
256 case TypeID
.INT32_TOKEN
:
257 toReturn
= StringParser
.fromStringINT32(string
);
259 case TypeID
.INT64_TOKEN
:
260 toReturn
= StringParser
.fromStringINT64(string
);
262 case TypeID
.UINT64_TOKEN
:
263 toReturn
= StringParser
.fromStringUINT64(string
);
265 case TypeID
.STRING_TOKEN
:
266 toReturn
= StringParser
.fromStringSTRING(string
);
268 case TypeID
.IPADDR_TOKEN
:
269 toReturn
= StringParser
.fromStringIPADDR(string
);
271 case TypeID
.BOOLEAN_TOKEN
:
272 toReturn
= StringParser
.fromStringBOOLEAN(string
);
275 throw new NoSuchAttributeTypeException("Unknown size of BaseType "
281 public static BaseType
baseTypeFromObject(Object value
) {
282 if (value
instanceof String
) {
283 return new BaseType(TypeID
.STRING_STRING
, TypeID
.STRING_TOKEN
, value
);
285 if (value
instanceof Short
) {
286 return new BaseType(TypeID
.INT16_STRING
, TypeID
.INT16_TOKEN
, value
);
288 if (value
instanceof Integer
) {
289 return new BaseType(TypeID
.INT32_STRING
, TypeID
.INT32_TOKEN
, value
);
291 if (value
instanceof Long
) {
292 return new BaseType(TypeID
.INT64_STRING
, TypeID
.INT64_TOKEN
, value
);
294 if (value
instanceof InetAddress
|| value
instanceof IPAddress
) {
295 return new BaseType(TypeID
.IPADDR_STRING
, TypeID
.IPADDR_TOKEN
, value
);
297 if (value
instanceof BigInteger
) {
298 return new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, value
);
301 log
.warn("unaccounted for object class: " + value
.getClass().getName());
306 public BaseType
cloneBaseType() {
307 BaseType newBaseType
= new BaseType(getTypeName(), getTypeToken(),
312 public String
toString() {
313 return typeObject
.toString();