package com.libreecm.commonAPI.auxiliars;

import java.util.concurrent.atomic.AtomicLong;
/**
 * Class that generates a unique ID for an object.
 * 
 * It's implemented by an Singleton pattern with concurrency checking
 * 
 * It has no set property, so it have to be instanced
 * @author Jordi Pérez Bayerri
 *
 */
public final class IdGenerator {
	private final static AtomicLong counter=new AtomicLong();
	
	private static volatile IdGenerator instance;
	
	private IdGenerator() {}
	private Integer id;
	
	public static IdGenerator getInstance() {
        if (instance == null ) {
            synchronized (IdGenerator.class) {
                if (instance == null) {
                    instance = new IdGenerator();
            		}
                }
            	//At this section, we have a sure generated instance, and we are an non concurrent section
            	instance.id=(int) ((counter.incrementAndGet()));
            }
        return instance;
    }

	
	public Integer getId(){
		
		return id;
	}
		
}
