KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.dream.util.MultipleIterator;
32
33 /**
34  * Implementation of extensible message with named sub messages.
35  */

36 public class ExtensibleMessageNCImpl extends ExtensibleMessageImpl
37     implements
38       ExtensibleMessageNC,
39       MessageTypeNC
40 {
41
42   Map JavaDoc subMessageMap = new HashMap JavaDoc();
43   Message[] allSubMessagesArray = null;
44   MessageType[] allSubMessageTypesArray = null;
45
46   // ---------------------------------------------------------------------------
47
// Constructors
48
// ---------------------------------------------------------------------------
49

50   /**
51    * No-arg constructor, used for instance pooling
52    */

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

63   public ExtensibleMessageNCImpl(short messageManagerId)
64   {
65     super(messageManagerId);
66   }
67
68   // ---------------------------------------------------------------------------
69
// Implementation of ExtensibleMessageNC interface
70
// ---------------------------------------------------------------------------
71

72   /**
73    * @see org.objectweb.dream.message.MessageNC#getSubMessage(java.lang.String)
74    */

75   public Message getSubMessage(String JavaDoc name)
76   {
77     return (Message) subMessageMap.get(name);
78   }
79
80   /**
81    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageNames()
82    */

83   public String JavaDoc[] getSubMessageNames()
84   {
85     return (String JavaDoc[]) subMessageMap.keySet().toArray(EMPTY_STRING_ARRAY);
86   }
87
88   /**
89    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageNamesIterator()
90    */

91   public Iterator JavaDoc getSubMessageNamesIterator()
92   {
93     return subMessageMap.keySet().iterator();
94   }
95
96   /**
97    * @see org.objectweb.dream.message.ExtensibleMessageNC#addSubMessage(java.lang.String,
98    * org.objectweb.dream.message.Message)
99    */

100   public void addSubMessage(String JavaDoc name, Message message)
101       throws MessageAlreadyExistException
102   {
103     subMessageMap.put(name, message);
104     subMessagesArray = null;
105     subMessageTypesArray = null;
106     allSubMessagesArray = null;
107     allSubMessageTypesArray = null;
108   }
109
110   /**
111    * @see org.objectweb.dream.message.ExtensibleMessageNC#removeSubMessage(java.lang.String)
112    */

113   public Message removeSubMessage(String JavaDoc name)
114   {
115     Message removed = (Message) subMessageMap.remove(name);
116     if (removed != null)
117     {
118       subMessagesArray = null;
119       subMessageTypesArray = null;
120       allSubMessagesArray = null;
121       allSubMessageTypesArray = null;
122     }
123     return removed;
124   }
125
126   /**
127    * @see org.objectweb.dream.message.MessageNC#getUnnamedSubMessageIterator()
128    */

129   public Iterator JavaDoc getUnnamedSubMessageIterator()
130   {
131     return super.getSubMessageIterator();
132   }
133
134   /**
135    * @see org.objectweb.dream.message.MessageNC#getUnnamedSubMessages()
136    */

137   public Message[] getUnnamedSubMessages()
138   {
139     return super.getSubMessages();
140   }
141
142   // ---------------------------------------------------------------------------
143
// Implementation of MessageTypeNC interface
144
// ---------------------------------------------------------------------------
145

146   /**
147    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageType(java.lang.String)
148    */

149   public MessageType getSubMessageType(String JavaDoc name)
150   {
151     Message msg = getSubMessage(name);
152     if (msg == null)
153     {
154       return null;
155     }
156     return msg.getMessageType();
157   }
158
159   /**
160    * @see org.objectweb.dream.message.MessageTypeNC#getUnnamedSubMessageTypes()
161    */

162   public MessageType[] getUnnamedSubMessageTypes()
163   {
164     return super.getSubMessageTypes();
165   }
166
167   /**
168    * @see org.objectweb.dream.message.MessageTypeNC#getUnnamedSubMessageTypesIterator()
169    */

170   public Iterator JavaDoc getUnnamedSubMessageTypesIterator()
171   {
172     return super.getSubMessageTypesIterator();
173   }
174
175   // ---------------------------------------------------------------------------
176
// Overriden methods
177
// ---------------------------------------------------------------------------
178

179   /**
180    * @see org.objectweb.dream.message.Message#getSubMessageIterator()
181    */

182   public Iterator JavaDoc getSubMessageIterator()
183   {
184     return new MultipleIterator(new Iterator JavaDoc[]{
185         subMessageMap.values().iterator(), super.getSubMessageIterator()});
186   }
187
188   /**
189    * @see org.objectweb.dream.message.Message#getSubMessages()
190    */

191   public Message[] getSubMessages()
192   {
193     if (allSubMessagesArray == null)
194     {
195       if (subMessages.isEmpty() && subMessageMap.isEmpty())
196       {
197         allSubMessagesArray = EMPTY_MESSAGE_ARRAY;
198       }
199       else
200       {
201         allSubMessagesArray = new Message[subMessages.size()
202             + subMessageMap.size()];
203         subMessages.toArray(allSubMessagesArray);
204         Iterator JavaDoc iter = subMessageMap.values().iterator();
205         int i = subMessages.size();
206         while (iter.hasNext())
207         {
208           allSubMessagesArray[i] = (Message) iter.next();
209           i++;
210         }
211       }
212     }
213     return allSubMessagesArray;
214   }
215
216   /**
217    * @see org.objectweb.dream.message.MessageType#getSubMessageTypes()
218    */

219   public MessageType[] getSubMessageTypes()
220   {
221     if (allSubMessageTypesArray == null)
222     {
223       if (subMessages.isEmpty() && subMessageMap.isEmpty())
224       {
225         allSubMessageTypesArray = EMPTY_MESSAGE_TYPE_ARRAY;
226       }
227       else
228       {
229         allSubMessageTypesArray = new MessageType[subMessages.size()];
230         Iterator JavaDoc iter = getSubMessageIterator();
231         int i = 0;
232         while (iter.hasNext())
233         {
234           Message message = (Message) iter.next();
235           allSubMessageTypesArray[i] = message.getMessageType();
236           i++;
237         }
238       }
239     }
240     return allSubMessageTypesArray;
241   }
242
243   /**
244    * @see org.objectweb.dream.pool.Recyclable#recycle()
245    */

246   public void recycle()
247   {
248     super.recycle();
249     if (!subMessageMap.isEmpty())
250     {
251       subMessageMap.clear();
252     }
253     allSubMessagesArray = null;
254     allSubMessageTypesArray = null;
255   }
256
257 }
Popular Tags