1 8 14 package jfun.yan.containers; 15 16 import java.util.ArrayList ; 17 import java.util.LinkedHashSet ; 18 19 import jfun.yan.Component; 20 import jfun.yan.ComponentMap; 21 import jfun.yan.Dependency; 22 import jfun.yan.Registrar; 23 24 37 public class InheritedRegistrar implements Registrar { 38 private final Registrar parent; 39 private final Registrar child; 40 41 45 public Registrar getChild() { 46 return child; 47 } 48 52 public Registrar getParent() { 53 return parent; 54 } 55 61 public InheritedRegistrar(final Registrar parent, final Registrar child) { 62 this.parent = parent; 63 this.child = child; 64 } 65 66 69 public void registerComponent(Object key, Component cc) { 70 child.registerComponent(key, cc); 71 } 72 75 public void unregisterComponentsOfType(Class type){ 76 child.unregisterComponentsOfType(type); 77 } 78 83 public void unregisterComponent(Object key) { 84 child.unregisterComponent(key); 85 } 87 public boolean containsKey(Object key){ 88 return child.containsKey(key) || parent.containsKey(key); 89 } 90 public boolean containsType(Class type){ 91 return child.containsType(type) || parent.containsType(type); 92 } 93 98 public Component getComponent(Object key) { 99 Component cc = child.getComponent(key); 100 if(cc==null) return parent.getComponent(key); 101 else return cc; 102 } 103 108 public Component getComponentOfType(Class type){ 109 Component c1 = getComponent(type); 110 if(c1!=null) return c1; 111 c1 = child.getComponentOfType(type); 112 if(c1!=null) return c1; 113 else return parent.getComponentOfType(type); 114 } 115 122 public java.util.List getComponentsOfType(Class type){ 123 final java.util.List ret = new ArrayList (parent.getComponentsOfType(type)); 124 ret.addAll(child.getComponentsOfType(type)); 125 return ret; 126 } 127 128 135 public java.util.Collection getComponents(){ 136 final java.util.List ret = new ArrayList (parent.getComponents()); 137 ret.addAll(child.getComponents()); 138 return ret; 139 } 140 149 public Dependency getDependencyOfType(Class type, ComponentMap cmap){ 150 if(type==null || child.containsType(type)) 151 return child.getDependencyOfType(type, cmap); 152 else return parent.getDependencyOfType(type, parent); 153 } 154 162 public Dependency getDependency(Object key, ComponentMap cmap){ 163 if(key==null || child.containsKey(key)) 165 return child.getDependency(key, cmap); 166 else 167 return parent.getDependency(key, parent); 168 210 } 211 217 public void verify(ComponentMap cmap) { 218 child.verify(cmap); 219 parent.verify(parent); 220 } 221 222 223 232 237 public java.util.Set keys(){ 238 final LinkedHashSet hset = new LinkedHashSet (parent.keys()); 239 hset.addAll(child.keys()); 240 return hset; 241 } 242 243 public boolean equals(Object obj) { 244 if(obj instanceof InheritedRegistrar){ 245 final InheritedRegistrar ic2 = (InheritedRegistrar)obj; 246 return parent.equals(ic2.parent) && child.equals(ic2.child); 247 } 248 else return false; 249 } 250 public int hashCode() { 251 return parent.hashCode()*31 + child.hashCode(); 252 } 253 public String toString() { 254 return child.toString()+":"+parent; 255 } 256 } 257 | Popular Tags |