KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > RoleMultiple


1 /*
2  * Copyright (C) 1996 - 2000 BULL
3  * Copyright (C) 1996 - 2000 INRIA
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.1 of the License, or 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
18  * USA.
19  */

20
21
22 package fr.dyade.aaa.agent;
23
24 import java.io.*;
25 import java.util.*;
26
27
28 /**
29  * This structure provides code for managing target agents registering
30  * in a role. A notification may be sent to a role using the <code>sendTo</code>
31  * function of the sending agent.
32  *
33  * The class does not handle duplicates in the list.
34  */

35 public class RoleMultiple implements Serializable {
36
37
38
39   private String JavaDoc name;
40   private Vector list = null;
41
42   public RoleMultiple() {}
43
44   /**
45    * Creates a new RoleMultiple with a specified name.
46    * @param name the role name.
47    */

48   public RoleMultiple(String JavaDoc name) {
49       this.name = name;
50   }
51
52   /**
53    * Adds an agent in the listeners list.
54    */

55   public void addListener(AgentId target) {
56     if (list == null)
57       list = new Vector();
58     list.addElement(target);
59   }
60
61   /**
62    * Removes an agent from the listeners list.
63    */

64   public void removeListener(AgentId target) {
65     if (list == null)
66       return;
67     for (int i = list.size(); i-- > 0;) {
68       AgentId id = (AgentId) list.elementAt(i);
69       if (target.equals(id)) {
70     list.removeElement(id);
71     break;
72       }
73     }
74   }
75
76   /**
77    * Gets the listeners list as an <code>Enumeration</code> of <code>AgentId</code> objects.
78    *
79    * There is no synchronization as we assume this object is manipulated
80    * from the enclosing agent reaction.
81    */

82   public Enumeration getListeners() {
83     if (list == null)
84       return null;
85     return list.elements();
86   }
87
88   /**
89    * Returns the role name.
90    */

91   public String JavaDoc getName() {
92     return name;
93   }
94     
95   /**
96    * Sets the role name.
97    * @param name the role name.
98    */

99   public void setName(String JavaDoc name) {
100     this.name = name;
101   }
102
103   /**
104    * Tests if the specified agent id belongs to
105    * role multiple.
106    * @param id the specified agent id.
107    * @return true if the specified id belongs to the role;
108    * false otherwise.
109    */

110   public boolean contains(AgentId id) {
111     if (list == null)
112       return false;
113     return list.contains(id);
114   }
115   
116   /**
117    * Provides a string image for this object.
118    */

119   public String JavaDoc toString() {
120     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
121     output.append("(");
122     output.append(super.toString());
123     output.append(",name=" + name);
124     output.append(",list=");
125     if (list == null) {
126       output.append("null");
127     } else {
128       output.append("(");
129       output.append(list.size());
130       for (int i = 0; i < list.size(); i ++) {
131     output.append(",");
132     output.append((AgentId) list.elementAt(i));
133       }
134       output.append(")");
135     }
136     output.append(")");
137     return output.toString();
138   }
139 }
140
Popular Tags