UUID经常被用在产生全局唯一性ID/主键的场合,在java/kotlin中生成它们也很方便。
UUID字符串编码为16进制方便解析但编码长度比较长,所以不便于阅读和传输。
所以在UUID基础上,使用更高密度的进制(62进制)写了个紧凑版本的UUID(tight UUID)。
工具类代码如下 :
package wxm.androidutil.tightUUID
import java.util.*
import kotlin.collections.HashMap
/**
* @author WangXM
* @version create:2018/6/9
*/
object tightUUID {
private const val NEW_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
private const val NEW_CHAR_LEN = NEW_CHAR.length
private val NEW_POW_ARR = Array(12,
{ Math.pow(NEW_CHAR_LEN.toDouble(), it.toDouble()).toLong() })
private val IDX_MAP = HashMap<Char, Int>().apply {
for(i in 0 until NEW_CHAR_LEN) {
put(NEW_CHAR[i], i)
}
}
private const val DELIMITER_CHAR = '-'
/**
* get tightUUID can revert to UUID
* example : '0f04803e-502a-4f90-8012-db98b6efccb0' -> '0h3axw-5l0-5iw-8wO-16yQl101G'
*/
fun getTUUID(): String {
return toTUUID(UUID.randomUUID().toString())
}
/**
* get fined tightUUID
* fined tightUUID with smaller length but can not revert to UUID
* example : '0f04803e-502a-4f90-8012-db98b6efccb0' -> 'h3axw5l05iw8wO16yQl101G'
*/
fun getFineTUUID(): String {
val sb = StringBuilder()
UUID.randomUUID().toString().forEach {
if (DELIMITER_CHAR != it) {
sb.append(it)
}
}
return toTUUID(sb.toString())
}
/**
* translate UUID [org] to TUUID
* when [fined] false
* '0f04803e-502a-4f90-8012-db98b6efccb0' -> '0h3axw-5l0-5iw-8wO-16yQl101G'
* when [fined] true
* '0f04803e-502a-4f90-8012-db98b6efccb0' -> 'h3axw5l05iw8wO16yQl101G'
*/
fun toTUUID(org: String, fined: Boolean = false): String {
val ret = StringBuilder()
org.split(DELIMITER_CHAR).let {
it.forEach {
val sz = dataToNewStr(it.toLong(16))
ret.append(if (fined) {
sz
} else {
val tag = getZeroTag(it)
(if (tag.isNotEmpty()) "$tag$sz" else sz) + DELIMITER_CHAR
})
}
}
return if (fined) ret.toString() else ret.removeSuffix(DELIMITER_CHAR.toString()).toString()
}
/**
* translate TUUID [org] to UUID
* [org] must get from [toTUUID] with 'fined' is false
*/
fun toUUID(org: String): String {
val ret = StringBuilder()
org.split(DELIMITER_CHAR).let {
it.forEach {
strToData(it).toString(16).let { sz ->
getZeroTag(it).let { tag ->
(if (tag.isNotEmpty()) "$tag$sz" else sz) + DELIMITER_CHAR
}.let {
ret.append(it)
}
}
}
}
return ret.removeSuffix(DELIMITER_CHAR.toString()).toString()
}
/**
* use [NEW_CHAR] get str from [data]
*/
private fun dataToNewStr(data: Long): String {
val ret = StringBuilder()
val toSZ = {idx:Int ->
ret.insert(0, NEW_CHAR[idx])
}
var vd = data
while (vd >= NEW_CHAR_LEN) {
toSZ((vd % NEW_CHAR_LEN).toInt())
vd /= NEW_CHAR_LEN
}
if (vd.toInt() != 0) {
toSZ((vd % NEW_CHAR_LEN).toInt())
} else {
if(ret.isEmpty()) {
toSZ((vd % NEW_CHAR_LEN).toInt())
}
}
return ret.toString()
}
/**
* translate str from [NEW_CHAR] to Long
*/
private fun strToData(sz: String): Long {
var ret = 0L
val maxPos = sz.length - 1
for (i in maxPos downTo 0) {
ret += IDX_MAP[sz[i]]!! * NEW_POW_ARR[maxPos - i]
}
return ret
}
/**
* get '0' tag in HEX str
*/
private fun getZeroTag(org: String): String {
val tag = StringBuilder()
// 避免'0000'字符串情况,不检查org最后一位
for (i in 0 until org.length - 1) {
if ('0' == org[i]) {
tag.append("0")
} else {
return tag.toString()
}
}
return tag.toString()
}
}
单元测试如下 :
package com.wxm.KeepAccountJavaTest
import junit.framework.Assert
import org.junit.Test
import wxm.androidutil.tightUUID.tightUUID
import java.util.*
/**
* @author WangXM
* @version create:2018/5/23
*/
class TightUUIDUT {
@Test
fun testTightUUID() {
for(i in 0 until 5000) {
val org = UUID.randomUUID().toString()
val trans = tightUUID.toTUUID(org)
val orgTrans = tightUUID.toUUID(trans)
System.out.println(org)
System.out.println(trans)
System.out.println(tightUUID.toTUUID(org, true))
System.out.println("------------------------")
Assert.assertEquals(org, orgTrans)
Assert.assertTrue(org.length > trans.length)
}
}
}
单元测试输出 :
f0b1a2ec-5966-4b43-b3e6-cb147585062d
4phLnm-5X8-50L-bYO-11p7E5L53
4phLnm5X850LbYO11p7E5L53
------------------------
67463b16-b599-4ccb-93e3-43bad7b576d7
1Tg2WO-c5P-575-9QD-l954U1pB
1Tg2WOc5P5759QDl954U1pB
------------------------
94ff3199-07b5-43cc-b464-0efc4de413c3
2JaHMd-0vP-4vW-c0Q-04G5ajgYj
2JaHMdvP4vWc0Q4G5ajgYj
------------------------
.............(省略)...............