/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.framework;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ComponentName {
    /**
     * always be the Component implementation class name
     */
    private Class implementation = null;

    /**
     * number to differentiate components with same implementation
     */
    private String factor = null;


    private ComponentName(Class name, String seed) {
        this.implementation = name;
        if(seed == null || seed.trim().equals("")) {
            this.factor = "" + System.identityHashCode(this);
        }
        else {
            this.factor = seed;
        }
    }

    public static ComponentName newInstance(Class implClass) {
        return newInstance(implClass, null);
    }

    public static ComponentName newInstance(Class implClass, String factor) {
        return new ComponentName(implClass, factor);
    }

    /**
     * use current Thread context class loader
     *
     * @param compName
     * @return
     * @throws ComponentException
     */
    public static ComponentName parseString(String compName) throws ComponentException {
        return parseString(compName, Thread.currentThread().getContextClassLoader());
    }

    /**
     * construct a ComponentName2 from String like name@domain
     *
     * @param compName
     * @return
     */
    public static ComponentName parseString(String compName, ClassLoader cl) throws ComponentException {
        String className = compName;
        int index = compName.indexOf("@");
        if(index > 0) {
            className = compName.substring(0, index);
            String factor = compName.substring(index + 1);
            try {
                Class clz = cl.loadClass(className);
                return newInstance(clz, factor);
            }
            catch(ClassNotFoundException e) {
                throw new ComponentException(e);
            }
        }
        else {
            try {
                Class clz = cl.loadClass(className);
                return new ComponentName(clz, null);
            }
            catch(ClassNotFoundException e) {
                throw new ComponentException(e);
            }
        }

    }

    public Class getImplementation() {
        return implementation;
    }

    public String getFactor() {
        return factor;
    }

    public String toString() {
        return implementation.getName() + "@" + factor;
    }

    public int hashCode() {
        return implementation.hashCode() + factor.hashCode();
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof ComponentName)) {
            return false;
        }
        else {
            ComponentName _name = (ComponentName) obj;
            return implementation.equals(_name.implementation) && factor.equals(_name.factor);
        }
    }

    public static void main(String[] args) {

    }
}

