提交 | 用户 | 时间
|
0cceb6
|
1 |
package com.jcdm.common.utils.uuid; |
Y |
2 |
|
|
3 |
import java.util.concurrent.atomic.AtomicInteger; |
|
4 |
import com.jcdm.common.utils.DateUtils; |
|
5 |
import com.jcdm.common.utils.StringUtils; |
|
6 |
|
|
7 |
/** |
|
8 |
* @author jc 序列生成类 |
|
9 |
*/ |
|
10 |
public class Seq |
|
11 |
{ |
|
12 |
// 通用序列类型 |
|
13 |
public static final String commSeqType = "COMMON"; |
|
14 |
|
|
15 |
// 上传序列类型 |
|
16 |
public static final String uploadSeqType = "UPLOAD"; |
|
17 |
|
|
18 |
// 通用接口序列数 |
|
19 |
private static AtomicInteger commSeq = new AtomicInteger(1); |
|
20 |
|
|
21 |
// 上传接口序列数 |
|
22 |
private static AtomicInteger uploadSeq = new AtomicInteger(1); |
|
23 |
|
|
24 |
// 机器标识 |
|
25 |
private static final String machineCode = "A"; |
|
26 |
|
|
27 |
/** |
|
28 |
* 获取通用序列号 |
|
29 |
* |
|
30 |
* @return 序列值 |
|
31 |
*/ |
|
32 |
public static String getId() |
|
33 |
{ |
|
34 |
return getId(commSeqType); |
|
35 |
} |
|
36 |
|
|
37 |
/** |
|
38 |
* 默认16位序列号 yyMMddHHmmss + 一位机器标识 + 3长度循环递增字符串 |
|
39 |
* |
|
40 |
* @return 序列值 |
|
41 |
*/ |
|
42 |
public static String getId(String type) |
|
43 |
{ |
|
44 |
AtomicInteger atomicInt = commSeq; |
|
45 |
if (uploadSeqType.equals(type)) |
|
46 |
{ |
|
47 |
atomicInt = uploadSeq; |
|
48 |
} |
|
49 |
return getId(atomicInt, 3); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* 通用接口序列号 yyMMddHHmmss + 一位机器标识 + length长度循环递增字符串 |
|
54 |
* |
|
55 |
* @param atomicInt 序列数 |
|
56 |
* @param length 数值长度 |
|
57 |
* @return 序列值 |
|
58 |
*/ |
|
59 |
public static String getId(AtomicInteger atomicInt, int length) |
|
60 |
{ |
|
61 |
String result = DateUtils.dateTimeNow(); |
|
62 |
result += machineCode; |
|
63 |
result += getSeq(atomicInt, length); |
|
64 |
return result; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* 序列循环递增字符串[1, 10 的 (length)幂次方), 用0左补齐length位数 |
|
69 |
* |
|
70 |
* @return 序列值 |
|
71 |
*/ |
|
72 |
private synchronized static String getSeq(AtomicInteger atomicInt, int length) |
|
73 |
{ |
|
74 |
// 先取值再+1 |
|
75 |
int value = atomicInt.getAndIncrement(); |
|
76 |
|
|
77 |
// 如果更新后值>=10 的 (length)幂次方则重置为1 |
|
78 |
int maxSeq = (int) Math.pow(10, length); |
|
79 |
if (atomicInt.get() >= maxSeq) |
|
80 |
{ |
|
81 |
atomicInt.set(1); |
|
82 |
} |
|
83 |
// 转字符串,用0左补齐 |
|
84 |
return StringUtils.padl(value, length); |
|
85 |
} |
|
86 |
} |