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
56179770
Commit
56179770
authored
May 12, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复等待时间
parent
2a0ecb83
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
379 additions
and
3 deletions
+379
-3
CollectionString.java
...main/java/com/yanzuoguang/util/base/CollectionString.java
+124
-0
ObjectEach.java
...e/src/main/java/com/yanzuoguang/util/base/ObjectEach.java
+56
-0
ObjectEachItem.java
...c/main/java/com/yanzuoguang/util/base/ObjectEachItem.java
+45
-0
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+2
-2
DemoVo.java
yzg-util-base/src/test/java/base/DemoVo.java
+8
-0
TestCollectionString.java
yzg-util-base/src/test/java/helper/TestCollectionString.java
+38
-0
AspectLogStart.java
...c/main/java/com/yanzuoguang/cloud/aop/AspectLogStart.java
+0
-1
AspectLogTime.java
...rc/main/java/com/yanzuoguang/cloud/aop/AspectLogTime.java
+38
-0
AspectUrlCountVo.java
...main/java/com/yanzuoguang/cloud/aop/AspectUrlCountVo.java
+68
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/CollectionString.java
0 → 100644
View file @
56179770
package
com
.
yanzuoguang
.
util
.
base
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 集合转换为表格字符串
*
* @author 颜佐光
*/
public
class
CollectionString
{
private
static
final
String
SPLIT
=
"|"
;
private
static
final
String
EMPTY
=
" "
;
private
static
final
String
ROW
=
"-"
;
private
static
final
int
EMPTY_SIZE
=
2
;
/**
* 集合转换为表格字符串
*
* @param rowList 需要转换的集合
* @param <T> 集合类型
* @return 表格字符串
*/
public
static
<
T
>
String
getCollectionString
(
String
tag
,
Collection
<
T
>
rowList
)
{
// 每个字段的长度
Map
<
String
,
Integer
>
mapFieldCount
=
new
HashMap
<>(
15
);
// 每个值的长度
rowList
.
forEach
(
k
->
ObjectEach
.
each
(
k
,
(
name
,
value
,
field
)
->
{
// 获取字符串
String
val
=
StringHelper
.
getFirst
(
StringHelper
.
toString
(
value
));
int
len
=
Math
.
max
(
length
(
name
),
length
(
val
));
Integer
hisLen
=
mapFieldCount
.
getOrDefault
(
name
,
len
);
int
maxLen
=
Math
.
max
(
len
,
hisLen
);
mapFieldCount
.
put
(
name
,
maxLen
);
}));
// 生成结果
StringBuilder
sbResult
=
new
StringBuilder
();
sbResult
.
append
(
tag
);
appendEmptyRow
(
sbResult
,
mapFieldCount
);
sbResult
.
append
(
System
.
lineSeparator
());
sbResult
.
append
(
SPLIT
);
mapFieldCount
.
forEach
((
name
,
len
)
->
{
int
nameLen
=
length
(
name
);
appendChar
(
sbResult
,
EMPTY_SIZE
,
EMPTY
);
sbResult
.
append
(
name
);
appendChar
(
sbResult
,
len
-
nameLen
+
EMPTY_SIZE
,
EMPTY
);
sbResult
.
append
(
SPLIT
);
});
appendEmptyRow
(
sbResult
,
mapFieldCount
);
// 遍历每一行
rowList
.
forEach
(
row
->
{
sbResult
.
append
(
System
.
lineSeparator
());
sbResult
.
append
(
SPLIT
);
mapFieldCount
.
forEach
((
name
,
len
)
->
{
appendChar
(
sbResult
,
EMPTY_SIZE
,
EMPTY
);
// 获取字符串
String
value
=
StringHelper
.
getFirst
(
ObjectHelper
.
getString
(
row
,
name
));
sbResult
.
append
(
value
);
appendChar
(
sbResult
,
len
-
length
(
value
)
+
EMPTY_SIZE
,
EMPTY
);
sbResult
.
append
(
SPLIT
);
});
});
appendEmptyRow
(
sbResult
,
mapFieldCount
);
return
sbResult
.
toString
();
}
/**
* 添加空行
*
* @param sbResult
* @param mapFieldCount
*/
private
static
void
appendEmptyRow
(
StringBuilder
sbResult
,
Map
<
String
,
Integer
>
mapFieldCount
)
{
if
(
sbResult
.
length
()
>
0
)
{
sbResult
.
append
(
System
.
lineSeparator
());
}
sbResult
.
append
(
SPLIT
);
mapFieldCount
.
forEach
((
name
,
len
)
->
{
appendChar
(
sbResult
,
len
+
EMPTY_SIZE
+
EMPTY_SIZE
,
ROW
);
sbResult
.
append
(
SPLIT
);
});
}
/**
* 添加字符
*
* @param sb 字符串
* @param len 长度
* @param ch 字符
*/
private
static
void
appendChar
(
StringBuilder
sb
,
int
len
,
String
ch
)
{
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
sb
.
append
(
ch
);
}
}
/**
* 获取字符串的长度,如果有中⽂,则每个中⽂字符计为2位
*
* @param value 指定的字符串
* @return 字符串的长度
*/
public
static
int
length
(
String
value
)
{
int
valueLength
=
0
;
String
chinese
=
"[\u0391-\uFFE5]"
;
/* 获取字段值的长度,如果含中⽂字符,则每个中⽂字符长度为2,否则为1 */
for
(
int
i
=
0
;
i
<
value
.
length
();
i
++)
{
/* 获取⼀个字符 */
String
temp
=
value
.
substring
(
i
,
i
+
1
);
/* 判断是否为中⽂字符 */
if
(
temp
.
matches
(
chinese
))
{
/* 中⽂字符长度为2 */
valueLength
+=
2
;
}
else
{
/* 其他字符长度为1 */
valueLength
+=
1
;
}
}
return
valueLength
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectEach.java
0 → 100644
View file @
56179770
package
com
.
yanzuoguang
.
util
.
base
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 遍历对象和值
*
* @author 颜佐光
*/
public
class
ObjectEach
{
/**
* 遍历对象
*
* @param target 对象
* @param consumer 消费者,其中包含名称,值,方法(map时为空)
*/
public
static
void
each
(
Object
target
,
ObjectEachItem
<
String
,
Object
,
MethodField
>
consumer
)
{
if
(
target
instanceof
Map
)
{
eachMap
((
Map
)
target
,
consumer
);
}
else
{
eachObject
(
target
,
consumer
);
}
}
/**
* 遍历map对象
*
* @param target 目标对象
* @param consumer 遍历的字段,值,方法
*/
private
static
void
eachMap
(
Map
target
,
ObjectEachItem
<
String
,
Object
,
MethodField
>
consumer
)
{
for
(
Object
key
:
target
.
keySet
())
{
Object
fromValue
=
target
.
get
(
key
);
consumer
.
accept
(
StringHelper
.
toString
(
key
),
fromValue
,
null
);
}
}
/**
* 遍历对象
*
* @param target 目标对象
* @param consumer 遍历的字段,值,方法
*/
private
static
void
eachObject
(
Object
target
,
ObjectEachItem
<
String
,
Object
,
MethodField
>
consumer
)
{
HashMap
<
String
,
MethodField
>
mapField
=
ObjectHelper
.
getTypeField
(
target
.
getClass
());
for
(
Map
.
Entry
<
String
,
MethodField
>
field
:
mapField
.
entrySet
())
{
String
name
=
field
.
getValue
().
getName
();
Object
fromValue
=
ObjectHelper
.
get
(
target
,
name
);
consumer
.
accept
(
name
,
fromValue
,
field
.
getValue
());
}
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectEachItem.java
0 → 100644
View file @
56179770
package
com
.
yanzuoguang
.
util
.
base
;
import
java.util.Objects
;
/**
* 处理3个参数的函数接口
*
* @param <H> the input argument
* @param <I> the input argument
* @param <J> the input argument
* @author 颜佐光
*/
@FunctionalInterface
public
interface
ObjectEachItem
<
H
,
I
,
J
>
{
/**
* Performs this operation on the given argument.
*
* @param h the input argument
* @param i the input argument
* @param j the input argument
*/
void
accept
(
H
h
,
I
i
,
J
j
);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default
ObjectEachItem
<
H
,
I
,
J
>
andThen
(
ObjectEachItem
<?
super
H
,
?
super
I
,
?
super
J
>
after
)
{
Objects
.
requireNonNull
(
after
);
return
(
H
h
,
I
i
,
J
j
)
->
{
accept
(
h
,
i
,
j
);
after
.
accept
(
h
,
i
,
j
);
};
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
56179770
...
@@ -470,7 +470,7 @@ public class ObjectHelper {
...
@@ -470,7 +470,7 @@ public class ObjectHelper {
if
(
from
==
null
||
to
==
null
)
{
if
(
from
==
null
||
to
==
null
)
{
return
to
;
return
to
;
}
}
HashMap
<
String
,
MethodField
>
mapField
=
get
Init
TypeField
(
to
.
getClass
());
HashMap
<
String
,
MethodField
>
mapField
=
getTypeField
(
to
.
getClass
());
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
}
}
...
@@ -568,7 +568,7 @@ public class ObjectHelper {
...
@@ -568,7 +568,7 @@ public class ObjectHelper {
if
(
from
==
null
||
to
==
null
)
{
if
(
from
==
null
||
to
==
null
)
{
return
to
;
return
to
;
}
}
HashMap
<
String
,
MethodField
>
mapField
=
get
Init
TypeField
(
from
.
getClass
());
HashMap
<
String
,
MethodField
>
mapField
=
getTypeField
(
from
.
getClass
());
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
}
}
...
...
yzg-util-base/src/test/java/base/DemoVo.java
View file @
56179770
...
@@ -11,6 +11,14 @@ public class DemoVo {
...
@@ -11,6 +11,14 @@ public class DemoVo {
*/
*/
private
String
name
;
private
String
name
;
public
DemoVo
()
{
}
public
DemoVo
(
String
id
,
String
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
}
public
String
getId
()
{
public
String
getId
()
{
return
id
;
return
id
;
}
}
...
...
yzg-util-base/src/test/java/helper/TestCollectionString.java
0 → 100644
View file @
56179770
package
helper
;
import
base.DemoVo
;
import
com.yanzuoguang.util.base.CollectionString
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.junit.Test
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
TestCollectionString
{
@Test
public
void
testCh
()
{
List
<
DemoVo
>
mapList
=
new
ArrayList
<>(
Arrays
.
asList
(
new
DemoVo
(
"1"
,
"颜佐光"
),
new
DemoVo
(
"10000"
,
"成吉思汗"
),
new
DemoVo
(
"10001"
,
"毛泽东"
)
));
System
.
out
.
println
(
CollectionString
.
getCollectionString
(
"人名:"
,
mapList
));
System
.
out
.
println
(
CollectionString
.
getCollectionString
(
StringHelper
.
EMPTY
,
mapList
));
}
@Test
public
void
testEn
()
{
List
<
DemoVo
>
mapList
=
new
ArrayList
<>(
Arrays
.
asList
(
new
DemoVo
(
"1"
,
"yan zuo guang"
),
new
DemoVo
(
"10000"
,
"chen jie si han"
),
new
DemoVo
(
"10001"
,
"mao ze dong"
)
));
System
.
out
.
println
(
CollectionString
.
getCollectionString
(
StringHelper
.
EMPTY
,
mapList
));
System
.
out
.
println
(
CollectionString
.
getCollectionString
(
"人名:"
,
mapList
));
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectLogStart.java
View file @
56179770
...
@@ -4,7 +4,6 @@ import com.yanzuoguang.cloud.CloudConfig;
...
@@ -4,7 +4,6 @@ import com.yanzuoguang.cloud.CloudConfig;
import
com.yanzuoguang.cloud.aop.log.LogLocal
;
import
com.yanzuoguang.cloud.aop.log.LogLocal
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.LogVo
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
/**
/**
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectLogTime.java
0 → 100644
View file @
56179770
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.yanzuoguang.util.base.CollectionString
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.stereotype.Component
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 日志时间
*
* @author 颜佐光
*/
@Component
public
class
AspectLogTime
implements
ThreadNext
.
Next
,
InitializingBean
{
private
MemoryCache
<
AspectUrlCountVo
>
memoryCache
=
new
MemoryCache
<>();
@Override
public
void
afterPropertiesSet
()
{
ThreadNext
.
start
(
this
,
"aspectLogTime"
);
}
@Override
public
boolean
next
()
{
List
<
AspectUrlCountVo
>
rowList
=
new
ArrayList
<>(
memoryCache
.
getValues
());
System
.
out
.
println
(
CollectionString
.
getCollectionString
(
"接口执行次数:"
,
rowList
));
return
true
;
}
@Override
public
int
getNextTime
()
{
return
60
*
1000
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectUrlCountVo.java
0 → 100644
View file @
56179770
package
com
.
yanzuoguang
.
cloud
.
aop
;
/**
* 次数统计
*
* @author 颜佐光
*/
public
class
AspectUrlCountVo
{
private
volatile
String
url
;
private
volatile
long
startCount
;
private
volatile
long
endCount
;
private
volatile
long
totalTime
;
private
volatile
long
minTime
;
private
volatile
long
maxTime
;
public
AspectUrlCountVo
(
String
url
)
{
this
.
url
=
url
;
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
long
getStartCount
()
{
return
startCount
;
}
public
void
setStartCount
(
long
startCount
)
{
this
.
startCount
=
startCount
;
}
public
long
getEndCount
()
{
return
endCount
;
}
public
void
setEndCount
(
long
endCount
)
{
this
.
endCount
=
endCount
;
}
public
long
getTotalTime
()
{
return
totalTime
;
}
public
void
setTotalTime
(
long
totalTime
)
{
this
.
totalTime
=
totalTime
;
}
public
long
getMinTime
()
{
return
minTime
;
}
public
void
setMinTime
(
long
minTime
)
{
this
.
minTime
=
minTime
;
}
public
long
getMaxTime
()
{
return
maxTime
;
}
public
void
setMaxTime
(
long
maxTime
)
{
this
.
maxTime
=
maxTime
;
}
}
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