KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > ExtensibleMessageImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.message;
26
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInput JavaDoc;
29 import java.io.ObjectOutput JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32
33 /**
34  * Implementation of extensible message, using a {@link java.util.LinkedList}to
35  * store sub message
36  */

37 public class ExtensibleMessageImpl extends AbstractExtensibleMessage
38 {
39
40   transient LinkedList JavaDoc subMessages = new LinkedList JavaDoc();
41   transient Message[] subMessagesArray = null;
42   transient MessageType[] subMessageTypesArray = null;
43
44   // ---------------------------------------------------------------------------
45
// Constructors
46
// ---------------------------------------------------------------------------
47

48   /**
49    * No-arg constructor, used for instance pooling
50    */

51   public ExtensibleMessageImpl()
52   {
53   }
54
55   /**
56    * Constructor.
57    *
58    * @param messageManagerId the id of the message manager that creates this
59    * message.
60    */

61   public ExtensibleMessageImpl(short messageManagerId)
62   {
63     this.messageManagerId = messageManagerId;
64   }
65
66   // ---------------------------------------------------------------------------
67
// Implementation of ExtensibleMessage interface
68
// ---------------------------------------------------------------------------
69

70   /**
71    * @see ExtensibleMessage#addSubMessage(Message)
72    */

73   public void addSubMessage(Message message)
74   {
75     if (subMessages.add(message))
76     {
77       subMessagesArray = null;
78       subMessageTypesArray = null;
79     }
80   }
81
82   /**
83    * @see ExtensibleMessage#removeSubMessage(Message)
84    */

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   /**
97    * @see ExtensibleMessage#removeSubMessages()
98    */

99   public void removeSubMessages()
100   {
101     subMessages.clear();
102     subMessagesArray = null;
103     subMessageTypesArray = null;
104   }
105
106   // ---------------------------------------------------------------------------
107
// Implementation of Message interface
108
// ---------------------------------------------------------------------------
109

110   /**
111    * @see Message#getSubMessageIterator()
112    */

113   public Iterator JavaDoc getSubMessageIterator()
114   {
115     return subMessages.iterator();
116   }
117
118   /**
119    * @see Message#getSubMessages()
120    */

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   // ---------------------------------------------------------------------------
138
// Implementation of MessageType interface
139
// ---------------------------------------------------------------------------
140

141   /**
142    * @see MessageType#getSubMessageTypes()
143    */

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 JavaDoc 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   /**
169    * @see MessageType#getSubMessageTypesIterator()
170    */

171   public Iterator JavaDoc getSubMessageTypesIterator()
172   {
173     return new SubMessageTypeIterator();
174   }
175
176   /**
177    * @see MessageType#isSubTypeOf(MessageType)
178    */

179   public boolean isSubTypeOf(MessageType t)
180   {
181     // NOT IMPLEMENTED implement subtyping
182
throw new UnsupportedOperationException JavaDoc("not yet implemented");
183   }
184
185   private class SubMessageTypeIterator implements Iterator JavaDoc
186   {
187
188     Iterator JavaDoc messageIterator = getSubMessageIterator();
189
190     /**
191      * @see java.util.Iterator#remove()
192      */

193     public void remove()
194     {
195       throw new UnsupportedOperationException JavaDoc();
196     }
197
198     /**
199      * @see java.util.Iterator#hasNext()
200      */

201     public boolean hasNext()
202     {
203       return messageIterator.hasNext();
204     }
205
206     /**
207      * @see java.util.Iterator#next()
208      */

209     public Object JavaDoc next()
210     {
211       return ((Message) messageIterator.next()).getMessageType();
212     }
213
214   }
215
216   // ---------------------------------------------------------------------------
217
// Overriden method
218
// ---------------------------------------------------------------------------
219

220   /**
221    * @see org.objectweb.dream.pool.Recyclable#recycle()
222    */

223   public void recycle()
224   {
225     super.recycle();
226     subMessages.clear();
227     subMessagesArray = null;
228     subMessageTypesArray = null;
229   }
230
231   // ---------------------------------------------------------------------------
232
// Implementation of the serialization methods
233
// ---------------------------------------------------------------------------
234

235   /**
236    * @see java.io.Externalizable#readExternal(ObjectInput)
237    */

238   public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
239       ClassNotFoundException JavaDoc
240   {
241     referenceCounter = 1;
242     if (subMessages == null) // if message has not been reused, but recreated.
243
{
244       subMessages = new LinkedList JavaDoc();
245     }
246     readChunksState(in);
247   }
248
249   /**
250    * @see java.io.Externalizable#writeExternal(ObjectOutput)
251    */

252   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
253   {
254     writeChunksState(out);
255   }
256 }
Popular Tags