1 51 52 package org.objectweb.jass.as.util; 53 54 import java.lang.reflect.Array ; 55 import java.util.Arrays ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.LinkedList ; 59 import java.util.Set ; 60 61 import javax.activity.coordination.Action; 62 import javax.activity.coordination.SignalSet; 63 64 import org.apache.log4j.Logger; 65 66 72 public class SignalSetInfo { 73 74 private static Logger log = Logger.getLogger(SignalSetInfo.class); 75 76 78 private SignalSet signalSet; 79 private HashMap registeredActions = new HashMap (); 80 81 83 87 public SignalSetInfo(SignalSet signalSet) { 88 this.signalSet = signalSet; 89 } 90 91 93 97 public SignalSet getSignalSet() { 98 return signalSet; 99 } 100 101 107 public void addAction(Action action, int priority, boolean global) { 108 109 LinkedList actions = null; 110 111 if (priority >= 0) 112 actions = (LinkedList ) registeredActions.get(new Integer (priority)); 113 else 114 actions = (LinkedList ) registeredActions.get(new Integer (0)); 115 116 if (actions == null) 117 actions = new LinkedList (); 118 119 actions.add(new ActionInfo(action, global)); 120 121 if (priority >= 0) 123 registeredActions.put(new Integer (priority), actions); 124 else registeredActions.put(new Integer (0), actions); 126 127 } 128 129 public Action[] getActions() { 131 Action[] actions = null; 132 133 Object [] keys = registeredActions.keySet().toArray(); 135 Arrays.sort(keys); 136 137 139 LinkedList actionInfoList = new LinkedList (); 141 142 for (int j = (keys.length - 1); j >= 0; j--) 143 actionInfoList.addAll((LinkedList ) registeredActions.get(keys[j])); 144 145 LinkedList actionList = new LinkedList (); 147 Iterator it = actionInfoList.iterator(); 148 149 while (it.hasNext()) { 150 actionList.add(((ActionInfo) it.next()).getAction()); 151 } 152 153 Object [] array = actionList.toArray(); 155 actions = (Action[]) Array.newInstance(Action.class, array.length); 156 for (int i = 0; i < array.length; i++) 157 actions[i] = (Action) array[i]; 158 159 return actions; 160 } 161 162 168 public boolean removeAction(Action action, boolean global) { 169 170 boolean removed = false; 171 Set keys = registeredActions.keySet(); 172 Iterator itra = keys.iterator(); 173 174 while (itra.hasNext() && (removed == false)) { 175 Integer priority = (Integer ) itra.next(); 176 LinkedList actionInfoList = 177 (LinkedList ) registeredActions.get(priority); 178 Iterator itail = actionInfoList.iterator(); 179 180 while (itail.hasNext() && (removed == false)) { 181 ActionInfo actionInfo = ((ActionInfo) itail.next()); 182 183 if (global) { 186 if ((action.equals(actionInfo.getAction())) 187 && (actionInfo.isGlobal() == true)) { 188 actionInfoList.remove(actionInfo); 189 removed = true; 190 } 191 192 } else { 193 if ((action.equals(actionInfo.getAction())) 194 && (actionInfo.isGlobal() == false)) { 195 actionInfoList.remove(actionInfo); 196 removed = true; 197 } 198 } 199 } 200 if (removed) { 201 if (actionInfoList.isEmpty()) 202 registeredActions.remove(priority); 203 } 204 } 205 206 return removed; 207 } 208 209 213 public int getNumberRegisteredActions() { 214 return getActions().length; 215 } 216 217 220 public String toString() { 221 String result = "Signal Set: " + signalSet.getSignalSetName() + "\n"; 222 Action[] actions = null; 223 224 Object [] keys = registeredActions.keySet().toArray(); 226 227 Arrays.sort(keys); 229 230 for (int i = (keys.length - 1); i >= 0; i--) { 231 result += "\nActions in Priority = " + keys[i] + "\n"; 232 result += registeredActions.get(keys[i]); 233 } 234 235 return result; 236 } 237 238 } 239 | Popular Tags |