1 24 25 package org.objectweb.dream.message; 26 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 31 import org.objectweb.dream.util.EmptyIterator; 32 import org.objectweb.dream.util.EmptyStringArray; 33 34 37 public class MessageTypeImpl implements MessageType, EmptyStringArray 38 { 39 40 41 public static final MessageType EMPTY_MESSAGE_TYPE = new MessageTypeImpl(); 42 43 44 protected Hashtable chunkTypes = null; 45 46 protected LinkedList subMessageTypes = null; 47 48 49 protected String [] chkNames = null; 50 51 protected MessageType[] subMessageTypesArray = null; 52 53 58 protected MessageTypeImpl() 59 { 60 } 61 62 78 public MessageTypeImpl(String [] chkNames, ChunkType[] chkTypes, 79 MessageType[] msgTypes) throws ChunkAlreadyExistsException 80 { 81 ChunkAlreadyExistsException chunkAlreadyExistException = null; 82 if (chkNames != null) 83 { 84 if (chkNames.length != chkTypes.length) 85 { 86 throw new IllegalArgumentException ( 87 "chkNames and chkTypes do not have the same length"); 88 } 89 90 for (int i = 0; i < chkNames.length; i++) 91 { 92 try 93 { 94 addChunkType(chkNames[i], chkTypes[i]); 95 } 96 catch (ChunkAlreadyExistsException e) 97 { 98 chunkAlreadyExistException = e; 99 } 100 } 101 } 102 103 if (msgTypes != null) 104 { 105 for (int i = 0; i < msgTypes.length; i++) 106 { 107 addSubMessageType(msgTypes[i]); 108 } 109 } 110 if (chunkAlreadyExistException != null) 111 { 112 throw chunkAlreadyExistException; 113 } 114 } 115 116 123 public MessageTypeImpl(String chkName, ChunkType chkType) 124 { 125 chunkTypes = new Hashtable (); 126 chunkTypes.put(chkName, chkType); 127 } 128 129 140 public MessageTypeImpl(String chkName1, ChunkType chkType1, String chkName2, 141 ChunkType chkType2) throws ChunkAlreadyExistsException 142 { 143 addChunkType(chkName1, chkType1); 144 addChunkType(chkName2, chkType2); 145 } 146 147 150 public ChunkType getChunkType(String name) 151 { 152 if (chunkTypes == null) 153 { 154 return null; 155 } 156 return (ChunkType) chunkTypes.get(name); 157 158 } 159 160 163 public String [] getChunkNames() 164 { 165 if (chkNames == null) 166 { 167 if (chunkTypes == null) 168 { 169 chkNames = EMPTY_STRING_ARRAY; 170 } 171 else 172 { 173 chkNames = (String []) chunkTypes.keySet().toArray(EMPTY_STRING_ARRAY); 174 } 175 } 176 return chkNames; 177 } 178 179 182 public Iterator getChunkNamesIterator() 183 { 184 if (chunkTypes == null) 185 { 186 return EmptyIterator.INSTANCE; 187 } 188 return chunkTypes.keySet().iterator(); 189 } 190 191 194 public MessageType[] getSubMessageTypes() 195 { 196 if (subMessageTypesArray == null) 197 { 198 if (subMessageTypes == null) 199 { 200 subMessageTypesArray = EMPTY_MESSAGE_TYPE_ARRAY; 201 } 202 else 203 { 204 subMessageTypesArray = (MessageType[]) subMessageTypes 205 .toArray(EMPTY_MESSAGE_TYPE_ARRAY); 206 } 207 } 208 return subMessageTypesArray; 209 } 210 211 214 public Iterator getSubMessageTypesIterator() 215 { 216 if (subMessageTypes == null) 217 { 218 return EmptyIterator.INSTANCE; 219 } 220 return subMessageTypes.iterator(); 221 } 222 223 226 public boolean isEmpty() 227 { 228 return chunkTypes == null && subMessageTypes == null; 229 } 230 231 234 public boolean isSubTypeOf(MessageType t) 235 { 236 if (t.isEmpty()) 237 { 238 return true; 239 } 240 241 String [] tChkNames = t.getChunkNames(); 243 for (int i = 0; i < tChkNames.length; i++) 244 { 245 String name = tChkNames[i]; 246 ChunkType thisChkType = (ChunkType) chunkTypes.get(name); 247 if (thisChkType == null) 248 { 249 return false; 252 } 253 ChunkType tChkType = t.getChunkType(name); 254 if (tChkType == null) 255 { 256 return false; 257 } 258 if (!thisChkType.isSubTypeOf(tChkType)) 259 { 260 return false; 261 } 262 } 263 264 return true; 267 } 268 269 277 public void addChunkType(String chunkName, ChunkType chunkType) 278 throws ChunkAlreadyExistsException 279 { 280 if (chunkTypes == null) 281 { 282 chunkTypes = new Hashtable (); 283 } 284 Object previousChunk = chunkTypes.put(chunkName, chunkType); 285 if (previousChunk != null) 286 { 287 chunkTypes.put(chunkName, chunkType); 288 throw new ChunkAlreadyExistsException( 289 "Chunk type with the same name already exist"); 290 } 291 chkNames = null; 293 } 294 295 300 public void addSubMessageType(MessageType msgType) 301 { 302 if (subMessageTypes == null) 303 { 304 subMessageTypes = new LinkedList (); 305 } 306 subMessageTypes.add(msgType); 307 subMessageTypesArray = null; 309 } 310 311 318 public ChunkType removeChunkType(String name) 319 { 320 if (chunkTypes == null) 321 { 322 return null; 323 } 324 ChunkType t = (ChunkType) chunkTypes.remove(name); 325 if (t != null) 326 { 327 chkNames = null; 329 if (chunkTypes.isEmpty()) 330 { 331 chunkTypes = null; 332 } 333 } 334 return t; 335 } 336 337 343 public boolean removeSubMessageType(MessageType msgType) 344 { 345 if (subMessageTypes == null) 346 { 347 return false; 348 } 349 if (subMessageTypes.remove(msgType)) 350 { 351 subMessageTypesArray = null; 353 if (subMessageTypes.isEmpty()) 354 { 355 subMessageTypes = null; 356 } 357 return true; 358 } 359 return false; 360 } 361 } | Popular Tags |