All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.idgenerator.uid.DefaultIds Maven / Gradle / Ivy

The newest version!
package com.taotao.boot.idgenerator.uid;

import com.taotao.boot.common.exception.BusinessException;
import com.taotao.boot.common.support.oid.Oid;
import com.taotao.boot.core.enums.TtcEnvEnum;
import com.taotao.boot.idgenerator.uid.config.UidGenProperties;
import com.taotao.boot.idgenerator.uid.exception.UidGenerateException;
import org.dromara.hutool.core.text.StrUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class DefaultIds implements Ids {

	private static final int SEQ_LEN = 16;
	private static final int OID_LEN = 46;
	private static final int RID_LEN = 30;
	private static final int EXTENSION_CODE_LEN = 3;
	private static final int SHARD_CODE_LEN = 2;
	private static final int SERVICE_CODE_LEN = 4;
	private static final int BUSINESS_CODE_LEN = 12;
	private static final String EMPTY_SERVICE_CODE = "0000";
	private static final String EMPTY_SHARD_CODE = "00";
	private static final String EMPTY_EXTENSION_CODE = "000";

	private UidGenerator uidGenerator;
	private UidGenProperties uidGenProperties;

	public void setUidGenerator(UidGenerator uidGenerator) {
		this.uidGenerator = uidGenerator;
	}

	public void setUidGenProperties(UidGenProperties uidGenProperties) {
		this.uidGenProperties = uidGenProperties;
	}


	@Override
	public Oid getOid(String businessCode) {
		return getOid(businessCode, EMPTY_SERVICE_CODE, EMPTY_SHARD_CODE, EMPTY_EXTENSION_CODE);
	}

	@Override
	public Oid getOid(String businessCode, String serviceCode) {
		return getOid(businessCode, serviceCode, EMPTY_SHARD_CODE, EMPTY_EXTENSION_CODE);
	}

	@Override
	public Oid getOid(String businessCode, String serviceCode, String extensionCode) {
		return getOid(businessCode, serviceCode, EMPTY_SHARD_CODE, extensionCode);
	}

	@Override
	public Oid getOid(String businessCode, String serviceCode, String shardCode,
					  String extensionCode) {
		assertLen(businessCode, BUSINESS_CODE_LEN, "businessCode长度必须是12位");
		assertLen(serviceCode, SERVICE_CODE_LEN, "businessCode长度必须是4位");
		String id = getId(shardCode, extensionCode);
		StringBuilder sb = new StringBuilder(OID_LEN);
		sb.append(businessCode)
			.append(serviceCode)
			.append(id);

		return Oid.NEW.from(sb.toString().toUpperCase());
	}

	@Override
	public String getId() {
		return getId(EMPTY_SHARD_CODE, EMPTY_EXTENSION_CODE);
	}

	@Override
	public String getId(String extensionCode) {
		return getId(EMPTY_SHARD_CODE, extensionCode);
	}

	@Override
	public String getId(String shardCode, String extensionCode) {
		assertLen(shardCode, SHARD_CODE_LEN, "shardCode长度必须是2位");
		assertLen(extensionCode, EXTENSION_CODE_LEN, "extensionCode长度必须是3位");
		StringBuilder sb = new StringBuilder(RID_LEN);
		sb.append(uidGenProperties.getSystemCode())
			.append(TtcEnvEnum.getCurrEnv().getCode())
			.append(uidGenProperties.getDataCenterCode())
			.append(shardCode)
			.append(extensionCode);
		try {
			sb.append(longToHex(uidGenerator.getUID()));
		} catch (Exception e) {
			throw new UidGenerateException(e);
		}
		return sb.toString().toUpperCase();

	}

	@Override
	public Map parseId(String id) {
		if (id != null && id.length() == RID_LEN) {
			Map map = new HashMap<>(6);
			map.put("systemCode", UidParser.parse(id, UidParser.ID_SYSTEM_CODE));
			map.put("envCode", UidParser.parse(id, UidParser.ID_ENV_CODE));
			map.put("dataCenterCode", UidParser.parse(id, UidParser.ID_DATA_CENTER_CODE));
			map.put("shardCode", UidParser.parse(id, UidParser.ID_SHARD_CODE));
			map.put("extensionCode", UidParser.parse(id, UidParser.ID_EXTENSION_CODE));
			map.putAll(uidGenerator.parseUid(
				Long.parseLong(UidParser.parse(id, UidParser.ID_SEQ_CODE), 16)));
			return Collections.unmodifiableMap(map);
		}
		throw new BusinessException("id 长度必须是30位");
	}

	@Override
	public Map parseOid(Oid oid) {
		if (oid == null) {
			throw new BusinessException("oid 対象不能空");
		}
		return parseOid(oid.value().get());
	}

	@Override
	public Map parseOid(String oid) {
		if (oid != null && oid.length() == OID_LEN) {
			Map map = new HashMap<>(8);
			map.put("businessCode", UidParser.parse(oid, UidParser.OID_BUSINESS_CODE));
			map.put("serviceCode", UidParser.parse(oid, UidParser.OID_SERVICE_CODE));
			map.put("systemCode", UidParser.parse(oid, UidParser.OID_SYSTEM_CODE));
			map.put("envCode", UidParser.parse(oid, UidParser.OID_ENV_CODE));
			map.put("dataCenterCode", UidParser.parse(oid, UidParser.OID_DATA_CENTER_CODE));
			map.put("shardCode", UidParser.parse(oid, UidParser.OID_SHARD_CODE));
			map.put("extensionCode", UidParser.parse(oid, UidParser.OID_EXTENSION_CODE));
			map.putAll(uidGenerator.parseUid(Long.parseLong(UidParser.parse(oid, UidParser.OID_SEQ_CODE), 16)));
			return Collections.unmodifiableMap(map);
		}
		throw new BusinessException("id 长度必须是46位");
	}

	@Override
	public String parseSystemCode(String uid) {
		if (uid != null && uid.length() == RID_LEN) {
			return UidParser.parse(uid, UidParser.ID_SYSTEM_CODE);
		} else if (uid != null && uid.length() == OID_LEN) {
			return UidParser.parse(uid, UidParser.OID_SYSTEM_CODE);
		}
		throw new BusinessException("id 长度必须是30位或者46位");
	}

	@Override
	public String parseDataCenterCode(String uid) {
		if (uid != null && uid.length() == RID_LEN) {
			return UidParser.parse(uid, UidParser.ID_DATA_CENTER_CODE);
		} else if (uid != null && uid.length() == OID_LEN) {
			return UidParser.parse(uid, UidParser.ID_DATA_CENTER_CODE);
		}
		throw new BusinessException("id 长度必须是30位或者46位");
	}

	@Override
	public String parseShardCode(String uid) {
		if (uid != null && uid.length() == RID_LEN) {
			return UidParser.parse(uid, UidParser.ID_SHARD_CODE);
		} else if (uid != null && uid.length() == OID_LEN) {
			return UidParser.parse(uid, UidParser.ID_SHARD_CODE);
		}
		throw new BusinessException("id 长度必须是30位或者46位");
	}

	@Override
	public Long parseSequenceCode(String uid) {
		if (uid != null && uid.length() == RID_LEN) {
			return Long.parseLong(UidParser.parse(uid, UidParser.ID_SEQ_CODE), 16);
		} else if (uid != null && uid.length() == OID_LEN) {
			return Long.parseLong(UidParser.parse(uid, UidParser.OID_SEQ_CODE), 16);
		}
		throw new BusinessException("id 长度必须是30位或者46位");
	}

	@Override
	public String parseExtensionCode(String uid) {
		if (uid != null && uid.length() == RID_LEN) {
			return UidParser.parse(uid, UidParser.ID_EXTENSION_CODE);
		} else if (uid != null && uid.length() == OID_LEN) {
			return UidParser.parse(uid, UidParser.OID_EXTENSION_CODE);
		}
		throw new BusinessException("id 长度必须是30位或者46位");
	}

	@Override
	public String parseBusinessCode(String oid) {
		if (oid != null && oid.length() == OID_LEN) {
			return UidParser.parse(oid, UidParser.OID_BUSINESS_CODE);
		}
		throw new BusinessException("id 长度必须46位");
	}

	@Override
	public String parseServiceCode(String oid) {
		if (oid != null && oid.length() == OID_LEN) {
			return UidParser.parse(oid, UidParser.OID_SERVICE_CODE);
		}
		throw new BusinessException("id 长度必须46位");
	}


	@Override
	public String parseChannelCode(String oid) {
		if (oid != null && oid.length() == OID_LEN) {
			return UidParser.parse(oid, UidParser.OID_CHANNEL_CODE);
		}
		throw new BusinessException("id 长度必须46位");
	}

	@Override
	public String parseProductCode(String oid) {
		if (oid != null && oid.length() == OID_LEN) {
			return UidParser.parse(oid, UidParser.OID_PRODUCT_CODE);
		}
		throw new BusinessException("id 长度必须46位");
	}

	private void assertLen(String str, int permitLen, String msg) {
		if (StrUtil.isBlank(str) || str.length() != permitLen) {
			throw new BusinessException(msg);
		}
	}

	private String longToHex(long l) {
		String hexString = Long.toHexString(l);
		StringBuilder sb = new StringBuilder(SEQ_LEN);
		int m = SEQ_LEN - hexString.length();
		if (m > 0) {
			for (; m > 0; m--) {
				sb.append("0");
			}
			sb.append(hexString);
			return sb.toString();
		}
		return hexString;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy