1 package org.objectweb.modfact.jmi.reflect; 2 3 import javax.jmi.reflect.*; 4 import java.util.*; 5 6 public class AssoEndList implements java.util.List { 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 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 obj) { 27 int i = forwardList().indexOf(obj); 28 if(i==index) return; 29 if(i!=-1) throw new RuntimeException ("obj already exists at " +i); 30 forwardList().add(index, obj); 31 reverseList(obj).add(key); 32 } 33 34 public boolean addAll(java.util.Collection collection) { 35 Iterator it = collection.iterator(); 36 boolean r = false; 37 while(it.hasNext()) { 38 Object 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 collection) { 49 throw new RuntimeException ("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 obj) { 62 reverseList(obj).remove(key); 63 return forwardList().remove(obj); 64 } 65 66 public Object remove(int index) { 67 Object obj = forwardList().remove(index); 68 reverseList(obj).remove(key); 69 return obj; 70 } 71 72 public boolean removeAll(java.util.Collection collection) { 73 Iterator it = collection.iterator(); 74 boolean r = false; 75 while(it.hasNext()) { 76 Object 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 collection) { 87 Iterator it = forwardList().iterator(); 88 while(it.hasNext()) { 89 Object 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 set(int index, Object obj) { 98 int i = forwardList().indexOf(obj); 99 if(i==index) return obj; 100 if(i!=-1) throw new RuntimeException ("obj already exists at " +i); 101 Object replaced = forwardList().set(index, obj); 102 reverseList(obj).add(key); 103 reverseList(replaced).remove(key); 104 return replaced; 105 } 106 107 110 List forwardList = null; 111 112 List forwardList() { 113 if(forwardList!=null) return forwardList; 114 if(keyIsFirstEnd) { 115 Object 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 o) { 132 if(!asso.container.canModify) { 133 throw new RuntimeException ("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 toString() { 153 return forwardList().toString(); 154 } 155 156 157 161 public boolean contains(Object obj) { 162 return forwardList().contains(obj); 163 } 164 165 public boolean containsAll(java.util.Collection collection) { 166 return forwardList().containsAll(collection); 167 } 168 169 public Object get(int index) { 170 return forwardList().get(index); 171 } 172 173 public int indexOf(Object obj) { 174 return forwardList().indexOf(obj); 175 } 176 177 public boolean isEmpty() { 178 return forwardList().isEmpty(); 179 } 180 181 public int lastIndexOf(Object obj) { 182 return forwardList().lastIndexOf(obj); 183 } 184 185 public java.util.Iterator iterator() { 187 return forwardList().iterator(); 188 } 189 public java.util.ListIterator listIterator() { 190 return forwardList().listIterator(); 191 } 192 public java.util.ListIterator 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 subList(int i1, int i2) { 201 return forwardList().subList(i1, i2); 202 } 203 204 public Object [] toArray() { 205 return forwardList().toArray(); 206 } 207 208 public Object [] toArray(Object [] obj) { 209 return forwardList().toArray(obj); 210 } 211 212 213 } 214 | Popular Tags |