removed a debug
[lwes-java.git] / src / main / java / org / lwes / BaseType.java
blobf94bc7d3a21686f9c48fd790e58514ba60149d27
1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
3 * *
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 *======================================================================*/
13 package org.lwes;
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;
25 /**
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.
29 * <p/>
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
32 * type.
34 * @author Anthony Molinaro
36 public class BaseType {
38 private static transient Log log = LogFactory.getLog(BaseType.class);
40 /**
41 * The name of this type used in the ESF file
43 String typeName = null;
45 /**
46 * The type token used during serialization
48 byte typeToken = TypeID.UNDEFINED_TOKEN;
50 /**
51 * The object stored in this type
53 Object typeObject = null;
55 /**
56 * Is this guy required
58 private boolean required;
60 /**
61 * What is the size restriction on this guy
63 private int sizeRestriction;
65 /**
66 * What the default value for this guy should be.
68 private Object defaultValue;
70 public BaseType() {
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,
82 byte typeToken,
83 Object typeObject,
84 boolean required,
85 int sizeRestriction) {
86 this(typeName, typeToken, typeObject, required, sizeRestriction, null);
89 public BaseType(String typeName,
90 byte typeToken,
91 Object typeObject,
92 boolean required,
93 int sizeRestriction,
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() {
104 return defaultValue;
107 public void setDefaultValue(Object defaultValue) {
108 this.defaultValue = defaultValue;
111 public void setTypeName(String typeName) {
112 this.typeName = typeName;
115 public String getTypeName() {
116 return typeName;
119 public void setTypeToken(byte typeToken) {
120 this.typeToken = typeToken;
123 public byte getTypeToken() {
124 return typeToken;
127 public void setTypeObject(Object typeObject) {
128 this.typeObject = typeObject;
131 public Object getTypeObject() {
132 return typeObject;
135 public boolean isRequired() {
136 return required;
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 {
152 int size;
153 switch (typeToken) {
154 case TypeID.UINT16_TOKEN:
155 size = 2;
156 break;
157 case TypeID.INT16_TOKEN:
158 size = 2;
159 break;
160 case TypeID.UINT32_TOKEN:
161 size = 4;
162 break;
163 case TypeID.INT32_TOKEN:
164 size = 4;
165 break;
166 case TypeID.INT64_TOKEN:
167 size = 8;
168 break;
169 case TypeID.UINT64_TOKEN:
170 size = 8;
171 break;
172 case TypeID.FLOAT_TOKEN:
173 size = 4;
174 break;
175 case TypeID.DOUBLE_TOKEN:
176 size = 8;
177 break;
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;
182 break;
183 case TypeID.IPADDR_TOKEN:
184 size = 4;
185 break;
186 case TypeID.IPV4_TOKEN:
187 size = 4;
188 break;
189 case TypeID.BOOLEAN_TOKEN:
190 size = 1;
191 break;
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;
198 size = count;
199 break;
200 case TypeID.INT16_ARRAY_TOKEN:
201 size = ((List) typeObject).size() * 2 + 2;
202 break;
203 case TypeID.INT32_ARRAY_TOKEN:
204 size = ((List) typeObject).size() * 4 + 2;
205 break;
206 case TypeID.INT64_ARRAY_TOKEN:
207 size = ((List) typeObject).size() * 8 + 2;
208 break;
209 case TypeID.UINT16_ARRAY_TOKEN:
210 size = ((List) typeObject).size() * 4 + 2;
211 break;
212 case TypeID.UINT32_ARRAY_TOKEN:
213 size = ((List) typeObject).size() * 8 + 2;
214 break;
215 case TypeID.UINT64_ARRAY_TOKEN:
216 size = ((List) typeObject).size() * 8 + 2;
217 break;
218 case TypeID.BOOLEAN_ARRAY_TOKEN:
219 size = ((List) typeObject).size() + 2;
220 break;
221 case TypeID.BYTE_ARRAY_TOKEN:
222 size = ((List) typeObject).size() + 2;
223 break;
224 case TypeID.DOUBLE_ARRAY_TOKEN:
225 size = ((List) typeObject).size() * 8 + 2;
226 break;
227 case TypeID.FLOAT_ARRAY_TOKEN:
228 size = ((List) typeObject).size() * 4 + 2;
229 break;
230 case TypeID.IPV4_ARRAY_TOKEN:
231 size = ((List) typeObject).size() * 4 + 2;
232 break;
233 default:
234 throw new NoSuchAttributeTypeException("Unknown size of BaseType " + typeName);
236 return size;
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;
246 switch (typeToken) {
247 case TypeID.UINT16_TOKEN:
248 toReturn = StringParser.fromStringUINT16(string);
249 break;
250 case TypeID.INT16_TOKEN:
251 toReturn = StringParser.fromStringINT16(string);
252 break;
253 case TypeID.UINT32_TOKEN:
254 toReturn = StringParser.fromStringUINT32(string);
255 break;
256 case TypeID.INT32_TOKEN:
257 toReturn = StringParser.fromStringINT32(string);
258 break;
259 case TypeID.INT64_TOKEN:
260 toReturn = StringParser.fromStringINT64(string);
261 break;
262 case TypeID.UINT64_TOKEN:
263 toReturn = StringParser.fromStringUINT64(string);
264 break;
265 case TypeID.STRING_TOKEN:
266 toReturn = StringParser.fromStringSTRING(string);
267 break;
268 case TypeID.IPADDR_TOKEN:
269 toReturn = StringParser.fromStringIPADDR(string);
270 break;
271 case TypeID.BOOLEAN_TOKEN:
272 toReturn = StringParser.fromStringBOOLEAN(string);
273 break;
274 default:
275 throw new NoSuchAttributeTypeException("Unknown size of BaseType "
276 + typeName);
278 return toReturn;
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);
300 else {
301 log.warn("unaccounted for object class: " + value.getClass().getName());
302 return null;
306 public BaseType cloneBaseType() {
307 BaseType newBaseType = new BaseType(getTypeName(), getTypeToken(),
308 getTypeObject());
309 return newBaseType;
312 public String toString() {
313 return typeObject.toString();