/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;

import java.util.stream.IntStream;

/**
 * <pre>
 *     url安全的id生成，比uuid短，无任何符号，默认24位
 *     适合做一些tokenId或者其他唯一id
 * </pre>
 *
 * @author 田平 create 2022/2/25 10:02
 */
public class NanoIdUtil {

	public static final char[] ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	public static final char[] LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz".toCharArray();
	public static final char[] UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	public static final int ID_LENGTH = 24;

	public static String randomNanoId() {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, ALPHABET, ID_LENGTH);
	}

	public static String randomLowerNanoId() {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, LOWER_ALPHABET, ID_LENGTH);
	}

	public static String randomLowerNanoId(int idLength) {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, LOWER_ALPHABET, idLength);
	}

	public static String randomUpperNanoId() {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, UPPER_ALPHABET, ID_LENGTH);
	}

	public static String randomUpperNanoId(int idLength) {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, UPPER_ALPHABET, idLength);
	}

	public static String randomNanoId(int idLength) {
		return NanoIdUtils.randomNanoId(NanoIdUtils.DEFAULT_NUMBER_GENERATOR, ALPHABET, idLength);
	}

	public static void main(String[] args) {
		IntStream.range(0, 1).forEach(i -> {
			System.out.println(i + ":" + randomLowerNanoId());
		});
	}
}
