1 24 25 package org.objectweb.dream.message; 26 27 import java.io.IOException ; 28 import java.io.ObjectInput ; 29 import java.io.ObjectOutput ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 33 37 public class ExtensibleMessageImpl extends AbstractExtensibleMessage 38 { 39 40 transient LinkedList subMessages = new LinkedList (); 41 transient Message[] subMessagesArray = null; 42 transient MessageType[] subMessageTypesArray = null; 43 44 48 51 public ExtensibleMessageImpl() 52 { 53 } 54 55 61 public ExtensibleMessageImpl(short messageManagerId) 62 { 63 this.messageManagerId = messageManagerId; 64 } 65 66 70 73 public void addSubMessage(Message message) 74 { 75 if (subMessages.add(message)) 76 { 77 subMessagesArray = null; 78 subMessageTypesArray = null; 79 } 80 } 81 82 85 public boolean removeSubMessage(Message msg) 86 { 87 boolean removed = subMessages.remove(msg); 88 if (removed) 89 { 90 subMessagesArray = null; 91 subMessageTypesArray = null; 92 } 93 return removed; 94 } 95 96 99 public void removeSubMessages() 100 { 101 subMessages.clear(); 102 subMessagesArray = null; 103 subMessageTypesArray = null; 104 } 105 106 110 113 public Iterator getSubMessageIterator() 114 { 115 return subMessages.iterator(); 116 } 117 118 121 public Message[] getSubMessages() 122 { 123 if (subMessagesArray == null) 124 { 125 if (subMessages.isEmpty()) 126 { 127 subMessagesArray = EMPTY_MESSAGE_ARRAY; 128 } 129 else 130 { 131 subMessagesArray = (Message[]) subMessages.toArray(EMPTY_MESSAGE_ARRAY); 132 } 133 } 134 return subMessagesArray; 135 } 136 137 141 144 public MessageType[] getSubMessageTypes() 145 { 146 if (subMessageTypesArray == null) 147 { 148 if (subMessages.isEmpty()) 149 { 150 subMessageTypesArray = EMPTY_MESSAGE_TYPE_ARRAY; 151 } 152 else 153 { 154 subMessageTypesArray = new MessageType[subMessages.size()]; 155 Iterator iter = getSubMessageIterator(); 156 int i = 0; 157 while (iter.hasNext()) 158 { 159 Message message = (Message) iter.next(); 160 subMessageTypesArray[i] = message.getMessageType(); 161 i++; 162 } 163 } 164 } 165 return subMessageTypesArray; 166 } 167 168 171 public Iterator getSubMessageTypesIterator() 172 { 173 return new SubMessageTypeIterator(); 174 } 175 176 179 public boolean isSubTypeOf(MessageType t) 180 { 181 throw new UnsupportedOperationException ("not yet implemented"); 183 } 184 185 private class SubMessageTypeIterator implements Iterator 186 { 187 188 Iterator messageIterator = getSubMessageIterator(); 189 190 193 public void remove() 194 { 195 throw new UnsupportedOperationException (); 196 } 197 198 201 public boolean hasNext() 202 { 203 return messageIterator.hasNext(); 204 } 205 206 209 public Object next() 210 { 211 return ((Message) messageIterator.next()).getMessageType(); 212 } 213 214 } 215 216 220 223 public void recycle() 224 { 225 super.recycle(); 226 subMessages.clear(); 227 subMessagesArray = null; 228 subMessageTypesArray = null; 229 } 230 231 235 238 public void readExternal(ObjectInput in) throws IOException , 239 ClassNotFoundException 240 { 241 referenceCounter = 1; 242 if (subMessages == null) { 244 subMessages = new LinkedList (); 245 } 246 readChunksState(in); 247 } 248 249 252 public void writeExternal(ObjectOutput out) throws IOException 253 { 254 writeChunksState(out); 255 } 256 } | Popular Tags |