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 { 7 8 String firstEndName; 9 String secondEndName; 10 String type1; 11 String type2; 12 boolean isEnd1Ordered; 13 boolean isEnd2Ordered; 14 15 Hashtable table_2_1 = new Hashtable(); Hashtable table_1_2 = new Hashtable(); 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 ("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 refAllLinks() { 48 doSync(); 49 Collection r = new Vector(); 50 51 if(isEnd2Ordered) { 52 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 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 refQuery(String 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 ("refQuery " +name +"(" +firstEndName +"," +secondEndName +"):Invalide name: " +endName); 96 } 97 98 public java.util.Collection refQuery(javax.jmi.reflect.RefObject meta, javax.jmi.reflect.RefObject endObj) { 99 return refQuery( (String )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 ("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 void doSync() { 123 } 124 125 147 148 String getOtherEndName(String end) { 149 return (firstEndName.equals(end))? secondEndName: 150 (secondEndName.equals(end))? firstEndName: null; 151 } 152 153 } 154 | Popular Tags |