Skip to content

Commit

Permalink
Merge pull request #68 from yindz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yindz authored Jan 26, 2023
2 parents 3e553b7 + a465946 commit 176ca9a
Show file tree
Hide file tree
Showing 7 changed files with 593 additions and 181 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.apifan.common</groupId>
<artifactId>common-random</artifactId>
<version>1.0.18</version>
<version>1.0.19</version>
<packaging>jar</packaging>
<name>common-random</name>
<description>An easy-to-use random data generator.</description>
Expand All @@ -23,7 +23,7 @@
<commons-collections.version>4.4</commons-collections.version>
<guava.version>31.1-jre</guava.version>
<jackson-databind.version>2.13.4.1</jackson-databind.version>
<gson.version>2.9.0</gson.version>
<gson.version>2.9.1</gson.version>
<fastjson.version>1.2.83</fastjson.version>
<tinypinyin.version>2.0.3.RELEASE</tinypinyin.version>
<pinyin4j.version>2.5.1</pinyin4j.version>
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/com/apifan/common/random/RandomSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.apifan.common.random;

import com.apifan.common.random.source.*;

/**
* 统一入口类
*
* @author yin
* @since 1.0.19
*/
public class RandomSource {

/**
* 获取地区数据源
*
* @return 地区数据源
*/
public static AreaSource areaSource() {
return AreaSource.getInstance();
}

/**
* 获取日期时间数据源
*
* @return 日期时间数据源
*/
public static DateTimeSource dateTimeSource() {
return DateTimeSource.getInstance();
}

/**
* 获取教育信息数据源
*
* @return 教育信息数据源
*/
public static EducationSource educationSource() {
return EducationSource.getInstance();
}

/**
* 获取金融相关数据源
*
* @return 金融相关数据源
*/
public static FinancialSource financialSource() {
return FinancialSource.getInstance();
}

/**
* 获取互联网信息数据源
*
* @return 互联网信息数据源
*/
public static InternetSource internetSource() {
return InternetSource.getInstance();
}

/**
* 获取数值数据源
*
* @return 数值数据源
*/
public static NumberSource numberSource() {
return NumberSource.getInstance();
}

/**
* 获取个人信息数据源
*
* @return 个人信息数据源
*/
public static PersonInfoSource personInfoSource() {
return PersonInfoSource.getInstance();
}

/**
* 获取体育竞技数据源
*
* @return 体育竞技数据源
*/
public static SportSource sportSource() {
return SportSource.getInstance();
}

/**
* 获取语言文字数据源
*
* @return 语言文字数据源
*/
public static LanguageSource languageSource() {
return LanguageSource.getInstance();
}

/**
* 获取其它杂项数据源
*
* @return 其它杂项数据源
*/
public static OtherSource otherSource() {
return OtherSource.getInstance();
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/apifan/common/random/constant/Province.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.apifan.common.random.constant;

/**
* 省/直辖市/自治区/特别行政区名称枚举
*
* @author yin
* @since 1.0.19
*/
public enum Province {

BJ("北京市", "京"),
SH("上海市", "沪"),
TJ("天津市", "津"),
CQ("重庆市", "渝"),
HE("河北省", "冀"),
SX("山西省", "晋"),
NM("内蒙古自治区", "蒙"),
LN("辽宁省", "辽"),
JL("吉林省", "吉"),
HL("黑龙江省", "黑"),
JS("江苏省", "苏"),
ZJ("浙江省", "浙"),
AH("安徽省", "皖"),
FJ("福建省", "闽"),
JX("江西省", "赣"),
SD("山东省", "鲁"),
HA("河南省", "豫"),
HB("湖北省", "鄂"),
HN("湖南省", "湘"),
GD("广东省", "粤"),
GX("广西壮族自治区", "桂"),
HI("海南省", "琼"),
SC("四川省", "川"),
GZ("贵州省", "贵"),
YN("云南省", "云"),
XZ("西藏自治区", "藏"),
SN("陕西省", "陕"),
GS("甘肃省", "甘"),
QH("青海省", "青"),
NX("宁夏回族自治区", "宁"),
XJ("新疆维吾尔自治区", "新"),
TW("台湾省", "台"),
HK("香港特别行政区", "港"),
MO("澳门特别行政区", "澳");

private String name;
private String prefix;

Province(String n, String p) {
this.name = n;
this.prefix = p;
}

public String getName() {
return this.name;
}

public String getPrefix() {
return this.prefix;
}
}
69 changes: 48 additions & 21 deletions src/main/java/com/apifan/common/random/source/AreaSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apifan.common.random.source;

import com.apifan.common.random.constant.Province;
import com.apifan.common.random.entity.Area;
import com.apifan.common.random.entity.CountryOrRegionCode;
import com.apifan.common.random.util.ResourceUtils;
Expand Down Expand Up @@ -171,28 +172,21 @@ public String randomZipCode() {
* @return 随机的详细地址
*/
public String randomAddress() {
Area area = nextArea();
String prefix = area.getProvince() + area.getCity() + Objects.toString(area.getCounty(), "");
if (prefix.endsWith("县") || prefix.endsWith("旗")) {
//乡村地址
String town = ResourceUtils.getRandomString(addressWordList, 2) + ResourceUtils.getRandomElement(townSuffixList);
String village = ResourceUtils.getRandomString(addressWordList, 2) + "村";
String group = ResourceUtils.getRandomString(addressWordList, 2) + "组";
return prefix + town + village + group + RandomUtils.nextInt(1, 100) + "号";
} else {
//城镇地址
String road = ResourceUtils.getRandomString(addressWordList, 2) + ResourceUtils.getRandomElement(directionList);
String community = ResourceUtils.getRandomElement(communityNameList) + ResourceUtils.getRandomElement(communitySuffixList);
String extra = "";
int x = NumberSource.getInstance().randomInt(0, 11);
if (x % 3 == 0) {
extra = ResourceUtils.getRandomElement(directionList);
}
String building = RandomUtils.nextInt(1, 20) + "栋";
String unit = RandomUtils.nextInt(1, 5) + "单元";
String room = String.format("%02d", RandomUtils.nextInt(1, 31)) + String.format("%02d", RandomUtils.nextInt(1, 5)) + "房";
return prefix + road + "路" + RandomUtils.nextInt(1, 1000) + "号" + community + extra + building + unit + room;
return randomAddress(nextArea());
}

/**
* 获取随机的详细地址(指定省级行政区)
*
* @param province 省级行政区枚举
* @return 省级行政区范围内的随机详细地址
*/
public String randomAddress(Province province) {
if (province == null) {
return null;
}
Area area = ResourceUtils.getRandomElement(areaList.stream().filter(i -> province.getName().equals(i.getProvince())).collect(Collectors.toList()));
return randomAddress(area);
}

/**
Expand Down Expand Up @@ -281,4 +275,37 @@ public CountryOrRegionCode randomCountryOrRegionCode() {
private CountryOrRegionCode randomCountryOrRegionCode(List<CountryOrRegionCode> list) {
return CollectionUtils.isNotEmpty(list) ? list.get(RandomUtils.nextInt(0, list.size() - 1)) : null;
}

/**
* 根据指定的区域信息生成随机的详细地址
*
* @param area 区域信息
* @return 随机的详细地址
*/
private String randomAddress(Area area) {
if (area == null) {
return null;
}
String prefix = area.getProvince() + area.getCity() + Objects.toString(area.getCounty(), "");
if (prefix.endsWith("县") || prefix.endsWith("旗")) {
//乡村地址
String town = ResourceUtils.getRandomString(addressWordList, 2) + ResourceUtils.getRandomElement(townSuffixList);
String village = ResourceUtils.getRandomString(addressWordList, 2) + "村";
String group = ResourceUtils.getRandomString(addressWordList, 2) + "组";
return prefix + town + village + group + RandomUtils.nextInt(1, 100) + "号";
} else {
//城镇地址
String road = ResourceUtils.getRandomString(addressWordList, 2) + ResourceUtils.getRandomElement(directionList);
String community = ResourceUtils.getRandomElement(communityNameList) + ResourceUtils.getRandomElement(communitySuffixList);
String extra = "";
int x = NumberSource.getInstance().randomInt(0, 11);
if (x % 3 == 0) {
extra = ResourceUtils.getRandomElement(directionList);
}
String building = RandomUtils.nextInt(1, 20) + "栋";
String unit = RandomUtils.nextInt(1, 5) + "单元";
String room = String.format("%02d", RandomUtils.nextInt(1, 31)) + String.format("%02d", RandomUtils.nextInt(1, 5)) + "房";
return prefix + road + "路" + RandomUtils.nextInt(1, 1000) + "号" + community + extra + building + unit + room;
}
}
}
Loading

0 comments on commit 176ca9a

Please sign in to comment.