Skip to content

Commit 5f557b9

Browse files
committed
idworker 和 中文人名生成器
1 parent 7655b1c commit 5f557b9

File tree

7 files changed

+1116
-63
lines changed

7 files changed

+1116
-63
lines changed

luna-commons-common/src/main/java/com/luna/common/domain/BaseEntity.java renamed to luna-commons-common/src/main/java/com/luna/common/entity/BaseEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package com.luna.common.domain;
1+
package com.luna.common.entity;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
24

35
import java.io.Serializable;
46
import java.util.Date;
57
import java.util.HashMap;
68
import java.util.Map;
79

8-
import com.fasterxml.jackson.annotation.JsonFormat;
9-
1010
/**
1111
* Entity基类
1212
*

luna-commons-common/src/main/java/com/luna/common/domain/TreeEntity.java renamed to luna-commons-common/src/main/java/com/luna/common/entity/TreeEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.luna.common.domain;
1+
package com.luna.common.entity;
22

33
/**
44
* Tree基类

luna-commons-common/src/main/java/com/luna/common/domain/Ztree.java renamed to luna-commons-common/src/main/java/com/luna/common/entity/Ztree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.luna.common.domain;
1+
package com.luna.common.entity;
22

33
import java.io.Serializable;
44

luna-commons-common/src/main/java/com/luna/common/utils/BCrypt.java

Lines changed: 808 additions & 0 deletions
Large diffs are not rendered by default.

luna-commons-common/src/main/java/com/luna/common/utils/ImageUtils.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.luna.common.utils.text;
2+
3+
import java.lang.management.ManagementFactory;
4+
import java.net.InetAddress;
5+
import java.net.NetworkInterface;
6+
7+
/**
8+
* <p>名称:IdWorker.java</p>
9+
* <p>描述:分布式自增长ID</p>
10+
* <pre>
11+
* Twitter的 Snowflake JAVA实现方案
12+
* </pre>
13+
* 核心代码为其IdWorker这个类实现,其原理结构如下,我分别用一个0表示一位,用—分割开部分的作用:
14+
* 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
15+
* 在上面的字符串中,第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间,
16+
* 然后5位datacenter标识位,5位机器ID(并不算标识符,实际是为线程标识),
17+
* 然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。
18+
* 这样的好处是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分),
19+
* 并且效率较高,经测试,snowflake每秒能够产生26万ID左右,完全满足需要。
20+
* <p>
21+
* 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加))
22+
*
23+
* @author Polim
24+
*/
25+
public class IdWorker {
26+
// 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
27+
private final static long twepoch = 1288834974657L;
28+
// 机器标识位数
29+
private final static long workerIdBits = 5L;
30+
// 数据中心标识位数
31+
private final static long datacenterIdBits = 5L;
32+
// 机器ID最大值
33+
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
34+
// 数据中心ID最大值
35+
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
36+
// 毫秒内自增位
37+
private final static long sequenceBits = 12L;
38+
// 机器ID偏左移12位
39+
private final static long workerIdShift = sequenceBits;
40+
// 数据中心ID左移17位
41+
private final static long datacenterIdShift = sequenceBits + workerIdBits;
42+
// 时间毫秒左移22位
43+
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
44+
45+
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
46+
/* 上次生产id时间戳 */
47+
private static long lastTimestamp = -1L;
48+
// 0,并发控制
49+
private long sequence = 0L;
50+
51+
private final long workerId;
52+
// 数据标识id部分
53+
private final long datacenterId;
54+
55+
public IdWorker(){
56+
this.datacenterId = getDatacenterId(maxDatacenterId);
57+
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
58+
}
59+
/**
60+
* @param workerId
61+
* 工作机器ID
62+
* @param datacenterId
63+
* 序列号
64+
*/
65+
public IdWorker(long workerId, long datacenterId) {
66+
if (workerId > maxWorkerId || workerId < 0) {
67+
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
68+
}
69+
if (datacenterId > maxDatacenterId || datacenterId < 0) {
70+
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
71+
}
72+
this.workerId = workerId;
73+
this.datacenterId = datacenterId;
74+
}
75+
/**
76+
* 获取下一个ID
77+
*
78+
* @return
79+
*/
80+
public synchronized long nextId() {
81+
long timestamp = timeGen();
82+
if (timestamp < lastTimestamp) {
83+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
84+
}
85+
86+
if (lastTimestamp == timestamp) {
87+
// 当前毫秒内,则+1
88+
sequence = (sequence + 1) & sequenceMask;
89+
if (sequence == 0) {
90+
// 当前毫秒内计数满了,则等待下一秒
91+
timestamp = tilNextMillis(lastTimestamp);
92+
}
93+
} else {
94+
sequence = 0L;
95+
}
96+
lastTimestamp = timestamp;
97+
// ID偏移组合生成最终的ID,并返回ID
98+
long nextId = ((timestamp - twepoch) << timestampLeftShift)
99+
| (datacenterId << datacenterIdShift)
100+
| (workerId << workerIdShift) | sequence;
101+
102+
return nextId;
103+
}
104+
105+
private long tilNextMillis(final long lastTimestamp) {
106+
long timestamp = this.timeGen();
107+
while (timestamp <= lastTimestamp) {
108+
timestamp = this.timeGen();
109+
}
110+
return timestamp;
111+
}
112+
113+
private long timeGen() {
114+
return System.currentTimeMillis();
115+
}
116+
117+
/**
118+
* <p>
119+
* 获取 maxWorkerId
120+
* </p>
121+
*/
122+
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
123+
StringBuffer mpid = new StringBuffer();
124+
mpid.append(datacenterId);
125+
String name = ManagementFactory.getRuntimeMXBean().getName();
126+
if (!name.isEmpty()) {
127+
/*
128+
* GET jvmPid
129+
*/
130+
mpid.append(name.split("@")[0]);
131+
}
132+
/*
133+
* MAC + PID 的 hashcode 获取16个低位
134+
*/
135+
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
136+
}
137+
138+
/**
139+
* <p>
140+
* 数据标识id部分
141+
* </p>
142+
*/
143+
protected static long getDatacenterId(long maxDatacenterId) {
144+
long id = 0L;
145+
try {
146+
InetAddress ip = InetAddress.getLocalHost();
147+
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
148+
if (network == null) {
149+
id = 1L;
150+
} else {
151+
byte[] mac = network.getHardwareAddress();
152+
id = ((0x000000FF & (long) mac[mac.length - 1])
153+
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
154+
id = id % (maxDatacenterId + 1);
155+
}
156+
} catch (Exception e) {
157+
System.out.println(" getDatacenterId: " + e.getMessage());
158+
}
159+
return id;
160+
}
161+
162+
163+
public static void main(String[] args) {
164+
//推特 26万个不重复的ID
165+
//参数1 参数2 是在0 -31 之间
166+
IdWorker idWorker = new IdWorker(0,1);
167+
for (int i = 0; i <100 ; i++) {
168+
System.out.println(idWorker.nextId());//用于生成唯一的ID
169+
}
170+
}
171+
172+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.luna.common.utils.text;
2+
3+
/***
4+
*
5+
* @Author:www.itheima.com
6+
* @Description:itheima
7+
*
8+
****/
9+
public class RandomValueUtil {
10+
public static String base = "abcdefghijklmnopqrstuvwxyz0123456789";
11+
private static String firstName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续";
12+
private static String girl="秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
13+
public static String boy="伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
14+
public static final String[] email_suffix="@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");
15+
16+
public static int getNum(int start,int end) {
17+
return (int)(Math.random()*(end-start+1)+start);
18+
}
19+
20+
/***
21+
*
22+
* Project Name: recruit-helper-util
23+
* <p>随机生成Email
24+
*
25+
* @author youqiang.xiong
26+
* @date 2018年5月23日 下午2:13:06
27+
* @version v1.0
28+
* @since
29+
* @param lMin
30+
* 最小长度
31+
* @param lMax
32+
* 最大长度
33+
* @return
34+
*/
35+
public static String getEmail(int lMin,int lMax) {
36+
int length=getNum(lMin,lMax);
37+
StringBuffer sb = new StringBuffer();
38+
for (int i = 0; i < length; i++) {
39+
int number = (int)(Math.random()*base.length());
40+
sb.append(base.charAt(number));
41+
}
42+
sb.append(email_suffix[(int)(Math.random()*email_suffix.length)]);
43+
return sb.toString();
44+
}
45+
46+
private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
47+
48+
/***
49+
*
50+
* Project Name: recruit-helper-util
51+
* <p>随机生成手机号码
52+
*
53+
* @author youqiang.xiong
54+
* @date 2018年5月23日 下午2:14:17
55+
* @version v1.0
56+
* @since
57+
* @return
58+
*/
59+
public static String getTelephone() {
60+
int index=getNum(0,telFirst.length-1);
61+
String first=telFirst[index];
62+
String second=String.valueOf(getNum(1,888)+10000).substring(1);
63+
String thrid=String.valueOf(getNum(1,9100)+10000).substring(1);
64+
return first+second+thrid;
65+
}
66+
67+
/***
68+
*
69+
* Project Name: recruit-helper-util
70+
* <p>随机生成8位电话号码
71+
*
72+
* @author youqiang.xiong
73+
* @date 2018年5月23日 下午2:15:31
74+
* @version v1.0
75+
* @since
76+
* @return
77+
*/
78+
public static String getLandline() {
79+
int index=getNum(0,telFirst.length-1);
80+
String first=telFirst[index];
81+
String second=String.valueOf(getNum(1,888)+10000).substring(1);
82+
String thrid=String.valueOf(getNum(1,9100)+10000).substring(1);
83+
return first+second+thrid;
84+
}
85+
86+
87+
88+
/**
89+
* 返回中文姓名
90+
*/
91+
public static String name_sex = "";
92+
93+
/***
94+
*
95+
* Project Name: recruit-helper-util
96+
* <p>返回中文姓名
97+
*
98+
* @author youqiang.xiong
99+
* @date 2018年5月23日 下午2:16:16
100+
* @version v1.0
101+
* @since
102+
* @return
103+
*/
104+
public static String getChineseName() {
105+
int index = getNum(0, firstName.length() - 1);
106+
String first = firstName.substring(index, index + 1);
107+
int sex = getNum(0, 1);
108+
String str = boy;
109+
int length = boy.length();
110+
if (sex == 0) {
111+
str = girl;
112+
length = girl.length();
113+
name_sex = "女";
114+
} else {
115+
name_sex = "男";
116+
}
117+
index = getNum(0, length - 1);
118+
String second = str.substring(index, index + 1);
119+
int hasThird = getNum(0, 1);
120+
String third = "";
121+
if (hasThird == 1) {
122+
index = getNum(0, length - 1);
123+
third = str.substring(index, index + 1);
124+
}
125+
return first + second + third;
126+
}
127+
128+
public static void main(String[] args) {
129+
System.out.println(getChineseName());
130+
}
131+
}

0 commit comments

Comments
 (0)