Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
60237a90
Commit
60237a90
authored
Mar 20, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GIT工具类
parent
609ffc62
Changes
21
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
3181 additions
and
356 deletions
+3181
-356
pom.xml
pom.xml
+12
-0
MemoryCache.java
src/main/java/com/yanzuoguang/util/cache/MemoryCache.java
+232
-232
MemoryCacheCenter.java
...in/java/com/yanzuoguang/util/cache/MemoryCacheCenter.java
+26
-24
ByteHelper.java
src/main/java/com/yanzuoguang/util/helper/ByteHelper.java
+186
-99
DateAutoHelper.java
...main/java/com/yanzuoguang/util/helper/DateAutoHelper.java
+132
-0
DateHelper.java
src/main/java/com/yanzuoguang/util/helper/DateHelper.java
+593
-0
EnumHelper.java
src/main/java/com/yanzuoguang/util/helper/EnumHelper.java
+73
-0
Event.java
src/main/java/com/yanzuoguang/util/helper/Event.java
+89
-0
EventRun.java
src/main/java/com/yanzuoguang/util/helper/EventRun.java
+17
-0
ExceptionHelper.java
...ain/java/com/yanzuoguang/util/helper/ExceptionHelper.java
+32
-0
HttpHelper.java
src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+115
-0
JSONHelper.java
src/main/java/com/yanzuoguang/util/helper/JSONHelper.java
+81
-0
RunHelper.java
src/main/java/com/yanzuoguang/util/helper/RunHelper.java
+53
-0
StringHelper.java
src/main/java/com/yanzuoguang/util/helper/StringHelper.java
+719
-0
ThreadHelper.java
src/main/java/com/yanzuoguang/util/helper/ThreadHelper.java
+426
-0
TableHead.java
...ain/java/com/yanzuoguang/util/helper/table/TableHead.java
+72
-0
TableHeadHelper.java
...va/com/yanzuoguang/util/helper/table/TableHeadHelper.java
+145
-0
TableHeadItem.java
...java/com/yanzuoguang/util/helper/table/TableHeadItem.java
+124
-0
BaseVo.java
src/main/java/com/yanzuoguang/util/vo/BaseVo.java
+1
-1
TestByteHelper.java
src/test/java/helper/TestByteHelper.java
+32
-0
TestDateAutoHelper.java
src/test/java/helper/TestDateAutoHelper.java
+21
-0
No files found.
pom.xml
View file @
60237a90
...
...
@@ -18,6 +18,12 @@
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
<!-- Spring依赖 -->
<!-- 1.Spring核心依赖 -->
...
...
@@ -54,6 +60,12 @@
<artifactId>
fastjson
</artifactId>
<version>
${fastjson.version}
</version>
</dependency>
<dependency>
<groupId>
org.jetbrains
</groupId>
<artifactId>
annotations
</artifactId>
<version>
RELEASE
</version>
<scope>
compile
</scope>
</dependency>
</dependencies>
<dependencyManagement>
...
...
src/main/java/com/yanzuoguang/util/cache/MemoryCache.java
View file @
60237a90
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/cache/MemoryCacheCenter.java
View file @
60237a90
//package com.yanzuoguang.util.cache;
//
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Vector;
//
//@Component
//public class MemoryCacheCenter {
//
// /**
// * 缓存的对象
// */
// public static List<MemoryCache> CLEAR_LIST = new Vector<MemoryCache>();
//
// /**
// * 间隔5秒执行
// */
// @Scheduled(cron = "0/5 * * * * ? ")
// public void clear(){
// // 死循环处理
package
com
.
yanzuoguang
.
util
.
cache
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Vector
;
@Component
public
class
MemoryCacheCenter
{
/**
* 缓存的对象
*/
public
static
List
<
MemoryCache
>
CLEAR_LIST
=
new
Vector
<
MemoryCache
>();
/**
* 间隔5秒执行
*/
@Scheduled
(
cron
=
"0/5 * * * * ? "
)
public
void
clear
(){
// todo: 需要修改
// 死循环处理
// ThreadList<MemoryCache> threadList = new ThreadList<MemoryCache>(1) {
// @Override
// public void Execute(MemoryCache item) {
...
...
@@ -29,5 +31,5 @@
// threadList.setPrint(false);
// threadList.add(CLEAR_LIST);
// threadList.waitSucccess(true);
//
}
//
}
}
}
src/main/java/com/yanzuoguang/util/
base/ByteConvert
Helper.java
→
src/main/java/com/yanzuoguang/util/
helper/Byte
Helper.java
View file @
60237a90
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/helper/DateAutoHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* 将时间格式字符串转换为时间
*/
public
class
DateAutoHelper
{
/**
* 无符号正则表达式
*/
public
static
final
String
DATE_FORMAT_NO_SPLIT_REGEX
=
"^(\\d{4})(\\d{2})(\\d{2})$"
;
/**
* 有符号正常日期格式
*/
public
static
final
String
DATE_FORMAT_COMMON_REGEX
=
"^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2})$"
;
/**
* 有符号正常日期格式替换
*/
public
static
final
String
DATE_FORMAT_COMMON_REPLACE
=
"$1-$2-$3 00:00:00.000"
;
/**
* 倒序的日期格式
*/
public
static
final
String
DATE_FORMAT_REVERT_REGEX
=
"^(\\d{1,2})[/-](\\d{1,2})[/-](\\d{4,})$"
;
/**
* 有符号正常日期格式替换
*/
public
static
final
String
DATE_FORMAT_REVERT_REPLACE
=
"$3-$2-$1 00:00:00.000"
;
/**
* 正常时间格式
*/
public
static
final
String
DATETIME_HOUR_FORMAT_REGEX
=
"^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2}).{1}(\\d{1,2}):(\\d{1,2})$"
;
/**
* 正常时间格式替换
*/
public
static
final
String
DATETIME_HOUR_FORMAT_REPLACE
=
"$1-$2-$3 $4:$5:00.000"
;
/**
* 正常时间格式
*/
public
static
final
String
DATETIME_FORMAT_REGEX
=
"^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2}).{1}(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$"
;
/**
* 正常时间格式替换
*/
public
static
final
String
DATETIME_FORMAT_REPLACE
=
"$1-$2-$3 $4:$5:$6.000"
;
/**
* 时间格式化字符串 yyyy-MM-dd HH:mm:ss.SSS
*/
public
static
final
String
DATETIME_FULL_FORMAT
=
"yyyy-MM-dd HH:mm:ss.SSS"
;
/**
* 缓存的自动识别的格式正则表达式
*/
private
static
List
<
DateReplace
>
autoDateCache
=
new
ArrayList
<>();
static
{
registerAutoFormat
(
DATE_FORMAT_NO_SPLIT_REGEX
,
DATE_FORMAT_COMMON_REPLACE
);
registerAutoFormat
(
DATE_FORMAT_COMMON_REGEX
,
DATE_FORMAT_COMMON_REPLACE
);
registerAutoFormat
(
DATE_FORMAT_REVERT_REGEX
,
DATE_FORMAT_REVERT_REPLACE
);
registerAutoFormat
(
DATETIME_HOUR_FORMAT_REGEX
,
DATETIME_HOUR_FORMAT_REPLACE
);
registerAutoFormat
(
DATETIME_FORMAT_REGEX
,
DATETIME_FORMAT_REPLACE
);
}
/**
* 时间格式字符串
*/
private
static
class
DateReplace
{
// 正则表达式
public
String
regex
;
// 替换表达式
public
String
replace
;
// 终止标志位
public
boolean
end
;
}
/**
* 注册正则表达式,将时间转换为正确格式的正则表达式,后注册的会优先执行 。
*
* @param regex 正则表达式
* @param replace 替换表达式
*/
public
static
void
registerAutoFormat
(
String
regex
,
String
replace
)
{
registerAutoFormat
(
regex
,
replace
,
true
);
}
/**
* 注册正则表达式,将时间转换为正确格式的正则表达式,后注册的会优先执行 。
*
* @param regex 正则表达式
* @param replace 替换表达式
* @param end 是否需要结束
*/
public
static
void
registerAutoFormat
(
String
regex
,
String
replace
,
boolean
end
)
{
DateReplace
item
=
new
DateReplace
();
item
.
regex
=
regex
;
item
.
replace
=
replace
;
item
.
end
=
end
;
autoDateCache
.
add
(
item
);
}
/**
* 根据时间字符串自动识别时间
*
* @param date 时间字符串
* @return 时间
*/
public
static
Date
getAutoDate
(
String
date
)
throws
ParseException
{
if
(
date
==
null
||
date
.
trim
().
length
()
<
1
)
{
return
null
;
}
date
=
date
.
trim
();
int
size
=
autoDateCache
.
size
();
for
(
int
i
=
size
-
1
;
i
>=
0
;
i
--)
{
// 遍历所有时间格式
DateReplace
item
=
autoDateCache
.
get
(
i
);
String
dateTo
=
date
.
replaceAll
(
item
.
regex
,
item
.
replace
);
// 如何替换成功,且终止标志位为true则终止执行
boolean
isBreak
=
item
.
end
&&
!
dateTo
.
equals
(
date
);
date
=
dateTo
;
if
(
isBreak
)
{
break
;
}
}
// 将正常格式的时间字符串转换为时间
return
new
SimpleDateFormat
(
DATETIME_FULL_FORMAT
).
parse
(
String
.
valueOf
(
date
));
}
}
src/main/java/com/yanzuoguang/util/helper/DateHelper.java
0 → 100644
View file @
60237a90
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/helper/EnumHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
java.lang.reflect.Method
;
import
java.util.HashMap
;
/**
* 字符串和枚举进行转换
*/
public
class
EnumHelper
{
private
static
HashMap
<
Class
,
Method
>
CacheMethod
=
new
HashMap
<>();
public
static
<
T
extends
Enum
<
T
>>
T
toEnum
(
String
vStr
,
T
vDefault
)
{
Class
<
T
>
vType
=
(
Class
<
T
>)
vDefault
.
getClass
();
T
result
=
vDefault
;
try
{
if
(!
StringHelper
.
isEmpty
(
vStr
))
{
result
=
Enum
.
valueOf
(
vType
,
vStr
);
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ex
,
vStr
);
}
return
result
;
}
public
static
<
T
>
T
toEnum
(
Class
<
T
>
vType
,
String
vStr
)
{
return
toEnum
(
vType
,
vStr
,
null
);
}
public
static
<
T
>
T
toEnum
(
Class
<
T
>
vType
,
String
vStr
,
T
vDefault
)
{
T
result
=
vDefault
;
try
{
if
(
StringHelper
.
isInteger
(
vStr
))
{
result
=
toEnum
(
vType
,
StringHelper
.
toInt
(
vStr
));
}
else
if
(!
StringHelper
.
isEmpty
(
vStr
))
{
result
=
(
T
)
Enum
.
valueOf
((
Class
)
vType
,
vStr
);
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ex
,
vStr
);
}
if
(
result
==
null
&&
vDefault
==
null
)
{
result
=
toEnum
(
vType
,
0
);
}
return
result
;
}
public
static
<
T
>
T
toEnum
(
Class
<
T
>
vType
,
int
i
)
{
T
result
=
null
;
try
{
if
(
CacheMethod
.
containsKey
(
vType
))
{
Method
met
=
CacheMethod
.
get
(
vType
);
result
=
(
T
)
met
.
invoke
(
null
,
i
);
}
else
{
Method
[]
vMets
=
vType
.
getMethods
();
for
(
Method
vMet
:
vMets
)
{
String
vName
=
vMet
.
getName
();
if
(
vName
.
equals
(
"forValue"
)
&&
vMet
.
getParameterTypes
().
length
==
1
)
{
Class
vTempType
=
vMet
.
getParameterTypes
()[
0
];
Object
obj
=
vMet
.
invoke
(
null
,
i
);
result
=
(
T
)
obj
;
CacheMethod
.
put
(
vType
,
vMet
);
break
;
}
}
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ex
,
i
);
}
return
result
;
}
}
src/main/java/com/yanzuoguang/util/helper/Event.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Java事件处理类
*
* @param <T>
*/
public
class
Event
<
T
>
{
// 事件列表
private
List
<
T
>
m_List
=
new
ArrayList
<>();
/**
* 添加处理对象
*
* @param t
*/
public
void
add
(
T
t
)
{
if
(
t
!=
null
)
{
m_List
.
add
(
t
);
}
}
/**
* 触发事件
*
* @param m
* @param <M>
* @throws Exception
*/
public
<
M
extends
EventRun
<
T
>>
void
exeucte
(
M
m
)
throws
Exception
{
for
(
T
item
:
m_List
)
{
m
.
Execute
(
item
);
}
}
/**
* 删除某个事件处理类
*
* @param t 需要删除的事件处理类
* @return
*/
public
boolean
remove
(
T
t
)
{
if
(
t
!=
null
)
{
return
m_List
.
remove
(
t
);
}
return
false
;
}
/**
* 清除所有事件
*/
public
void
clear
()
{
m_List
.
clear
();
}
/**
* 判断是否包含事件
*
* @param t 需要判断的事件
* @return 是否包含
*/
public
boolean
contants
(
T
t
)
{
if
(
t
!=
null
)
{
return
m_List
.
contains
(
t
);
}
return
false
;
}
/**
* 获取事件列表
*
* @return 事件列表集合
*/
public
List
<
T
>
getList
()
{
return
m_List
;
}
/**
* 获取事件长度
*
* @return 长度
*/
public
int
size
()
{
return
m_List
.
size
();
}
}
src/main/java/com/yanzuoguang/util/helper/EventRun.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
/**
* 执行的时候得事件处理类
*
* @param <T> 需要处理的数据的类型
*/
public
interface
EventRun
<
T
>
{
/**
* 事件的执行主体
*
* @param t 接口
* @throws Exception 异常错误
*/
void
Execute
(
T
t
)
throws
Exception
;
}
src/main/java/com/yanzuoguang/util/helper/ExceptionHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
/**
* 异常处理帮助类
*/
public
class
ExceptionHelper
{
/**
* 不跑出异常,记录日常日志
*
* @param ex
* @param from
*/
public
static
void
handleException
(
Throwable
ex
,
Object
from
)
{
// todo: 需要修改
// Log.error(ExceptionHelper.class, ex, StringHelper.toString(from));
}
/**
* 异常信息输出
*
* @param local
* @param start
* @param end
* @param allsize
* @param str
* @throws Exception
*/
public
static
void
subStringException
(
String
local
,
int
start
,
int
end
,
int
allsize
,
String
str
)
throws
Exception
{
String
exception
=
local
+
"/n"
+
"开始:"
+
start
+
" 结束:"
+
end
+
"总长:"
+
allsize
+
"/n"
+
str
;
throw
new
Exception
(
exception
);
}
}
src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintWriter
;
import
java.net.HttpURLConnection
;
/**
* HTTP请求工具类
*/
public
class
HttpHelper
{
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
public
static
String
post
(
String
url
,
String
jsonString
)
{
try
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
return
post
(
httpConn
,
url
,
jsonString
);
}
catch
(
Exception
ex
)
{
throw
new
CodeException
(
ex
);
}
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
public
static
String
postApplicationJSON
(
String
url
,
String
jsonString
)
{
try
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
httpConn
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
return
post
(
httpConn
,
url
,
jsonString
);
}
catch
(
Exception
ex
)
{
throw
new
CodeException
(
ex
);
}
}
/**
* 获取连接
*
* @param url
* @return
* @throws IOException
*/
private
static
HttpURLConnection
getConn
(
String
url
)
throws
IOException
{
// 创建URL对象
java
.
net
.
URL
connURL
=
new
java
.
net
.
URL
(
url
);
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
(
java
.
net
.
HttpURLConnection
)
connURL
.
openConnection
();
// 设置通用属性
httpConn
.
setRequestProperty
(
"Accept"
,
"*/*"
);
httpConn
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
httpConn
.
setRequestProperty
(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
);
// 设置POST方式
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoOutput
(
true
);
return
httpConn
;
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
public
static
String
post
(
HttpURLConnection
httpConn
,
String
url
,
String
jsonString
)
throws
IOException
{
String
result
=
""
;
// 返回的结果
BufferedReader
in
=
null
;
// 读取响应输入流
PrintWriter
out
=
null
;
StringBuffer
sb
=
new
StringBuffer
();
// 处理请求参数
String
params
=
""
;
try
{
params
=
jsonString
;
// 获取HttpURLConnection对象对应的输出流
out
=
new
PrintWriter
(
httpConn
.
getOutputStream
());
// 发送请求参数
out
.
write
(
params
);
// flush输出流的缓冲
out
.
flush
();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
in
=
new
BufferedReader
(
new
InputStreamReader
(
httpConn
.
getInputStream
(),
"UTF-8"
));
String
line
;
// 读取返回的内容
while
((
line
=
in
.
readLine
())
!=
null
)
{
result
+=
line
;
}
}
finally
{
if
(
out
!=
null
)
{
out
.
close
();
}
if
(
in
!=
null
)
{
in
.
close
();
}
}
return
result
;
}
}
src/main/java/com/yanzuoguang/util/helper/JSONHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.TypeReference
;
/**
* JSON序列化的处理
* Created by yanzu on 2017/5/28.
*/
public
class
JSONHelper
{
/**
* 将JSON字符串转化为JSON对象
*
* @param json JSON字符串
* @param type 对象的类型
* @param <T> 转换后的结果的类型
* @return 转换后的结果
* @throws Exception 转换结果发生异常
*/
public
static
<
T
>
T
deserialize
(
String
json
,
Class
<
T
>
type
)
{
return
JSON
.
parseObject
(
json
,
type
);
}
/**
* 将JSON字符串转化为JSON对象
*
* @param json JSON字符串
* @param type 对象的类型
* @param <T> 转换后的结果的类型
* @return 转换后的结果
* @throws Exception 转换结果发生异常
*/
public
static
<
T
>
T
deserialize
(
String
json
,
TypeReference
<
T
>
type
)
{
return
JSON
.
parseObject
(
json
,
type
);
}
/**
* 将JSON字符串转换为数组
*
* @param json 需要转换的JSON
* @param type 转换的类型
* @param <T> 数组的类型
* @return 返回数组
*/
public
static
<
T
>
T
deserializeArray
(
String
json
,
TypeReference
<
T
>
type
)
{
if
(
json
==
null
||
json
.
length
()
<
1
)
{
return
null
;
}
json
=
json
.
trim
();
if
(
json
.
startsWith
(
"{"
))
{
json
=
String
.
format
(
"[%s]"
,
json
);
}
return
JSON
.
parseObject
(
json
,
type
);
}
/**
* 将对象序列化成JSON
*
* @param value 需要序列化的对象
* @return JSON结果
* @throws Exception 抛出的异常信息
*/
public
static
String
serialize
(
Object
value
)
{
return
JSON
.
toJSONString
(
value
);
}
/**
* 将对象序列化成JSON
*
* @param value 需要序列化的对象
* @param isLine 是否需要换行
* @return JSON结果
* @throws Exception 抛出的异常信息
*/
public
static
String
serialize
(
Object
value
,
boolean
isLine
)
{
return
JSON
.
toJSONString
(
value
,
isLine
);
}
}
\ No newline at end of file
src/main/java/com/yanzuoguang/util/helper/RunHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
/**
* 重复执行工具类
*/
public
class
RunHelper
{
/**
* 执行次数
*/
public
static
int
REQ_SIZE
=
3
;
/**
* 执行次数,并且获取最后一次的异常
*
* @param tag 标记
* @param run 需要执行的函数
* @param sleep 需要沉睡的时间
*/
public
static
void
run
(
String
tag
,
Runnable
run
,
int
sleep
)
{
run
(
tag
,
run
,
sleep
,
REQ_SIZE
);
}
/**
* 执行次数,并且获取最后一次的异常
*
* @param tag 标记
* @param run 需要执行的函数
* @param sleep 需要沉睡的时间
* @param size 需要执行的次数
*/
public
static
void
run
(
String
tag
,
Runnable
run
,
int
sleep
,
int
size
)
{
Exception
ex
=
null
;
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
try
{
run
.
run
();
ex
=
null
;
break
;
}
catch
(
Exception
e
)
{
if
(
i
<
size
)
{
ThreadHelper
.
sleep
(
sleep
);
// 间隔100ms,防止服务器重启时请求失败
}
ex
=
e
;
}
}
if
(
ex
!=
null
)
{
throw
new
CodeException
(
tag
+
ex
.
getMessage
(),
ex
);
}
}
}
src/main/java/com/yanzuoguang/util/helper/StringHelper.java
0 → 100644
View file @
60237a90
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/helper/ThreadHelper.java
0 → 100644
View file @
60237a90
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/helper/table/TableHead.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
.
table
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 表格头
*/
public
class
TableHead
{
/**
* 构造函数
*/
public
TableHead
()
{
this
.
TotalColumn
=
0
;
this
.
TotalRow
=
0
;
this
.
DataColumn
=
0
;
this
.
Columns
=
new
ArrayList
<>();
}
/**
* 总行数
*/
private
int
TotalRow
;
/**
* 总列数
*/
private
int
TotalColumn
;
/**
* 已添加数据列
*/
private
int
DataColumn
;
/**
* 列头数
*/
private
List
<
TableHeadItem
>
Columns
;
public
int
getTotalRow
()
{
return
TotalRow
;
}
public
void
setTotalRow
(
int
totalRow
)
{
TotalRow
=
totalRow
;
}
public
int
getTotalColumn
()
{
return
TotalColumn
;
}
public
void
setTotalColumn
(
int
totalColumn
)
{
TotalColumn
=
totalColumn
;
}
public
int
getDataColumn
()
{
return
DataColumn
;
}
public
void
setDataColumn
(
int
dataColumn
)
{
DataColumn
=
dataColumn
;
}
public
List
<
TableHeadItem
>
getColumns
()
{
return
Columns
;
}
public
void
setColumns
(
List
<
TableHeadItem
>
columns
)
{
Columns
=
columns
;
}
}
src/main/java/com/yanzuoguang/util/helper/table/TableHeadHelper.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
.
table
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* 表头计算
*/
public
class
TableHeadHelper
{
/**
* 列头函数
*
* @param columns 计算列头
* @return 返回计算结果
*/
public
static
TableHead
getTableHead
(
String
...
columns
)
{
TableHead
head
=
new
TableHead
();
head
.
setTotalColumn
(
columns
.
length
);
// 父子级别关系映射
Map
<
String
,
TableHeadItem
>
mapHead
=
new
LinkedHashMap
<
String
,
TableHeadItem
>();
// 当前节点的子节点元素
Map
<
String
,
List
<
TableHeadItem
>>
mapSub
=
new
LinkedHashMap
<
String
,
List
<
TableHeadItem
>>();
// 最顶层的元素
Map
<
String
,
TableHeadItem
>
mapTop
=
new
LinkedHashMap
<
String
,
TableHeadItem
>();
for
(
String
column
:
columns
)
{
TableHeadItem
item
=
setHead
(
mapHead
,
mapSub
,
mapTop
,
column
);
// 设置表格的最大行数
head
.
setTotalRow
(
Math
.
max
(
item
.
getLevel
(),
head
.
getTotalRow
()));
}
// 计算合并行的数量,第几行
for
(
Map
.
Entry
<
String
,
TableHeadItem
>
item
:
mapHead
.
entrySet
())
{
setCellRow
(
head
,
item
.
getValue
());
}
// 计算列合并的数量,第几列
for
(
Map
.
Entry
<
String
,
TableHeadItem
>
item
:
mapTop
.
entrySet
())
{
setCellColumn
(
head
,
mapSub
,
item
.
getValue
());
}
return
head
;
}
private
static
TableHeadItem
setHead
(
Map
<
String
,
TableHeadItem
>
mapHead
,
Map
<
String
,
List
<
TableHeadItem
>>
mapSub
,
Map
<
String
,
TableHeadItem
>
mapTop
,
String
columnFrom
)
{
String
[]
columns
=
columnFrom
.
split
(
":"
);
StringBuilder
path
=
new
StringBuilder
();
TableHeadItem
last
=
null
;
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
// 列名称
String
column
=
columns
[
i
];
// 父路径与当前节点路径
String
parentPath
=
path
.
toString
();
if
(
i
>
0
)
{
path
.
append
(
":"
);
}
path
.
append
(
column
);
String
nowPath
=
path
.
toString
();
last
=
new
TableHeadItem
();
last
.
setName
(
column
);
last
.
setPath
(
nowPath
);
last
.
setLevel
(
i
+
1
);
last
.
setDataColumn
(
columnFrom
.
equals
(
nowPath
));
if
(
i
==
0
&&
!
mapTop
.
containsKey
(
nowPath
))
{
mapTop
.
put
(
nowPath
,
last
);
}
// 添加到最顶层元素中
if
(!
mapSub
.
containsKey
(
parentPath
))
{
mapSub
.
put
(
parentPath
,
new
ArrayList
<
TableHeadItem
>());
}
// 将元素添加到当前元素中
if
(!
mapHead
.
containsKey
(
nowPath
))
{
mapHead
.
put
(
nowPath
,
last
);
// 添加到父级元素的下级元素
mapSub
.
get
(
parentPath
).
add
(
last
);
}
last
=
mapHead
.
get
(
nowPath
);
last
.
setLevelMax
(
Math
.
max
(
last
.
getLevelMax
(),
columns
.
length
));
}
return
last
;
}
private
static
void
setCellRow
(
TableHead
head
,
TableHeadItem
value
)
{
// 合并行数量
int
rowCell
=
head
.
getTotalRow
()
-
value
.
getLevelMax
()
+
1
;
value
.
setRow
(
value
.
getLevel
());
value
.
setRowCell
(
1
);
// 计算第几行,合并几行
if
(
value
.
getLevel
()
==
1
)
{
// 第一行的规则
if
(
value
.
getLevelMax
()
==
1
)
{
value
.
setRowCell
(
rowCell
);
}
}
else
if
(
value
.
getLevel
()
==
value
.
getLevelMax
())
{
// 最后一行的规则
if
(
value
.
getLevelMax
()
==
2
)
{
value
.
setRowCell
(
rowCell
);
}
else
{
value
.
setRow
(
head
.
getTotalRow
());
}
}
else
{
// 其他行
if
(
value
.
getLevel
()
==
value
.
getLevelMax
()
-
1
)
{
value
.
setRowCell
(
rowCell
);
}
}
}
private
static
void
setCellColumn
(
TableHead
head
,
Map
<
String
,
List
<
TableHeadItem
>>
mapSub
,
TableHeadItem
value
)
{
// 设置当前列号
value
.
setColumn
(
head
.
getDataColumn
()
+
1
);
// 设置默认合并单元格数量
value
.
setColumnCell
(
1
);
// 将列添加到当前列集合中
head
.
getColumns
().
add
(
value
);
// 计算合并几列
if
(
value
.
isDataColumn
())
{
head
.
setDataColumn
(
head
.
getDataColumn
()
+
1
);
}
// 假如有子节点,则合并的单元格等于子节点的想加
List
<
TableHeadItem
>
subColumns
=
mapSub
.
containsKey
(
value
.
getPath
())
?
mapSub
.
get
(
value
.
getPath
())
:
new
ArrayList
<
TableHeadItem
>();
if
(
subColumns
!=
null
&&
subColumns
.
size
()
>
0
)
{
value
.
setColumnCell
(
0
);
for
(
TableHeadItem
subColumn
:
subColumns
)
{
setCellColumn
(
head
,
mapSub
,
subColumn
);
value
.
setColumnCell
(
subColumn
.
getColumnCell
());
}
}
}
}
src/main/java/com/yanzuoguang/util/helper/table/TableHeadItem.java
0 → 100644
View file @
60237a90
package
com
.
yanzuoguang
.
util
.
helper
.
table
;
/**
* 列头
*/
public
class
TableHeadItem
{
/**
* 最高级别
*/
private
String
Name
;
/**
* 路径
*/
private
String
Path
;
/**
* 级别
*/
private
int
Level
;
/**
* 当前列的最大级别
*/
private
int
LevelMax
;
/**
* 第几行
*/
private
int
Row
;
/**
* 合并多少行
*/
private
int
RowCell
;
/**
* 第几列
*/
private
int
Column
;
/**
* 合并多少列
*/
private
int
ColumnCell
;
/**
* 是否属于数据列
*/
private
boolean
dataColumn
;
public
String
getName
()
{
return
Name
;
}
public
void
setName
(
String
name
)
{
Name
=
name
;
}
public
String
getPath
()
{
return
Path
;
}
public
void
setPath
(
String
path
)
{
Path
=
path
;
}
public
int
getLevel
()
{
return
Level
;
}
public
void
setLevel
(
int
level
)
{
Level
=
level
;
}
public
int
getLevelMax
()
{
return
LevelMax
;
}
public
void
setLevelMax
(
int
levelMax
)
{
LevelMax
=
levelMax
;
}
public
int
getRow
()
{
return
Row
;
}
public
void
setRow
(
int
row
)
{
Row
=
row
;
}
public
int
getRowCell
()
{
return
RowCell
;
}
public
void
setRowCell
(
int
rowCell
)
{
RowCell
=
rowCell
;
}
public
int
getColumn
()
{
return
Column
;
}
public
void
setColumn
(
int
column
)
{
Column
=
column
;
}
public
int
getColumnCell
()
{
return
ColumnCell
;
}
public
void
setColumnCell
(
int
columnCell
)
{
ColumnCell
=
columnCell
;
}
public
boolean
isDataColumn
()
{
return
dataColumn
;
}
public
void
setDataColumn
(
boolean
dataColumn
)
{
dataColumn
=
dataColumn
;
}
}
src/main/java/com/yanzuoguang/util/vo/BaseVo.java
View file @
60237a90
...
...
@@ -21,6 +21,6 @@
// */
// @Override
// public String toString() {
// return JSONHelper.
SerializeObject
(this);
// return JSONHelper.
serialize
(this);
// }
//}
src/test/java/helper/TestByteHelper.java
0 → 100644
View file @
60237a90
package
helper
;
import
com.yanzuoguang.util.helper.ByteHelper
;
import
org.junit.Assert
;
import
org.junit.Test
;
public
class
TestByteHelper
{
@Test
public
void
test
()
{
long
from
=
0x0f0f
;
// from = 1999;
System
.
out
.
println
(
from
);
System
.
out
.
println
(
ByteHelper
.
reverse
(
from
));
System
.
out
.
println
(
ByteHelper
.
reverse
(
ByteHelper
.
reverse
(
from
)));
System
.
out
.
println
(
ByteHelper
.
toHexString
(
ByteHelper
.
toHL
(
from
)));
System
.
out
.
println
(
ByteHelper
.
toHexString
(
ByteHelper
.
toHL
(
from
)));
System
.
out
.
println
(
ByteHelper
.
toHexString
(
ByteHelper
.
toLH
(
from
)));
System
.
out
.
println
(
ByteHelper
.
toLongByHL
(
ByteHelper
.
toHL
(
from
)));
System
.
out
.
println
(
ByteHelper
.
toLongByLH
(
ByteHelper
.
toLH
(
from
)));
}
@Test
public
void
test1
()
{
long
old
=
1999
;
old
=
0x0f0f
;
long
from
=
ByteHelper
.
reverse
(
old
);
long
to
=
ByteHelper
.
reverse
(
from
);
Assert
.
assertEquals
(
old
,
to
);
System
.
out
.
println
(
String
.
format
(
"old:%d %d %d"
,
old
,
from
,
to
));
}
}
src/test/java/helper/TestDateAutoHelper.java
0 → 100644
View file @
60237a90
package
helper
;
import
com.yanzuoguang.util.helper.DateAutoHelper
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.text.ParseException
;
public
class
TestDateAutoHelper
{
@Test
public
void
testGetAutoDate
()
throws
ParseException
{
Assert
.
assertEquals
(
"Wed Nov 04 00:00:00 CST 1987"
,
"Wed Nov 04 00:00:00 CST 1987"
);
Assert
.
assertEquals
(
"Wed Nov 04 00:00:00 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"1987-11-04"
).
toString
());
Assert
.
assertEquals
(
"Wed Nov 04 12:50:00 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"1987-11-04 12:50"
).
toString
());
Assert
.
assertEquals
(
"Wed Nov 04 12:50:15 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"1987-11-04 12:50:15"
).
toString
());
Assert
.
assertEquals
(
"Wed Nov 04 12:50:15 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"1987-11-04 12:50:15.000"
).
toString
());
Assert
.
assertEquals
(
"Wed Nov 04 00:00:00 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"19871104"
).
toString
());
Assert
.
assertEquals
(
"Wed Nov 04 00:00:00 CST 1987"
,
DateAutoHelper
.
getAutoDate
(
"04/11/1987"
).
toString
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment