1 24 25 package org.objectweb.dream.message; 26 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 30 import org.objectweb.dream.util.EmptyIterator; 31 import org.objectweb.dream.util.MultipleIterator; 32 33 35 public class MessageTypeNCImpl extends MessageTypeImpl implements MessageTypeNC 36 { 37 38 39 public static final MessageType EMPTY_MESSAGE_TYPE_NC = new MessageTypeNCImpl(); 40 41 Hashtable namedSubMessageTypes = null; 42 String [] subMessageNames = null; 43 MessageType[] allSubMessageTypesArray = null; 44 45 48 public MessageType getSubMessageType(String name) 49 { 50 if (namedSubMessageTypes == null) 51 { 52 return null; 53 } 54 return (MessageType) namedSubMessageTypes.get(name); 55 } 56 57 60 public String [] getSubMessageNames() 61 { 62 if (subMessageNames == null) 63 { 64 if (namedSubMessageTypes == null) 65 { 66 subMessageNames = EMPTY_STRING_ARRAY; 67 } 68 else 69 { 70 subMessageNames = (String []) namedSubMessageTypes.keySet().toArray( 71 EMPTY_STRING_ARRAY); 72 } 73 } 74 return subMessageNames; 75 } 76 77 80 public Iterator getSubMessageNamesIterator() 81 { 82 if (namedSubMessageTypes == null) 83 { 84 return EmptyIterator.INSTANCE; 85 } 86 return namedSubMessageTypes.keySet().iterator(); 87 } 88 89 97 public void addSubMessageType(String messageName, MessageType messageType) 98 throws MessageAlreadyExistException 99 { 100 if (namedSubMessageTypes == null) 101 { 102 namedSubMessageTypes = new Hashtable (); 103 namedSubMessageTypes.put(messageName, messageType); 104 } 105 else 106 { 107 Object 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 125 public MessageType removeSubMessageType(String 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 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 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 174 public Iterator getSubMessageTypesIterator() 175 { 176 return new MultipleIterator(new Iterator []{subMessageTypes.iterator(), 177 namedSubMessageTypes.values().iterator()}); 178 } 179 180 183 public MessageType[] getUnnamedSubMessageTypes() 184 { 185 return super.getSubMessageTypes(); 186 } 187 188 191 public Iterator getUnnamedSubMessageTypesIterator() 192 { 193 return super.getSubMessageTypesIterator(); 194 } 195 } | Popular Tags |