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

com.devskiller.friendly_id.Url62 Maven / Gradle / Ivy

package com.devskiller.friendly_id;

import java.math.BigInteger;
import java.util.UUID;

/**
 * Class to convert UUID to Url62 IDs
 */
public class Url62 {

	/**
	 * Create url62 id
	 *
	 * @return url62 encoded id
	 */
	public static String create() {
		return encode(UUID.randomUUID());
	}

	/**
	 * Encode UUID to Url62 id
	 *
	 * @param uuid UUID to be encoded
	 * @return url62 encoded UUID
	 */
	public static String encode(UUID uuid) {
		BigInteger pair = ElegantPairing.pair(
				BigInteger.valueOf(uuid.getMostSignificantBits()),
				BigInteger.valueOf(uuid.getLeastSignificantBits())
		);
		return Base62.encode(pair);
	}

	/**
	 * Decode url62 id to UUID
	 *
	 * @param id url62 encoded id
	 * @return decoded UUID
	 */
	public static UUID decode(String id) {
		BigInteger decoded = Base62.decode(id);
		BigInteger[] unpaired = ElegantPairing.unpair(decoded);
		return new UUID(unpaired[0].longValue(), unpaired[1].longValue());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy