package org.geotools.util.lookup;

/**
 * Quick implementation of ServiceEntry to be used 
 * @author jody
 *
 * @param <T>
 */
public class ServiceEntryImpl<T> extends Lookup.Entry<T> {

    String id;

    private T instance;

    ServiceEntryImpl(T obj) {
        this(obj, null);
    }

    ServiceEntryImpl(T instance, String id) {
        this.instance = instance;
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public T getInstance() {
        return instance;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public Class<T> getType() {
        return (Class<T>) instance.getClass();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((instance == null) ? 0 : instance.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        @SuppressWarnings("rawtypes")
        ServiceEntryImpl other = (ServiceEntryImpl) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (instance == null) {
            if (other.instance != null)
                return false;
        } else if (!instance.equals(other.instance))
            return false;
        return true;
    }

}
