1 8 14 package jfun.yan.containers; 15 16 17 import java.util.Iterator ; 18 import java.util.LinkedHashMap ; 19 20 import jfun.yan.AmbiguousComponentResolutionException; 21 import jfun.yan.Component; 22 import jfun.yan.ComponentMap; 23 import jfun.yan.Dependency; 24 import jfun.yan.Registrar; 25 import jfun.yan.SimpleDependency; 26 import jfun.yan.YanException; 27 import jfun.yan.util.ReflectionUtil; 28 29 44 public class SimpleRegistrar 45 implements Registrar, java.io.Serializable { 46 private final LinkedHashMap creators = new LinkedHashMap (); 47 public synchronized int size(){ 48 return creators.size(); 49 } 50 public synchronized java.util.Set keys(){ 51 return java.util.Collections.unmodifiableSet(creators.keySet()); 52 } 53 public synchronized java.util.Collection getComponents(){ 54 return java.util.Collections.unmodifiableCollection(creators.values()); 55 } 56 public synchronized boolean containsKey(final Object key){ 57 return creators.containsKey(key); 58 } 59 public synchronized Component getComponent(final Object key) { 60 return (Component)creators.get(key); 61 } 62 public synchronized boolean containsType(final Class type){ 63 Component cc = getComponent(type); 64 if(cc!=null){ 65 final Class ctype = cc.getType(); 66 if(ctype == null 67 || ReflectionUtil.isAssignableFrom(type, ctype)){ 68 return true; 70 } 71 cc = null; 72 } 73 for(Iterator it=creators.values().iterator();it.hasNext();){ 74 final Component cci = (Component)it.next(); 75 final Class typei = cci.getType(); 76 if(typei!=null && !void.class.equals(typei) && 77 ReflectionUtil.isAssignableFrom( 80 type, typei)){ 81 return true; 82 } 83 } 84 return false; 85 } 86 public synchronized Component getComponentOfType(final Class type){ 87 Component cc = getComponent(type); 88 if(cc!=null){ 89 final Class ctype = cc.getType(); 90 if(ctype == null 91 || ReflectionUtil.isAssignableFrom(type, ctype)){ 92 return cc; 94 } 95 cc = null; 96 } 97 Class subtype = null; 98 for(Iterator it=creators.values().iterator();it.hasNext();){ 99 final Component cci = (Component)it.next(); 100 final Class typei = cci.getType(); 101 if(typei!=null && !void.class.equals(typei) && 102 ReflectionUtil.isAssignableFrom( 105 type, typei)){ 106 if(subtype!=null){ 107 throw 108 new AmbiguousComponentResolutionException(type, subtype, typei); 109 } 110 else{ 111 subtype = typei; 112 cc = cci; 113 } 114 } 115 } 116 return cc; 117 } 118 public synchronized java.util.List getComponentsOfType(final Class type){ 119 final java.util.ArrayList ret = new java.util.ArrayList (); 120 for(Iterator it=creators.values().iterator();it.hasNext();){ 121 final Component cci = (Component)it.next(); 122 final Class typei = cci.getType(); 123 if(typei!=null && 124 !void.class.equals(typei) && ReflectionUtil.isAssignableFrom( 125 type, typei)){ 126 ret.add(cci); 127 } 128 } 129 return ret; 130 } 131 public synchronized void unregisterComponentsOfType(Class type){ 132 for(Iterator it=creators.values().iterator();it.hasNext();){ 133 final Component cci = (Component)it.next(); 134 final Class typei = cci.getType(); 135 if(typei!=null && ReflectionUtil.isAssignableFrom( 136 type, typei)){ 137 it.remove(); 138 } 139 } 140 } 141 public synchronized void registerComponent(Object key, 142 Component cc) { 143 creators.put(key, cc); 144 } 145 146 public synchronized void unregisterComponent(Object key) { 147 creators.remove(key); 148 } 149 public synchronized void verify(final ComponentMap cmap) { 150 for(Iterator it=creators.keySet().iterator();it.hasNext();){ 151 final Object ki = it.next(); 152 try{ 153 final Component cc = (Component)creators.get(ki); 154 cc.verify(cmap.getDependency(ki, cmap)); 155 } 156 catch(YanException e){ 157 e.push("verify <" + ki +">"); 158 throw e; 159 } 160 } 161 } 162 167 public synchronized Dependency getDependency(final Object ckey 168 ,final ComponentMap cmap){ 169 return new SimpleDependency(ckey, cmap); 170 } 171 176 public synchronized Dependency getDependencyOfType( 177 final Class type, final ComponentMap cmap){ 178 return new SimpleDependency(type, cmap); 179 } 180 public synchronized boolean equals(Object obj) { 181 if(obj instanceof SimpleRegistrar){ 182 final SimpleRegistrar sc2 = (SimpleRegistrar)obj; 183 return creators.equals(sc2.creators); 184 } 185 else return false; 186 } 187 public synchronized int hashCode() { 188 return creators.hashCode(); 189 } 190 public synchronized String toString() { 191 return creators.toString(); 192 } 193 } 194 | Popular Tags |