KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corba > Collective


1 /* Collective.java */
2 package org.enhydra.shark.corba;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * Collective
9  *
10  * @author V.Puskas
11  * @version 0.1
12  */

13 public interface Collective {
14    public void __recruit(org.omg.CORBA.Object JavaDoc obj);
15
16    public void __leave(org.omg.CORBA.Object JavaDoc _obj);
17
18    public void __disband(org.omg.CORBA.ORB JavaDoc _orb);
19
20    public final class CollectiveCORBA implements Collective {
21
22       private transient HashSet JavaDoc myObjects = new HashSet JavaDoc();
23
24       public synchronized void __recruit(org.omg.CORBA.Object JavaDoc obj) {
25          myObjects.add(obj);
26       }
27
28       public synchronized void __disband(org.omg.CORBA.ORB JavaDoc _orb) {
29          for (Iterator JavaDoc it = myObjects.iterator(); it.hasNext();) {
30             _orb.disconnect((org.omg.CORBA.Object JavaDoc) it.next());
31             it.remove();
32          }
33          myObjects = null;
34       }
35
36       public synchronized void __leave(org.omg.CORBA.Object JavaDoc obj) {
37          Iterator JavaDoc i=myObjects.iterator();
38          Object JavaDoc toRemove=obj;
39          while (i.hasNext()) {
40             org.omg.CORBA.Object JavaDoc o=(org.omg.CORBA.Object JavaDoc)i.next();
41             if (obj._is_equivalent(o)) {
42                toRemove=o;
43                break;
44             }
45          }
46          boolean r=myObjects.remove(toRemove);
47          if (r==false) System.out.println("Warning: object is not removed from collection!");
48       }
49    
50    }
51
52 }
Popular Tags