1 6 package org.jfox.ioc; 7 8 import org.jfox.ioc.exception.ComponentException; 9 10 11 22 23 public class ComponentName { 24 28 private Class type = null; 29 30 33 private String factor = null; 34 35 36 41 private ComponentName(Class clz, String factory) { 42 this.type = clz; 43 if(factory == null || factory.trim().equals("")) { 44 this.factor = "" + System.identityHashCode(clz); 45 } 46 else { 47 this.factor = factory; 48 } 49 } 50 56 public static ComponentName newInstance(Class type) { 57 return newInstance(type, null); 58 } 59 60 66 public static ComponentName newInstance(Class type, String factor) { 67 return new ComponentName(type, factor); 68 } 69 70 80 public static ComponentName parseString(String compName) throws ComponentException { 81 return parseString(compName, Thread.currentThread().getContextClassLoader()); 82 } 83 84 91 public static ComponentName parseString(String compName, ClassLoader cl) throws ComponentException { 92 String className = compName; 93 int index = compName.indexOf("@"); 94 if(index > 0) { 95 className = compName.substring(0, index); 96 String factor = compName.substring(index + 1); 97 try { 98 Class clz = cl.loadClass(className); 99 return newInstance(clz, factor); 100 } 101 catch(ClassNotFoundException e) { 102 throw new ComponentException(e); 103 } 104 } 105 else { 106 try { 107 Class clz = cl.loadClass(className); 108 return newInstance(clz); 109 } 110 catch(ClassNotFoundException e) { 111 throw new ComponentException(e); 112 } 113 } 114 115 } 116 117 121 public Class getType() { 122 return type; 123 } 124 125 129 public String getFactor() { 130 return factor; 131 } 132 133 public String toString() { 134 return type.getName() + "@" + factor; 135 } 136 137 public int hashCode() { 138 return type.hashCode() + factor.hashCode(); 139 } 140 141 146 public boolean equals(Object obj) { 147 if(!(obj instanceof ComponentName)) { 148 return false; 149 } 150 else { 151 ComponentName _name = (ComponentName) obj; 152 return type.equals(_name.type) && factor.equals(_name.factor); 153 } 154 } 155 156 public static void main(String [] args) { 157 158 } 159 } 160 161 | Popular Tags |