package com.railway.common.qrcode.factory;


import com.railway.common.qrcode.impl.DefaultSnowflakeGenerator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * workerId抽象工厂
 *
 * @author ZhaoMn
 */
public abstract class AbstractWorkerIdGeneratorFactory {

  private static ConcurrentHashMap<Integer, DefaultSnowflakeGenerator> snowflakeMap = new ConcurrentHashMap<>();

  public DefaultSnowflakeGenerator getSnowflakeGenerator(Integer workerId) {
    if (snowflakeMap.containsKey(workerId)) {
      return snowflakeMap.get(workerId);
    }
    synchronized (this) {
      if (snowflakeMap.containsKey(workerId)) {
        return snowflakeMap.get(workerId);
      }
      DefaultSnowflakeGenerator generator = new DefaultSnowflakeGenerator(workerId);
      snowflakeMap.put(workerId, generator);
      return generator;
    }
  }


}
