Commit 9619feb2 by KingBoy

init

parents
#gralde build
build
gradle
.gradle
classes
gradlew*
#idea
.idea
*.iml
out
#java
*.class
*.jar
*.war
*.ear
#log
phoenix_log/
\ No newline at end of file
group 'com.king'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
orikaVersion = '1.5.2'
fastJsonVersion = '1.2.41'
lombokVersion = '1.16.18'
}
dependencies {
compile("ma.glasnost.orika:orika-core:$orikaVersion")
compile("org.projectlombok:lombok:$lombokVersion")
compile("com.alibaba:fastjson:$fastJsonVersion")
testCompile group: 'junit', name: 'junit', version: '4.11'
}
package com.king.common.utils.apiresult;
import lombok.Data;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:19
* @desc 返回体.
*/
@Data
public abstract class AbstractApiResult {
protected String code;
/**
* 成功的返回
* @param data 数据
* @return 正常返回体
*/
public static AbstractApiResult success(Object data) {
return new SuccessApiResult(data);
}
/**
* 错误返回
* @param errorCode 错误码
* @param errorMessage 错误信息
* @return 错误返回体
*/
public static AbstractApiResult error(String errorCode, String errorMessage) {
return new ErrorApiResult(errorCode, errorMessage);
}
}
package com.king.common.utils.apiresult;
import lombok.Data;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:59
* @desc 错误返回.
*/
@Data
public class ErrorApiResult extends AbstractApiResult {
private String msg;
ErrorApiResult(String code, String msg) {
this.code = code;
this.msg = msg;
}
}
package com.king.common.utils.apiresult;
import lombok.Data;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/21 上午11:28
* @desc 正确返回体
*/
@Data
public class SuccessApiResult extends AbstractApiResult {
private Object data;
SuccessApiResult(Object data) {
this.code = "0";
this.data = data;
}
}
package com.king.common.utils.date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午9:16
* @desc java8日期工具.
*/
public final class LocalDateTimeUtils {
private LocalDateTimeUtils() { }
//获取当前时间的LocalDateTime对象
//LocalDateTime.now();
//根据年月日构建LocalDateTime
//LocalDateTime.of();
//比较日期先后
//LocalDateTime.now().isBefore(),
//LocalDateTime.now().isAfter(),
//Date转换为LocalDateTime
public static LocalDateTime convertDateToLDT(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
//LocalDateTime转换为Date
public static Date convertLDTToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
//获取指定日期的毫秒
public static Long getMilliByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
//获取指定日期的秒
public static Long getSecondsByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}
//获取指定时间的指定格式
public static String formatTime(LocalDateTime time, String pattern) {
return time.format(DateTimeFormatter.ofPattern(pattern));
}
//获取当前时间的指定格式
public static String formatNow(String pattern) {
return formatTime(LocalDateTime.now(), pattern);
}
//日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
}
//日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) {
return time.minus(number, field);
}
/**
* 获取两个日期的差 field参数为ChronoUnit.*
* @param startTime
* @param endTime
* @param field 单位(年月日时分秒)
* @return
*/
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS) {
return period.getYears();
}
if (field == ChronoUnit.MONTHS) {
return period.getYears() * 12 + period.getMonths();
}
return field.between(startTime, endTime);
}
//获取一天的开始时间,2017,7,22 00:00
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
//获取一天的结束时间,2017,7,22 23:59:59.999999999
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
}
}
package com.king.common.utils.json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/26 下午12:07
* @desc Json工具.
*/
public final class JsonUtils {
private JsonUtils() { }
/**
* json串转换为对象
* @param json
* @param clazz
* @param <T>
* @return
*/
public static <T> T jsonToBean(String json, Class<T> clazz) {
return JSON.parseObject(json, clazz);
}
/**
* 对象转换为json,可以带上date的格式化
* @param object
* @return
*/
public static String beanToJson(Object object, String dateFormat) {
if (Objects.isNull(dateFormat) || "".equals(dateFormat)) {
return JSON.toJSONString(object);
}
return JSON.toJSONStringWithDateFormat(object, dateFormat);
}
/**
* json返回List
* @param arrayJson
* @param clazz
* @param <T>
* @return
*/
public static <T> List<T> jsonToList(String arrayJson, Class<T> clazz, String dateFormat) {
String temp = JSONObject.DEFFAULT_DATE_FORMAT;
if (!"".equals(dateFormat) && dateFormat != null) {
JSONObject.DEFFAULT_DATE_FORMAT = dateFormat;
}
List<T> list = JSON.parseArray(arrayJson, clazz);
JSONObject.DEFFAULT_DATE_FORMAT = temp;
return list;
}
/**
* 反序列化Map
* @param mapJson
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map jsonMap(String mapJson, Class<K> keyType, Class<V> valueType) {
return JSON.parseObject(mapJson, new TypeReference<Map<K, V>>() { });
}
}
package com.king.common.utils.mapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:54
* @desc 属性映射工具.
*/
public final class MapperUtils {
private MapperUtils() { }
/**
* 构建一个Mapper工厂
*/
private static final MapperFactory MAPPER_FACTORY = new DefaultMapperFactory.Builder().build();
/**
* 将s属性映射到R的具体实例上
* @param s 已有的Bean,源Bean
* @param rClass
* @param <S> sourceBean
* @param <R> ReturnBean
* @return R的实例
*/
public static <S, R> R mapperBean(S s, Class<R> rClass) {
return MAPPER_FACTORY.getMapperFacade().map(s, rClass);
}
/**
* 将s属性映射到R的具体实例上,如果转换的属性名不一样,可以传入Map进行说明
* @param s 已有的Bean,源Bean
* @param rClass
* @param <S> sourceBean
* @param <R> ReturnBean
* @return R的实例
*/
public static <S, R> R mapperBean(S s, Class<R> rClass, Map<String, String> diffFieldMap) {
ClassMapBuilder<?, R> classMap = MAPPER_FACTORY.classMap(s.getClass(), rClass);
diffFieldMap.forEach(classMap::field);
classMap.byDefault()
.register();
return MAPPER_FACTORY.getMapperFacade().map(s, rClass);
}
/**
* 将s的集合射成R的集合
* @param sList 已有的Bean的集合
* @param rClass 要转换的类型
* @param <S> sourceBean
* @param <R> ReturnBean
* @return R的实例
*/
public static <S, R> List<R> mapperList(List<S> sList, Class<R> rClass) {
return MAPPER_FACTORY.getMapperFacade().mapAsList(sList, rClass);
}
/**
* 将s的集合射成R的集合,不同的属性通过Map<String, String> 传入
* @param sList 已有的Bean的集合
* @param rClass 要转换的类型
* @param <S> sourceBean
* @param <R> ReturnBean
* @return R的实例
*/
public static <S, R> List<R> mapperList(List<S> sList, Class<R> rClass, Map<String, String> diffFieldMap) {
if (sList.isEmpty()) {
return Collections.emptyList();
}
ClassMapBuilder<?, R> classMap = MAPPER_FACTORY.classMap(sList.get(0).getClass(), rClass);
diffFieldMap.forEach(classMap::field);
classMap.byDefault()
.register();
return MAPPER_FACTORY.getMapperFacade().mapAsList(sList, rClass);
}
}
package com.king.common.utils.md5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
/**
* @author kingboy
* @date 2017/7/22 下午1:00
* @desc MD5工具类, 全部接收UTF编码的String
*/
public final class MD5Utils {
private MD5Utils() { }
private static final String ALGORITHM_MD5 = "MD5";
private static final String UTF_8 = "UTF-8";
/**
* MD5 16bit 小写.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static String md5Bit16Lower(String readyEncryptStr) throws Exception {
if (Objects.nonNull(readyEncryptStr)) {
return MD5Utils.md5Bit32Lower(readyEncryptStr).substring(8, 24);
} else {
return null;
}
}
/**
* MD5 16bit 大写.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static String md5Bit16Upper(String readyEncryptStr) throws Exception {
return md5Bit16Lower(readyEncryptStr).toUpperCase();
}
/**
* MD5 32bit 小写.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static String md5Bit32Lower(String readyEncryptStr) throws Exception {
if (Objects.nonNull(readyEncryptStr)) {
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
md.update(readyEncryptStr.getBytes(UTF_8));
byte[] b = md.digest();
StringBuilder su = new StringBuilder();
for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
String haxHex = Integer.toHexString(b[offset] & 0xFF);
if (haxHex.length() < 2) {
su.append("0");
}
su.append(haxHex);
}
return su.toString();
} else {
return null;
}
}
/**
* MD5 32bit 大写.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static String md5Bit32Upper(String readyEncryptStr) throws Exception {
return md5Bit32Lower(readyEncryptStr).toUpperCase();
}
}
package com.king.common.utils.page;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:54
* @desc 分页请求参数封装.
*/
public class PageParam {
public static final int PAGE_SIZE = 10;
public PageParam() {
this.c = PAGE_SIZE;
}
public PageParam(Integer p, Integer c) {
this.setP(p);
this.setC(c);
}
//当前页
private Integer p = 1;
//每页容量
private Integer c;
public int firstResult() {
return (p - 1) * c;
}
public Integer getP() {
return p;
}
public void setP(Integer p) {
if (p != null) {
this.p = p;
}
}
public Integer getC() {
return c;
}
public void setC(Integer c) {
if (c != null) {
this.c = c;
}
}
}
package com.king.common.utils.page;
import java.util.List;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:53
* @param <T> 实体类型
* @desc 分页容器.
*/
public class PageResult<T> {
private PageResult() { }
//当前页
private Integer page;
//总数量
private Integer count;
//分页数据
private List<T> list;
public PageResult(int page, int count, List<T> list) {
this.page = page;
this.count = count;
this.list = list;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
package com.king.common.utils.page;
import com.king.common.utils.mapper.MapperUtils;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午7:53
* @desc PageResul工厂.
*/
public class PageResultFactory {
public <T> PageResult createPageResult(int page, int count, List<T> data) {
return new PageResult<T>(page, count, data);
}
public <E> PageResult<E> convert(PageResult pageResult, Class<E> dtoClass) {
List<E> dtoList = MapperUtils.mapperList(pageResult.getList(), dtoClass);
return new PageResult<E>(pageResult.getPage(), pageResult.getCount(), dtoList);
}
public <T, E> PageResult<E> convert(PageResult<T> pageResult, Function<T, E> function) {
return new PageResult<E>(pageResult.getPage(), pageResult.getCount(),
pageResult.getList().stream().map(function).collect(Collectors.toList()));
}
}
package com.king.common.utils.random;
import java.util.Random;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/24 上午11:05
* @desc 随机生成工具类.
*/
public final class RandomUtils {
private RandomUtils() { }
private static Random random = new Random();
/**
* 获取一个固定长度的随机整数,可当做验证码。
* @param size 长度
* @return String
*/
public static String getRandomNum(int size) {
return String.valueOf(Math.random()).substring(2, size + 2);
}
/**
* 获取两个数的中间数,包含min和max
* @param min 最小
* @param max 最大
* @return 中间数
*/
public static int getMidNum(int min, int max) {
return min + random.nextInt(max - min + 1);
}
}
package com.king.common.utils.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/23 下午9:12
* @desc 正则.
*/
public final class RegexUtils {
private static Pattern mobile = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
private static Pattern chinese = Pattern.compile("[\u4e00-\u9fa5]");
private RegexUtils() { }
/**
* @param mobiles
* @return isMobileNO
* @description 校验手机号是否正确
*/
public static boolean isMobileNO(String mobiles) {
Matcher m = mobile.matcher(mobiles);
return m.matches();
}
/**
* @param email
* @return isEmail
* @description 校验邮箱是否正确
*/
public static boolean isEmail(String email) {
String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(email);
return m.matches();
}
/**
* @param value
* @return isInteger
* @description 校验是否是整数
*/
public static boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断是否含有特殊字符
* @param text
* @return boolean true,通过,false,没通过
*/
public static boolean hasSpecialChar(String text) {
if (null == text || "".equals(text)) {
return true;
}
if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
// 如果不包含特殊字符
return false;
}
return true;
}
/**
* 判断是否正整数
* @param number 数字
* @return boolean true,通过,false,没通过
*/
public static boolean isNumber(String number) {
if (null == number || "".equals(number)) {
return false;
}
String regex = "[0-9]*";
return number.matches(regex);
}
/**
* 判断是否是正确的IP地址
* @param ip
* @return boolean true,通过,false,没通过
*/
public static boolean isIp(String ip) {
if (null == ip || "".equals(ip)) {
return false;
}
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
return ip.matches(regex);
}
/**
* 判断是否含有中文,仅适合中国汉字,不包括标点
* @param text
* @return boolean true,通过,false,没通过
*/
public static boolean isChinese(String text) {
if (null == text || "".equals(text)) {
return false;
}
Matcher m = chinese.matcher(text);
return m.find();
}
}
package com.king.common.utils.uuid;
import java.util.ArrayList;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/7/26 上午10:05
* @desc uuid生成.
*/
public final class UUIDUtils {
private UUIDUtils() { }
/**
* 获取UUID,不含有-
* @return
*/
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 批量获取UUID
* @param size
* @return
*/
public static ArrayList<String> getUUIDList(int size) {
return Stream.iterate(1, item -> item + 1)
.limit(size)
.map(item -> getUUID())
.collect(Collectors.toCollection(ArrayList::new));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment