KickJava   Java API By Example, From Geeks To Geeks.

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


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):
23  */

24
25 package org.objectweb.dream.message;
26
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.dream.util.EmptyIterator;
31 import org.objectweb.dream.util.MultipleIterator;
32
33 /**
34  */

35 public class MessageTypeNCImpl extends MessageTypeImpl implements MessageTypeNC
36 {
37
38   /** A constant designating the empty message type NC. */
39   public static final MessageType EMPTY_MESSAGE_TYPE_NC = new MessageTypeNCImpl();
40
41   Hashtable JavaDoc namedSubMessageTypes = null;
42   String JavaDoc[] subMessageNames = null;
43   MessageType[] allSubMessageTypesArray = null;
44
45   /**
46    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageType(java.lang.String)
47    */

48   public MessageType getSubMessageType(String JavaDoc name)
49   {
50     if (namedSubMessageTypes == null)
51     {
52       return null;
53     }
54     return (MessageType) namedSubMessageTypes.get(name);
55   }
56
57   /**
58    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageNames()
59    */

60   public String JavaDoc[] getSubMessageNames()
61   {
62     if (subMessageNames == null)
63     {
64       if (namedSubMessageTypes == null)
65       {
66         subMessageNames = EMPTY_STRING_ARRAY;
67       }
68       else
69       {
70         subMessageNames = (String JavaDoc[]) namedSubMessageTypes.keySet().toArray(
71             EMPTY_STRING_ARRAY);
72       }
73     }
74     return subMessageNames;
75   }
76
77   /**
78    * @see org.objectweb.dream.message.MessageTypeNC#getSubMessageNamesIterator()
79    */

80   public Iterator JavaDoc getSubMessageNamesIterator()
81   {
82     if (namedSubMessageTypes == null)
83     {
84       return EmptyIterator.INSTANCE;
85     }
86     return namedSubMessageTypes.keySet().iterator();
87   }
88
89   /**
90    * Adds a sub Message type
91    *
92    * @param messageName the name of the sub message type
93    * @param messageType the sub message type
94    * @throws MessageAlreadyExistException if this type already contains a sub
95    * message type with the specified name
96    */

97   public void addSubMessageType(String JavaDoc messageName, MessageType messageType)
98       throws MessageAlreadyExistException
99   {
100     if (namedSubMessageTypes == null)
101     {
102       namedSubMessageTypes = new Hashtable JavaDoc();
103       namedSubMessageTypes.put(messageName, messageType);
104     }
105     else
106     {
107       Object JavaDoc previousMessage = namedSubMessageTypes.put(messageName,
108           messageType);
109       if (previousMessage != null)
110       {
111         namedSubMessageTypes.put(messageName, previousMessage);
112         throw new MessageAlreadyExistException();
113       }
114     }
115     addSubMessageType(messageType);
116   }
117
118   /**
119    * Removes a sub message type
120    *
121    * @param subMessageName the name of the sub message type
122    * @return the sub message type or <code>null</code> if no sub message type
123    * with the specified name exists.
124    */

125   public MessageType removeSubMessageType(String JavaDoc subMessageName)
126   {
127     if (namedSubMessageTypes == null)
128     {
129       return null;
130     }
131     MessageType t = (MessageType) namedSubMessageTypes.get(subMessageName);
132     if (t != null)
133     {
134       removeSubMessageType(t);
135       if (namedSubMessageTypes.isEmpty())
136       {
137         namedSubMessageTypes = null;
138       }
139     }
140     return t;
141   }
142
143   /**
144    * @see org.objectweb.dream.message.MessageType#getSubMessageTypes()
145    */

146   public MessageType[] getSubMessageTypes()
147   {
148     if (allSubMessageTypesArray == null)
149     {
150       if (subMessageTypes.isEmpty() && namedSubMessageTypes.isEmpty())
151       {
152         allSubMessageTypesArray = EMPTY_MESSAGE_TYPE_ARRAY;
153       }
154       else
155       {
156         allSubMessageTypesArray = new MessageType[subMessageTypes.size()
157             + namedSubMessageTypes.size()];
158         subMessageTypes.toArray(allSubMessageTypesArray);
159         Iterator JavaDoc iter = namedSubMessageTypes.values().iterator();
160         int i = subMessageTypes.size();
161         while (iter.hasNext())
162         {
163           allSubMessageTypesArray[i] = (MessageType) iter.next();
164           i++;
165         }
166       }
167     }
168     return subMessageTypesArray;
169   }
170
171   /**
172    * @see org.objectweb.dream.message.MessageType#getSubMessageTypesIterator()
173    */

174   public Iterator JavaDoc getSubMessageTypesIterator()
175   {
176     return new MultipleIterator(new Iterator JavaDoc[]{subMessageTypes.iterator(),
177         namedSubMessageTypes.values().iterator()});
178   }
179
180   /**
181    * @see org.objectweb.dream.message.MessageTypeNC#getUnnamedSubMessageTypes()
182    */

183   public MessageType[] getUnnamedSubMessageTypes()
184   {
185     return super.getSubMessageTypes();
186   }
187
188   /**
189    * @see org.objectweb.dream.message.MessageTypeNC#getUnnamedSubMessageTypesIterator()
190    */

191   public Iterator JavaDoc getUnnamedSubMessageTypesIterator()
192   {
193     return super.getSubMessageTypesIterator();
194   }
195 }
Popular Tags