KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jass > as > util > SignalSetInfo


1 /**
2  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3  -
4  - JASS: Java Advanced tranSaction Support
5  -
6  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7  -
8  - This module was originally developed by
9  -
10  - LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
11  - at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
12  - (http://www.objectweb.org) project.
13  -
14  - This project has been partially funded by the European Commission under
15  - the IST programme of V FP grant IST-2001-37126 and by the Spanish
16  - Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
17  - TIC2001-1586-C03-02
18  -
19  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20  - The original code and portions created by LSD are
21  - Copyright (c) 2004 LSD (UPM)
22  - All rights reserved.
23  -
24  - Redistribution and use in source and binary forms, with or without
25  - modification, are permitted provided that the following conditions are met:
26  -
27  - -Redistributions of source code must retain the above copyright notice, this
28  - list of conditions and the following disclaimer.
29  -
30  - -Redistributions in binary form must reproduce the above copyright notice,
31  - this list of conditions and the following disclaimer in the documentation
32  - and/or other materials provided with the distribution.
33  -
34  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38  - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  - POSSIBILITY OF SUCH DAMAGE.
45  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46  -
47  - Author: Francisco Perez Sorrosal (frperezs)
48  -
49  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 */

51
52 package org.objectweb.jass.as.util;
53
54 import java.lang.reflect.Array JavaDoc;
55 import java.util.Arrays JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.LinkedList JavaDoc;
59 import java.util.Set JavaDoc;
60
61 import javax.activity.coordination.Action;
62 import javax.activity.coordination.SignalSet;
63
64 import org.apache.log4j.Logger;
65
66 /**
67  * Manages useful information about a signal set.
68  * @author fran
69  * Date: Feb 16, 2004
70  * org.objectweb.jass.as.utilSignalSetInfo.java
71  */

72 public class SignalSetInfo {
73
74     private static Logger log = Logger.getLogger(SignalSetInfo.class);
75
76     // Attributes ------------------------------------------------------------
77

78     private SignalSet signalSet;
79     private HashMap JavaDoc registeredActions = new HashMap JavaDoc();
80
81     // Constructors ----------------------------------------------------------
82

83     /**
84      * Constructor.
85      * @param signalSet the signal set.
86      */

87     public SignalSetInfo(SignalSet signalSet) {
88         this.signalSet = signalSet;
89     }
90
91     // My Public -------------------------------------------------------------
92

93     /**
94      * Returns the managed signal set.
95      * @return the signal set
96      */

97     public SignalSet getSignalSet() {
98         return signalSet;
99     }
100
101     /**
102      * Registers an action into the managed signal set.
103      * @param action the action.
104      * @param priority its priority.
105      * @param global true if the action is global.
106      */

107     public void addAction(Action action, int priority, boolean global) {
108
109         LinkedList JavaDoc actions = null;
110
111         if (priority >= 0)
112             actions = (LinkedList JavaDoc) registeredActions.get(new Integer JavaDoc(priority));
113         else
114             actions = (LinkedList JavaDoc) registeredActions.get(new Integer JavaDoc(0));
115
116         if (actions == null)
117             actions = new LinkedList JavaDoc();
118
119         actions.add(new ActionInfo(action, global));
120
121         // We put the action in the corresponding priority
122
if (priority >= 0)
123             registeredActions.put(new Integer JavaDoc(priority), actions);
124         else // Implementation specific for invalid priority values
125
registeredActions.put(new Integer JavaDoc(0), actions);
126         
127     }
128
129     // Returns an array containing the actions classified by priority
130
public Action[] getActions() {
131         Action[] actions = null;
132
133         // We obtain sorted the registered priorities (keys of the HashMap).
134
Object JavaDoc[] keys = registeredActions.keySet().toArray();
135         Arrays.sort(keys);
136
137         // With those keys, we obtain an array of actions ordered by priority in three steps:
138

139         // First, we get the complete actionInfo list...
140
LinkedList JavaDoc actionInfoList = new LinkedList JavaDoc();
141
142         for (int j = (keys.length - 1); j >= 0; j--)
143             actionInfoList.addAll((LinkedList JavaDoc) registeredActions.get(keys[j]));
144
145         // ...then, we get the actions from that list obtaining a new list and...
146
LinkedList JavaDoc actionList = new LinkedList JavaDoc();
147         Iterator JavaDoc it = actionInfoList.iterator();
148
149         while (it.hasNext()) {
150             actionList.add(((ActionInfo) it.next()).getAction());
151         }
152
153         // ...finally, we cast the list of actions to an array of Actions
154
Object JavaDoc[] 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     /**
163      * Removes an action from the managed signal set.
164      * @param action the action.
165      * @param global true if the action to remove is global.
166      * @return true if the action has been removed.
167      */

168     public boolean removeAction(Action action, boolean global) {
169
170         boolean removed = false;
171         Set JavaDoc keys = registeredActions.keySet();
172         Iterator JavaDoc itra = keys.iterator();
173
174         while (itra.hasNext() && (removed == false)) {
175             Integer JavaDoc priority = (Integer JavaDoc) itra.next();
176             LinkedList JavaDoc actionInfoList =
177                 (LinkedList JavaDoc) registeredActions.get(priority);
178             Iterator JavaDoc itail = actionInfoList.iterator();
179
180             while (itail.hasNext() && (removed == false)) {
181                 ActionInfo actionInfo = ((ActionInfo) itail.next());
182
183                 // If we want to remove a global action, the registered action
184
// must be global
185
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     /**
210      * Returns the number of registered actions in the managed signal set.
211      * @return the number of registered actions.
212      */

213     public int getNumberRegisteredActions() {
214         return getActions().length;
215     }
216     
217     /**
218      * Returns the string representation of the managed signal set.
219      */

220     public String JavaDoc toString() {
221         String JavaDoc result = "Signal Set: " + signalSet.getSignalSetName() + "\n";
222         Action[] actions = null;
223
224         // We obtain sorted the registered priorities (keys of the HashMap).
225
Object JavaDoc[] keys = registeredActions.keySet().toArray();
226         
227         // With those keys, we obtain an array of actions ordered by priority.
228
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