KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > reflect > RefAssociationImpl


1 package org.objectweb.modfact.jmi.reflect;
2
3 import javax.jmi.reflect.*;
4 import java.util.*;
5
6 public class RefAssociationImpl extends RefBaseObjectImpl implements RefAssociation, java.io.Serializable JavaDoc {
7     
8     String JavaDoc firstEndName;
9     String JavaDoc secondEndName;
10     String JavaDoc type1;
11     String JavaDoc type2;
12     boolean isEnd1Ordered;
13     boolean isEnd2Ordered;
14     
15     Hashtable table_2_1 = new Hashtable(); // second end (key)-> first ends (Collection)
16
Hashtable table_1_2 = new Hashtable(); // first end (key)-> second ends (Collection)
17

18     public RefAssociationImpl() {
19     }
20     
21     
22     public boolean refAddLink(javax.jmi.reflect.RefObject first, javax.jmi.reflect.RefObject second) {
23         if(!container.canModify) {
24             throw new RuntimeException JavaDoc("Not allowed to modify");
25         }
26         return _refAddLink(first ,second);
27     }
28     
29     boolean _refAddLink(javax.jmi.reflect.RefObject first, javax.jmi.reflect.RefObject second) {
30         doSync();
31         Collection firstSet = (Collection) table_2_1.get(second);
32         Collection secondSet = (Collection) table_1_2.get(first);
33         if (firstSet!=null && firstSet.contains(first) ) return false;
34         if(firstSet==null) {
35           firstSet = new Vector();
36           table_2_1.put(second ,firstSet);
37         }
38         if(secondSet==null) {
39           secondSet = new Vector();
40           table_1_2.put(first ,secondSet);
41         }
42         firstSet.add(first);
43         secondSet.add(second);
44         return true;
45     }
46     
47     public java.util.Collection JavaDoc refAllLinks() {
48         doSync();
49         Collection r = new Vector();
50         
51         if(isEnd2Ordered) {
52           // System.out.println("Association '" +name +"' end2 '" +secondEndName +"' is ordered");
53
Iterator it = table_1_2.keySet().iterator();
54           while(it.hasNext()) {
55             RefObject first = (RefObject) it.next();
56             Iterator it2 = ((Collection)table_1_2.get(first)).iterator();
57             while(it2.hasNext()) {
58               RefObject second = (RefObject) it2.next();
59               r.add(new RefAssociationLinkImpl(first,second));
60             }
61           }
62         } else {
63           
64           //if(isEnd1Ordered) System.out.println("Association '" +name +"' end1 '" +firstEndName +"' is ordered");
65
//else System.out.println("Association '" +name +"' is NOT ordered");
66

67           Iterator it = table_2_1.keySet().iterator();
68           while(it.hasNext()) {
69             RefObject second = (RefObject) it.next();
70             Iterator it2 = ((Collection)table_2_1.get(second)).iterator();
71             while(it2.hasNext()) {
72               RefObject first = (RefObject) it2.next();
73               r.add(new RefAssociationLinkImpl(first,second));
74             }
75           }
76         }
77         return r;
78     }
79     
80     public boolean refLinkExists(javax.jmi.reflect.RefObject first, javax.jmi.reflect.RefObject second) {
81         doSync();
82         Collection firstSet = (Collection) table_2_1.get(second);
83         if (firstSet==null) return false;
84         return firstSet.contains(first);
85     }
86     
87     public java.util.Collection JavaDoc refQuery(String JavaDoc endName, javax.jmi.reflect.RefObject endObj) {
88         doSync();
89         if(endName.equals(firstEndName)) {
90             return new AssoEndList(this, endObj, true );
91         }
92         if(endName.equals(secondEndName)) {
93             return new AssoEndList(this, endObj, false);
94         }
95         throw new RuntimeException JavaDoc("refQuery " +name +"(" +firstEndName +"," +secondEndName +"):Invalide name: " +endName);
96     }
97     
98     public java.util.Collection JavaDoc refQuery(javax.jmi.reflect.RefObject meta, javax.jmi.reflect.RefObject endObj) {
99         return refQuery( (String JavaDoc)meta.refGetValue("name") ,endObj );
100     }
101     
102     public boolean refRemoveLink(javax.jmi.reflect.RefObject first, javax.jmi.reflect.RefObject second) {
103         if(!container.canModify) {
104             throw new RuntimeException JavaDoc("Not allowed to modify");
105         }
106         doSync();
107         Collection firstSet = (Collection) table_2_1.get(second);
108         Collection secondSet = (Collection) table_1_2.get(first);
109         if (firstSet==null) return false;
110         firstSet.remove(first);
111         return secondSet.remove(second);
112     }
113
114     public javax.jmi.reflect.RefObject refMetaObject() {
115         if(metaObject!=null) return metaObject;
116         return findObjectByName( container.refClass("Association"), name);
117     }
118     
119     // this method is used to save the storage space
120
// One table can be derived from the other
121
// "isOrdered" table must be persistent!
122
void doSync() {
123     }
124     
125     /*
126     // create TableB from TableA;
127     // for every key a in A, find {b1, b2, ..., bn}
128     // for bi
129     // B.get(bi).add(a)
130     {
131         tableB = new Hashtable();
132         Iterator it = tableA.keySet().iterator();
133         while(it.hasNext()) {
134           RefObject a = (RefObject) it.next();
135           Iterator it2 = ((List)A.get(a)).iterator();
136           while(it2.hasNext()) {
137             RefObject bi = (RefObject) it2.next();
138             List l = (List) B.get(bi);
139             if(l==null) {
140                l = new Vector();
141                B.put(bi, l);
142             }
143             l.add(a);
144           }
145         }
146     */

147     
148     String JavaDoc getOtherEndName(String JavaDoc end) {
149         return (firstEndName.equals(end))? secondEndName:
150                   (secondEndName.equals(end))? firstEndName: null;
151     }
152     
153 }
154
Popular Tags