/**
 * Created on Jul 23, 2005
 * $id:  $
 */
package org.jvnet.lookout.core.model;

import java.io.Serializable;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <code>Identifiable</code> creates an object that contains an embedded unique ID. 
 * Its <code>equals</code> method is properly overwritten to use this embedded ID 
 * for comparison.  
 * 
 * <p>
 * @author Cleo Barretto
 *
 */
public class Identifiable implements Serializable {

    private static final ReentrantLock classLock = new ReentrantLock();
    private static int idCounter = 0;
    private String name = "";
    
    private int id;
    
    public Identifiable(String name) {
    	
    	this.name = name;
    	
        classLock.lock();
        
        id = idCounter++;
        
        classLock.unlock();        
    }
    
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Identifiable) {
            return (((Identifiable)obj).getId() == id) 
            && (obj.getClass().equals(this.getClass()));
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return getId();
    }    
    
    public int getId() {
        return id;
    }    
    
    @Override
    public String toString() {
        return this.getClass().getName() + " [ID=" + id + "][NAME=" + name +"]";
    }
    
    public String getName() {
		return name;
	}
}
