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
28f668d3
Commit
28f668d3
authored
Jul 20, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
视频压缩
parent
cfaac590
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
430 additions
and
15 deletions
+430
-15
CheckerHelper.java
.../main/java/com/yanzuoguang/util/helper/CheckerHelper.java
+59
-15
YzgLanguage.java
...rc/main/java/com/yanzuoguang/util/helper/YzgLanguage.java
+321
-0
CheckHelperTest.java
yzg-util-base/src/test/java/helper/CheckHelperTest.java
+50
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/CheckerHelper.java
View file @
28f668d3
...
...
@@ -16,22 +16,66 @@ import java.util.regex.Pattern;
*/
public
final
class
CheckerHelper
{
public
static
final
String
PARAM_NOT_NULL_TIPS
=
"param [%s] is null"
;
public
static
final
String
PARAM_NOT_NULL_TIPS_CH
=
"参数[%s]不能为空"
;
public
static
final
String
PARAM_NOT_EMPTY_TIPS
=
"param [%s] is empty"
;
public
static
final
String
PARAM_NOT_EMPTY_TIPS_CH
=
"参数[%s]不能为空"
;
public
static
final
String
PARAM_NOT_NUMBER
=
"param [%s] value [%s] must be number"
;
public
static
final
String
PARAM_OVER_MAX_LENGTH
=
"param [%s] maxlength is "
;
public
static
final
String
PARAM_NOT_NUMBER_CH
=
"参数[%s]的值[%s]必须为数字"
;
public
static
final
String
PARAM_OVER_MAX_LENGTH
=
"param [%s] max length is %d"
;
public
static
final
String
PARAM_OVER_MAX_LENGTH_CH
=
"参数[%s]长度不能大于%d"
;
public
static
final
String
PARAM_NOT_DATE_MONTH
=
"param [%s] must be yyyyMM"
;
public
static
final
String
PARAM_NOT_DATE_MONTH_CH
=
"参数[%s]的个是必须yyyyMM"
;
public
static
final
String
PARAM_NOT_DATE_DAY
=
"param [%s] must be yyyyMMdd "
;
public
static
final
String
PARAM_NOT_DATE_DAY_CH
=
"参数[%s]的格式必须为yyyyMMdd"
;
public
static
final
String
PARAM_NOT_DATE
=
"param [%s] must be [%s]"
;
public
static
final
String
PARAM_NOT_DATE_CH
=
"参数[%s]的格式必须为[%s]"
;
public
static
final
String
PARAM_NOT_PHONE
=
"param [%s] must be phone no"
;
public
static
final
String
PARAM_NOT_PHONE_CH
=
"参数[%s]必须为电话号码"
;
public
static
final
String
PARAM_NOT_ID_CARD
=
"param [%s] must be id card"
;
public
static
final
String
PARAM_NOT_ID_CARD_CH
=
"参数[%s]必须为身份证"
;
public
static
final
String
PARAM_NOT_MIN
=
"param [%s] must be big %d"
;
public
static
final
String
PARAM_NOT_MIN_CH
=
"参数[%s]和必须大于%d"
;
public
static
final
String
PARAM_NOT_MAX
=
"param [%s] must be max %d"
;
public
static
final
String
PARAM_NOT_MAX_CH
=
"param [%s] must be max %d"
;
public
static
final
String
PARAM_NOT_RANGE
=
"param [%s] must be range %d and %d"
;
public
static
final
String
PARAM_NOT_RANGE_CH
=
"参数[%s]请在范围%d至%d内"
;
public
static
final
String
DATE_CHECK_TYPE_DAY
=
"day"
;
public
static
final
String
DATE_CHECK_TYPE_DAY_CH
=
"日期"
;
public
static
final
String
DATE_CHECK_TYPE_MONTH
=
"month"
;
public
static
final
String
DATE_CHECK_TYPE_MONTH_CH
=
"月份"
;
private
static
YzgLanguage
language
=
YzgLanguage
.
get
();
static
{
language
.
setEnglishChinese
(
PARAM_NOT_NULL_TIPS
,
PARAM_NOT_NULL_TIPS_CH
)
.
setEnglishChinese
(
PARAM_NOT_EMPTY_TIPS
,
PARAM_NOT_EMPTY_TIPS_CH
)
.
setEnglishChinese
(
PARAM_NOT_NUMBER
,
PARAM_NOT_NUMBER_CH
)
.
setEnglishChinese
(
PARAM_OVER_MAX_LENGTH
,
PARAM_OVER_MAX_LENGTH_CH
)
.
setEnglishChinese
(
PARAM_NOT_DATE_MONTH
,
PARAM_NOT_DATE_MONTH_CH
)
.
setEnglishChinese
(
PARAM_NOT_DATE_DAY
,
PARAM_NOT_DATE_DAY_CH
)
.
setEnglishChinese
(
PARAM_NOT_DATE
,
PARAM_NOT_DATE_CH
)
.
setEnglishChinese
(
PARAM_NOT_PHONE
,
PARAM_NOT_PHONE_CH
)
.
setEnglishChinese
(
PARAM_NOT_ID_CARD
,
PARAM_NOT_ID_CARD_CH
)
.
setEnglishChinese
(
PARAM_NOT_MIN
,
PARAM_NOT_MIN_CH
)
.
setEnglishChinese
(
PARAM_NOT_MAX
,
PARAM_NOT_MAX_CH
)
.
setEnglishChinese
(
PARAM_NOT_RANGE
,
PARAM_NOT_RANGE_CH
)
.
setEnglishChinese
(
DATE_CHECK_TYPE_DAY
,
DATE_CHECK_TYPE_DAY_CH
)
.
setEnglishChinese
(
DATE_CHECK_TYPE_MONTH
,
DATE_CHECK_TYPE_MONTH_CH
)
;
}
private
boolean
isValid
=
true
;
private
String
checkResult
;
...
...
@@ -64,7 +108,7 @@ public final class CheckerHelper {
return
this
;
}
if
(
paramVal
==
null
)
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_NULL_TIPS
,
paramName
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_NULL_TIPS
,
paramName
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -82,7 +126,7 @@ public final class CheckerHelper {
return
this
;
}
if
(
StringHelper
.
isEmpty
(
paramVal
))
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_EMPTY_TIPS
,
paramName
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_EMPTY_TIPS
,
paramName
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -101,7 +145,7 @@ public final class CheckerHelper {
}
if
(
null
==
list
||
list
.
isEmpty
())
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_NULL_TIPS
,
paramName
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_NULL_TIPS
,
paramName
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -119,7 +163,7 @@ public final class CheckerHelper {
return
this
;
}
if
(!
StringHelper
.
isNumber
(
paramVal
))
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -137,7 +181,7 @@ public final class CheckerHelper {
return
this
;
}
if
(!
StringHelper
.
isEmpty
(
paramVal
)
&&
!
StringHelper
.
isNumber
(
paramVal
))
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -156,7 +200,7 @@ public final class CheckerHelper {
}
String
rexp
=
"([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"
;
if
(!
StringHelper
.
isEmpty
(
paramVal
)
&&
!
paramVal
.
matches
(
rexp
))
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_NUMBER
,
paramName
,
paramVal
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -191,7 +235,7 @@ public final class CheckerHelper {
Matcher
m
=
p
.
matcher
(
paramVal
);
boolean
dateFlag
=
m
.
matches
();
if
(!
StringHelper
.
isEmpty
(
paramVal
)
&&
!
dateFlag
)
{
this
.
checkResult
=
String
.
format
(
msg
,
paramName
,
paramVal
);
this
.
checkResult
=
language
.
format
(
msg
,
paramName
,
paramVal
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -214,7 +258,7 @@ public final class CheckerHelper {
String
regExp
=
"^1\\d{10}$"
;
Pattern
p
=
Pattern
.
compile
(
regExp
);
if
(!
p
.
matcher
(
paramVal
).
matches
())
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_PHONE
,
paramName
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_PHONE
,
paramName
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -233,7 +277,7 @@ public final class CheckerHelper {
return
this
;
}
if
(!
StringHelper
.
isEmpty
(
paramVal
)
&&
length
<
paramVal
.
length
())
{
this
.
checkResult
=
String
.
format
(
PARAM_OVER_MAX_LENGTH
,
paramName
)
+
length
;
this
.
checkResult
=
language
.
format
(
PARAM_OVER_MAX_LENGTH
,
paramName
,
length
)
;
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -273,7 +317,7 @@ public final class CheckerHelper {
DateAutoHelper
.
getAutoDate
(
paramVal
);
}
}
catch
(
ParseException
e
)
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_DATE
,
paramName
,
formatStr
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_DATE
,
paramName
,
formatStr
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -296,7 +340,7 @@ public final class CheckerHelper {
String
regExp
=
"^\\d{6}(18|19|20)?\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|X)$"
;
Pattern
p
=
Pattern
.
compile
(
regExp
);
if
(!
p
.
matcher
(
paramVal
.
toUpperCase
()).
matches
())
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_ID_CARD
,
paramName
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_ID_CARD
,
paramName
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -326,7 +370,7 @@ public final class CheckerHelper {
return
this
;
}
if
(
paramVal
<
min
)
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_MIN
,
paramName
,
min
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_MIN
,
paramName
,
min
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -345,7 +389,7 @@ public final class CheckerHelper {
return
this
;
}
if
(
paramVal
>
max
)
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_MAX
,
paramName
,
max
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_MAX
,
paramName
,
max
);
this
.
setValid
(
false
);
}
return
this
;
...
...
@@ -365,7 +409,7 @@ public final class CheckerHelper {
return
this
;
}
if
(
paramVal
>
max
||
paramVal
<
min
)
{
this
.
checkResult
=
String
.
format
(
PARAM_NOT_RANGE
,
paramName
,
min
,
max
);
this
.
checkResult
=
language
.
format
(
PARAM_NOT_RANGE
,
paramName
,
min
,
max
);
this
.
setValid
(
false
);
}
return
this
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/YzgLanguage.java
0 → 100644
View file @
28f668d3
package
com
.
yanzuoguang
.
util
.
helper
;
import
java.util.*
;
/**
* 设置提示语言
*/
public
class
YzgLanguage
{
private
static
final
YzgLanguage
_my
=
new
YzgLanguage
();
public
static
YzgLanguage
get
()
{
return
_my
;
}
/**
* 私有构造函数
*/
private
YzgLanguage
()
{
}
/**
* 默认场景
*/
public
static
final
String
SCENE_DEFAULT
=
"DEFAULT"
;
/**
* 英语
*/
public
static
final
String
LANGUAGE_EN
=
"en"
;
/**
* 中文
*/
public
static
final
String
LANGUAGE_CH
=
"ch"
;
/**
* 整个系统默认语言
*/
private
String
defaulLanguage
=
LANGUAGE_EN
;
/**
* 当前线程默认语言
*/
private
ThreadLocal
<
String
>
threadLanguage
=
new
ThreadLocal
<>();
/**
* 当前线程默认场景
*/
private
ThreadLocal
<
String
>
threadScene
=
new
ThreadLocal
<>();
/**
* 场景语言
*/
private
Map
<
String
,
Map
<
String
,
Map
<
String
,
String
>>>
mapScene
=
new
HashMap
<>();
/**
* 设置默认语言,默认为英语(en)
*
* @param defaulLanguage
*/
public
YzgLanguage
setDefaulLanguage
(
String
defaulLanguage
)
{
this
.
defaulLanguage
=
defaulLanguage
;
return
this
;
}
/**
* 获取默认语言
*
* @return
*/
public
String
getDefaulLanguage
()
{
return
defaulLanguage
;
}
/**
* 设置当前线程语言
*
* @param language
* @return
*/
public
YzgLanguage
setThreadLanguage
(
String
language
)
{
this
.
threadLanguage
.
set
(
language
);
return
this
;
}
/**
* 移除当前线程语言
*
* @return
*/
public
YzgLanguage
removeThreadLanguage
()
{
this
.
threadLanguage
.
remove
();
return
this
;
}
/**
* 获取默认语言
*
* @return
*/
public
String
getLanguage
()
{
return
StringHelper
.
getFirst
(
this
.
threadLanguage
.
get
(),
defaulLanguage
);
}
/**
* 设置当前线程语言
*
* @param scene
* @return
*/
public
YzgLanguage
setThreadScene
(
String
scene
)
{
this
.
threadScene
.
set
(
scene
);
return
this
;
}
/**
* 移除当前线程语言
*
* @return
*/
public
YzgLanguage
removeThreadScene
()
{
this
.
threadScene
.
remove
();
return
this
;
}
/**
* 获取默认场景
*
* @return
*/
public
String
getScene
()
{
return
StringHelper
.
getFirst
(
this
.
threadScene
.
get
(),
SCENE_DEFAULT
);
}
/**
* 县城安全获取子map
*
* @param mapFrom
* @param key
* @param <T>
* @return
*/
private
<
T
extends
Object
>
Map
<
String
,
T
>
getSafeValue
(
Map
<
String
,
Map
<
String
,
T
>>
mapFrom
,
String
key
)
{
Map
<
String
,
T
>
ret
=
mapFrom
.
get
(
key
);
if
(
ret
==
null
)
{
synchronized
(
mapFrom
)
{
ret
=
mapFrom
.
get
(
key
);
if
(
ret
==
null
)
{
ret
=
new
HashMap
<>();
mapFrom
.
put
(
key
,
ret
);
}
}
}
return
ret
;
}
/**
* 获取默认语言对象
*
* @param sceneName 场景
* @param defaultLanguageValue 语言
*/
private
Map
<
String
,
String
>
getDefaultFrom
(
String
sceneName
,
String
defaultLanguageValue
)
{
// 获取场景
Map
<
String
,
Map
<
String
,
String
>>
scene
=
getSafeValue
(
mapScene
,
sceneName
);
// 默认语言key
String
defaultKey
=
StringHelper
.
md5
(
defaultLanguageValue
);
// 获取语言对应关系
Map
<
String
,
String
>
defaultFrom
=
getSafeValue
(
scene
,
defaultKey
);
return
defaultFrom
;
}
/**
* 设置场景语言
*
* @param sceneName 场景名称
* @param defaultLanguageValue 默认语言值
* @param language 语言
* @param languageValue 语言值
*/
public
YzgLanguage
set
(
String
sceneName
,
String
defaultLanguageValue
,
String
language
,
String
languageValue
)
{
// 获取语言对应关系
Map
<
String
,
String
>
defaultFrom
=
getDefaultFrom
(
sceneName
,
defaultLanguageValue
);
// 设置默认语言
defaultFrom
.
put
(
defaulLanguage
,
defaultLanguageValue
);
// 设置语言
defaultFrom
.
put
(
language
,
languageValue
);
return
this
;
}
/**
* 设置场景语言
*
* @param defaultLanguageValue 默认语言值
* @param language 语言
* @param languageValue 语言值
*/
public
YzgLanguage
set
(
String
defaultLanguageValue
,
String
language
,
String
languageValue
)
{
return
this
.
set
(
getScene
(),
defaultLanguageValue
,
language
,
languageValue
);
}
/**
* 获取语言关系
*
* @param sceneName 场景名称
* @param defaultLanguageValue 默认语言值
* @param language 语言
* @return
*/
public
String
get
(
String
sceneName
,
String
defaultLanguageValue
,
String
language
)
{
// 获取语言对应关系
Map
<
String
,
String
>
defaultFrom
=
getDefaultFrom
(
sceneName
,
defaultLanguageValue
);
// 获取对应语言
String
ret
=
defaultFrom
.
get
(
language
);
if
(!
StringHelper
.
isEmpty
(
ret
))
{
return
ret
;
}
// 获取语言对应关系
Map
<
String
,
String
>
globalFrom
=
getDefaultFrom
(
SCENE_DEFAULT
,
defaultLanguageValue
);
// 获取对应语言
ret
=
globalFrom
.
get
(
language
);
if
(!
StringHelper
.
isEmpty
(
ret
))
{
return
ret
;
}
// 返回默认值
return
defaultLanguageValue
;
}
/**
* 获取语言关系
*
* @param defaultLanguageValue 默认语言值
* @param language 语言
* @return
*/
public
String
get
(
String
defaultLanguageValue
,
String
language
)
{
return
this
.
get
(
getScene
(),
defaultLanguageValue
,
language
);
}
/**
* 设置英文_中文对应
*
* @param enValue
* @param chValue
*/
public
YzgLanguage
setEnglishChinese
(
String
enValue
,
String
chValue
)
{
return
this
.
set
(
enValue
,
LANGUAGE_CH
,
chValue
);
}
/**
* 设置英文_中文对应
*
* @param scene 场景
* @param enValue
* @param chValue
*/
public
YzgLanguage
setEnglishChinese
(
String
scene
,
String
enValue
,
String
chValue
)
{
return
this
.
set
(
scene
,
enValue
,
LANGUAGE_CH
,
chValue
);
}
/**
* 获取英文_中文对应
*
* @param enValue
*/
public
String
getEnglishChinese
(
String
enValue
)
{
return
this
.
get
(
enValue
,
LANGUAGE_CH
);
}
/**
* 获取英文_中文对应
*
* @param scene 场景
* @param enValue
*/
public
String
getEnglishChinese
(
String
scene
,
String
enValue
)
{
return
this
.
get
(
scene
,
enValue
,
LANGUAGE_CH
);
}
/**
* 获取语言对应提示
*
* @param sceneName 场景
* @param format 格式化
* @param paras 参数
* @return
*/
public
String
formatScene
(
String
sceneName
,
String
format
,
Object
...
paras
)
{
String
lan
=
getLanguage
();
format
=
get
(
sceneName
,
format
,
lan
);
List
<
Object
>
list
=
new
ArrayList
<>();
if
(
paras
!=
null
)
{
for
(
int
i
=
0
;
i
<
paras
.
length
;
i
++)
{
Object
fromValue
=
paras
[
i
];
if
(
fromValue
instanceof
String
&&
fromValue
!=
null
)
{
String
from
=
get
(
sceneName
,
String
.
valueOf
(
fromValue
),
lan
);
list
.
add
(
from
);
}
else
{
list
.
add
(
fromValue
);
}
}
}
return
String
.
format
(
format
,
list
.
toArray
());
}
/**
* 获取语言对应提示
*
* @param format 格式化
* @param paras 参数
* @return
*/
public
String
format
(
String
format
,
Object
...
paras
)
{
return
this
.
formatScene
(
getScene
(),
format
,
paras
);
}
}
yzg-util-base/src/test/java/helper/CheckHelperTest.java
0 → 100644
View file @
28f668d3
package
helper
;
import
com.yanzuoguang.util.helper.CheckerHelper
;
import
com.yanzuoguang.util.helper.YzgLanguage
;
import
org.junit.Test
;
public
class
CheckHelperTest
{
static
{
CheckerHelper
.
newInstance
();
}
@Test
public
void
testEn
()
{
CheckerHelper
.
newInstance
()
.
notBlankCheck
(
"name"
,
null
)
.
checkException
();
}
@Test
public
void
testCh
()
{
// 设置默认语言为中文
YzgLanguage
language
=
YzgLanguage
.
get
();
language
.
setDefaulLanguage
(
YzgLanguage
.
LANGUAGE_CH
);
language
.
setEnglishChinese
(
"name"
,
"名称"
);
testEn
();
// 设置默认语言为英文
language
.
setDefaulLanguage
(
YzgLanguage
.
LANGUAGE_EN
);
}
@Test
public
void
testChScene
()
{
// 设置默认语言为中文
YzgLanguage
language
=
YzgLanguage
.
get
();
language
.
setDefaulLanguage
(
YzgLanguage
.
LANGUAGE_CH
);
language
.
setEnglishChinese
(
"name"
,
"名称"
);
String
scene1
=
"用户检测"
;
language
.
setEnglishChinese
(
scene1
,
"name"
,
"姓名"
);
language
.
setThreadScene
(
scene1
);
testEn
();
// 设置默认语言为英文
language
.
setDefaulLanguage
(
YzgLanguage
.
LANGUAGE_EN
);
}
}
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