KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.reflect;
2
3 import javax.jmi.reflect.*;
4 import java.util.*;
5
6 // this link is bind to Association
7
// the update to the other end is transparent to users
8
public class AssoEndList implements java.util.List JavaDoc {
9     
10     RefAssociationImpl asso;
11     RefObject key;
12     boolean keyIsFirstEnd;
13     
14     public AssoEndList(RefAssociationImpl asso, RefObject key, boolean keyIsFirstEnd) {
15         this.asso = asso;
16         this.key = key;
17         this.keyIsFirstEnd = keyIsFirstEnd;
18     }
19     
20     public boolean add(Object JavaDoc obj) {
21         if(forwardList().contains(obj)) return false;
22         reverseList(obj).add(key);
23         return forwardList().add(obj);
24     }
25     
26     public void add(int index, Object JavaDoc obj) {
27         int i = forwardList().indexOf(obj);
28         if(i==index) return;
29         if(i!=-1) throw new RuntimeException JavaDoc("obj already exists at " +i);
30         forwardList().add(index, obj);
31         reverseList(obj).add(key);
32     }
33     
34     public boolean addAll(java.util.Collection JavaDoc collection) {
35         Iterator it = collection.iterator();
36         boolean r = false;
37         while(it.hasNext()) {
38            Object JavaDoc o = it.next();
39            if(!forwardList().contains(o)) {
40               reverseList(o).add(key);
41               forwardList().add(o);
42               r = true;
43            }
44         }
45         return r;
46     }
47     
48     public boolean addAll(int index, java.util.Collection JavaDoc collection) {
49         throw new RuntimeException JavaDoc("not supported");
50     }
51     
52     public void clear() {
53        Iterator it = forwardList().iterator();
54        while(it.hasNext()) {
55           reverseList(it.next()).remove(key);
56        }
57        forwardList().clear();
58     }
59    
60        
61     public boolean remove(Object JavaDoc obj) {
62        reverseList(obj).remove(key);
63        return forwardList().remove(obj);
64     }
65     
66     public Object JavaDoc remove(int index) {
67        Object JavaDoc obj = forwardList().remove(index);
68        reverseList(obj).remove(key);
69        return obj;
70     }
71     
72     public boolean removeAll(java.util.Collection JavaDoc collection) {
73        Iterator it = collection.iterator();
74        boolean r = false;
75        while(it.hasNext()) {
76            Object JavaDoc o = it.next();
77            if(forwardList().contains(o)) {
78              reverseList(o).remove(key);
79              forwardList().remove(o);
80              r = true;
81           }
82        }
83        return r;
84     }
85     
86     public boolean retainAll(java.util.Collection JavaDoc collection) {
87        Iterator it = forwardList().iterator();
88        while(it.hasNext()) {
89            Object JavaDoc o = it.next();
90            if(!collection.contains(o) && forwardList().contains(o)) {
91              reverseList(o).remove(key);
92           }
93        }
94        return forwardList().retainAll(collection);
95     }
96     
97     public Object JavaDoc set(int index, Object JavaDoc obj) {
98       int i = forwardList().indexOf(obj);
99       if(i==index) return obj;
100       if(i!=-1) throw new RuntimeException JavaDoc("obj already exists at " +i);
101       Object JavaDoc replaced = forwardList().set(index, obj);
102       reverseList(obj).add(key);
103       reverseList(replaced).remove(key);
104       return replaced;
105     }
106     
107     //------------------------------------
108
// Helper functions
109

110     List forwardList = null;
111     
112     List forwardList() {
113         if(forwardList!=null) return forwardList;
114         if(keyIsFirstEnd) {
115            Object JavaDoc temp = asso.table_1_2.get(key);
116            forwardList = (List) temp;
117            if(forwardList==null) {
118              forwardList = new Vector();
119              asso.table_1_2.put(key, forwardList);
120            }
121         } else {
122            forwardList = (List) asso.table_2_1.get(key);
123            if(forwardList==null) {
124              forwardList = new Vector();
125              asso.table_2_1.put(key, forwardList);
126            }
127         }
128         return forwardList;
129     }
130     
131     List reverseList(Object JavaDoc o) {
132         if(!asso.container.canModify) {
133             throw new RuntimeException JavaDoc("Not allowed to modify");
134         }
135         List r;
136         if(keyIsFirstEnd) {
137            r = (List) asso.table_2_1.get(o);
138            if(r==null) {
139              r = new Vector();
140              asso.table_2_1.put(o, r);
141            }
142         } else {
143            r = (List) asso.table_1_2.get(o);
144            if(r==null) {
145              r = new Vector();
146              asso.table_1_2.put(o, r);
147            }
148         }
149         return r;
150     }
151     
152     public String JavaDoc toString() {
153        return forwardList().toString();
154     }
155     
156     
157     //--------------------------------------------------------------
158
//--------------------------------------------------------------
159
// Query operations
160

161     public boolean contains(Object JavaDoc obj) {
162        return forwardList().contains(obj);
163     }
164     
165     public boolean containsAll(java.util.Collection JavaDoc collection) {
166        return forwardList().containsAll(collection);
167     }
168     
169     public Object JavaDoc get(int index) {
170        return forwardList().get(index);
171     }
172     
173     public int indexOf(Object JavaDoc obj) {
174        return forwardList().indexOf(obj);
175     }
176     
177     public boolean isEmpty() {
178         return forwardList().isEmpty();
179     }
180     
181     public int lastIndexOf(Object JavaDoc obj) {
182         return forwardList().lastIndexOf(obj);
183     }
184     
185     // *** users should not use iterator to modify the list ***
186
public java.util.Iterator JavaDoc iterator() {
187         return forwardList().iterator();
188     }
189     public java.util.ListIterator JavaDoc listIterator() {
190         return forwardList().listIterator();
191     }
192     public java.util.ListIterator JavaDoc listIterator(int index) {
193         return forwardList().listIterator(index);
194     }
195     
196     public int size() {
197       return forwardList().size();
198     }
199     
200     public java.util.List JavaDoc subList(int i1, int i2) {
201       return forwardList().subList(i1, i2);
202     }
203     
204     public Object JavaDoc[] toArray() {
205       return forwardList().toArray();
206     }
207     
208     public Object JavaDoc[] toArray(Object JavaDoc[] obj) {
209       return forwardList().toArray(obj);
210     }
211     
212     
213 }
214
Popular Tags