Skip to content

Commit

Permalink
Merge pull request #60 from yindz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yindz authored Aug 6, 2022
2 parents 87304bc + 9f6b12a commit 1d0af21
Show file tree
Hide file tree
Showing 15 changed files with 628 additions and 25 deletions.
32 changes: 31 additions & 1 deletion 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.15</version>
<version>1.0.16</version>
<packaging>jar</packaging>
<name>common-random</name>
<description>An easy-to-use random data generator.</description>
Expand All @@ -23,7 +23,10 @@
<commons-collections.version>4.4</commons-collections.version>
<guava.version>31.1-jre</guava.version>
<jackson-databind.version>2.13.3</jackson-databind.version>
<gson.version>2.9.0</gson.version>
<fastjson.version>1.2.83</fastjson.version>
<tinypinyin.version>2.0.3.RELEASE</tinypinyin.version>
<pinyin4j.version>2.5.1</pinyin4j.version>
<slf4j-api.version>1.7.36</slf4j-api.version>
<slf4j-simple.version>1.7.36</slf4j-simple.version>
<junit.version>4.13.2</junit.version>
Expand Down Expand Up @@ -56,16 +59,43 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<!--JSON libraries-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<optional>true</optional>
</dependency>

<!--Chinese pinyin-->
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>TinyPinyin</artifactId>
<version>${tinypinyin.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>${pinyin4j.version}</version>
<optional>true</optional>
</dependency>

<!--Logs-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public String randomCreditCardNo(CreditCardType type) {
* <p> 特别说明: 不会与现实中的真实借记卡号产生重合 </p>
*
* @return 随机虚拟借记卡号码
* @since 1.0.15
*/
public String randomDebitCardNo() {
//发卡机构标识码9000~9999为目前尚未实际使用的号段(为了避免不必要的麻烦)
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/apifan/common/random/source/OtherSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.apifan.common.random.entity.Area;
import com.apifan.common.random.entity.EconomicCategory;
import com.apifan.common.random.entity.Poem;
import com.apifan.common.random.util.JsonUtils;
import com.apifan.common.random.util.ResourceUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -198,12 +196,9 @@ private OtherSource() {
astonishingPrefixList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("astonishing-prefix.txt"));
chineseIdiomsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("chinese-idioms.txt"));
englishWordsList = ResourceUtils.readLines("word-en.txt");

ObjectMapper objectMapper = new ObjectMapper();
CollectionType poemType = objectMapper.getTypeFactory().constructCollectionType(List.class, Poem.class);
try {
tangPoemsList = objectMapper.readValue(ResourceUtils.readString("tang-poems.json"), poemType);
} catch (JsonProcessingException e) {
tangPoemsList = JsonUtils.parseObjectList(ResourceUtils.readString("tang-poems.json"), Poem.class);
} catch (Exception e) {
logger.error("初始化数据异常", e);
}
}
Expand Down Expand Up @@ -524,6 +519,7 @@ public String randomChineseIdiom() {
*
* @param words 词语数量
* @return 随机英文文本
* @since 1.0.15
*/
public String randomEnglishText(int words) {
Preconditions.checkArgument(words > 1, "词语数量必须大于1");
Expand All @@ -534,6 +530,7 @@ public String randomEnglishText(int words) {
* 随机统一社会信用代码(虚拟)
*
* @return 统一社会信用代码(虚拟)
* @since 1.0.15
*/
public String randomSocialCreditCode() {
String prefix = "91";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.apifan.common.random.constant.CreditCardType;
import com.apifan.common.random.constant.RandomConstant;
import com.apifan.common.random.entity.IdPrefix;
import com.apifan.common.random.util.PinyinUtils;
import com.apifan.common.random.util.ResourceUtils;
import com.github.promeg.pinyinhelper.Pinyin;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -296,7 +296,7 @@ public String randomChineseNickName(int maxLength) {
*/
public String randomPinyinNickName(int maxLength) {
String nickName = randomChineseNickName(maxLength);
return Pinyin.toPinyin(nickName, "").toLowerCase();
return PinyinUtils.toPinyin(nickName, true);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/apifan/common/random/util/DataUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.apifan.common.random.util;

import com.apifan.common.random.entity.DataField;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -23,8 +21,6 @@
public class DataUtils {
private static final Logger logger = LoggerFactory.getLogger(DataUtils.class);

private static final ObjectMapper objectMapper = new ObjectMapper();

/**
* 生成JSON
*
Expand Down Expand Up @@ -97,7 +93,7 @@ public static String generateCsv(List<DataField> fieldList, int total) {
*/
public static <T> T generateObject(List<DataField> fieldList, Class<T> clazz) throws Exception {
Preconditions.checkArgument(clazz != null, "对象类型为空");
return objectMapper.readValue(generateJson(fieldList), clazz);
return JsonUtils.parseObject(generateJson(fieldList), clazz);
}

/**
Expand Down Expand Up @@ -153,8 +149,8 @@ private static String generateJson(List<DataField> fieldList) {
element.put(f.getField(), f.getValueSupplier().get());
});
try {
return objectMapper.writeValueAsString(element);
} catch (JsonProcessingException e) {
return JsonUtils.toJson(element);
} catch (Exception e) {
logger.error("转换JSON字符串时出错", e);
}
return null;
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/apifan/common/random/util/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.apifan.common.random.util;

import com.apifan.common.random.util.json.JsonConverter;
import com.apifan.common.random.util.json.impl.FastjsonConverter;
import com.apifan.common.random.util.json.impl.GsonConverter;
import com.apifan.common.random.util.json.impl.JacksonConverter;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
* JSON工具类
*
* <p>
* 从 1.0.16 起,不再强依赖 jackson; <br>
* 支持目前常用的以下3种json第三方库: jackson/fastjson/gson <br>
* 检测顺序:jackson>fastjson>gson <br>
* 注意: 不要忘记手动添加依赖
* </p>
*
* @author yin
* @since 1.0.16
*/
public class JsonUtils {
private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);

private static final JsonConverter jsonConverter;

static {
jsonConverter = getRealJsonConverter();
}

/**
* 对象转换为JSON字符串
*
* @param obj 对象
* @return JSON字符串
*/
public static String toJson(Object obj) {
Preconditions.checkNotNull(obj, "对象为空");
return jsonConverter.toJson(obj);
}

/**
* 解析JSON字符串并转换为单个对象
*
* @param text 待解析的JSON字符串
* @param targetClass 目标类
* @param <T> 泛型
* @return 对象
*/
public static <T> T parseObject(String text, Class<T> targetClass) {
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空");
Preconditions.checkNotNull(targetClass, "目标类为空");
return jsonConverter.parseObject(text, targetClass);
}

/**
* 解析JSON字符串并转换为对象列表
*
* @param text 待解析的JSON字符串
* @param targetClass 目标类
* @param <T> 泛型
* @return 对象列表
*/
public static <T> List<T> parseObjectList(String text, Class<T> targetClass) {
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空");
Preconditions.checkNotNull(targetClass, "目标类为空");
return jsonConverter.parseObjectList(text, targetClass);
}

/**
* 解析JSON字符串并转换为Map列表
*
* @param text 待解析的JSON字符串
* @return Map列表
*/
public static List<Map<String, Object>> parseMapList(String text) {
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空");
return jsonConverter.parseMapList(text);
}

/**
* 获取JSON转换器实例
*
* @return JSON转换器实例
*/
private static JsonConverter getRealJsonConverter() {
if (ResourceUtils.isClassLoaded("com.fasterxml.jackson.databind.ObjectMapper")) {
log.info("将使用 jackson");
return new JacksonConverter();
} else if (ResourceUtils.isClassLoaded("com.alibaba.fastjson.JSON")) {
log.info("将使用 fastjson");
return new FastjsonConverter();
} else if (ResourceUtils.isClassLoaded("com.google.gson.Gson")) {
log.info("将使用 gson");
return new GsonConverter();
} else {
throw new RuntimeException("没有找到可用的JSON库");
}
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/apifan/common/random/util/PinyinUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.apifan.common.random.util;

import com.apifan.common.random.util.pinyin.PinyinConverter;
import com.apifan.common.random.util.pinyin.impl.Pinyin4jConverter;
import com.apifan.common.random.util.pinyin.impl.TinyPinyinConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 拼音工具类
* <p>
* 从 1.0.16 起,不再强依赖 tinypinyin; <br>
* 支持目前常用的以下2种第三方库: tinypinyin/pinyin4j <br>
* 检测顺序:tinypinyin>pinyin4j <br>
* 注意: 不要忘记手动添加依赖
* </p>
*
* @author yin
* @since 1.0.16
*/
public class PinyinUtils {
private static final Logger log = LoggerFactory.getLogger(PinyinUtils.class);

private static final PinyinConverter pinyinConverter;

static {
pinyinConverter = getRealPinyinConverter();
}

/**
* 转换成拼音
*
* @param src 原始字符串
* @param toLowerCase 是否转换为小写
* @return 拼音
*/
public static String toPinyin(String src, boolean toLowerCase) {
return pinyinConverter.toPinyin(src, toLowerCase);
}

/**
* 获取拼音转换器实例
*
* @return 拼音转换器实例
*/
private static PinyinConverter getRealPinyinConverter() {
if (ResourceUtils.isClassLoaded("com.github.promeg.pinyinhelper.Pinyin")) {
log.info("将使用 tinypinyin");
return new TinyPinyinConverter();
} else if (ResourceUtils.isClassLoaded("net.sourceforge.pinyin4j.PinyinHelper")) {
log.info("将使用 pinyin4j");
return new Pinyin4jConverter();
} else {
throw new RuntimeException("没有找到可用的拼音库");
}
}
}
29 changes: 22 additions & 7 deletions src/main/java/com/apifan/common/random/util/ResourceUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.apifan.common.random.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -62,11 +60,9 @@ public static String readString(String fileName) {
*/
public static List<Map<String, Object>> readAsMapList(String fileName) {
Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), "资源文件名为空");
ObjectMapper objectMapper = new ObjectMapper();
try {
CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class);
return objectMapper.readValue(readString(fileName), collectionType);
} catch (IOException e) {
return JsonUtils.parseMapList(readString(fileName));
} catch (Exception e) {
logger.error("解析json出现异常", e);
}
return null;
Expand All @@ -83,7 +79,7 @@ public static <T> T getRandomElement(List<T> elementList) {
List<T> randomElement = getRandomElement(elementList, 1);
return randomElement.isEmpty() ? null : randomElement.get(0);
}

/**
* 从列表中获取number个随机元素
*
Expand Down Expand Up @@ -160,4 +156,23 @@ public static List<String> base64DecodeLines(List<String> lines) {
});
return decoded;
}

/**
* 判断当前运行环境下是否存在某个类
*
* @param clazzName 类名
* @return
*/
public static boolean isClassLoaded(String clazzName) {
if (StringUtils.isBlank(clazzName)) {
return false;
}
try {
Class.forName(clazzName);
return true;
} catch (ClassNotFoundException e) {
logger.warn("未找到类: {}", clazzName);
return false;
}
}
}
Loading

0 comments on commit 1d0af21

Please sign in to comment.