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
44e7a702
Commit
44e7a702
authored
Mar 27, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加了值的写入方法
parent
741604d5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
19 deletions
+85
-19
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+85
-19
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
44e7a702
...
@@ -401,17 +401,25 @@ public class ObjectHelper {
...
@@ -401,17 +401,25 @@ public class ObjectHelper {
* @return 转换后的值 to
* @return 转换后的值 to
*/
*/
public
static
Object
writeWithToClass
(
Object
to
,
Object
from
)
{
public
static
Object
writeWithToClass
(
Object
to
,
Object
from
)
{
if
(
to
==
null
)
{
return
writeWithToClass
(
false
,
to
,
from
);
return
null
;
}
HashMap
<
String
,
MethodField
>
mapField
=
getInitTypeField
(
to
.
getClass
());
for
(
Map
.
Entry
<
String
,
MethodField
>
field
:
mapField
.
entrySet
())
{
String
name
=
field
.
getValue
().
name
;
Object
fromValue
=
ObjectHelper
.
get
(
from
,
name
);
ObjectHelper
.
set
(
to
,
name
,
fromValue
);
}
}
/**
* 对象转换(一般将map转为javabean)
*
* @param emptyWrite 为空时是否写入
* @param to 转换后的对象
* @param from 要转换的对象
* @return 转换后的值 to
*/
public
static
Object
writeWithToClass
(
boolean
emptyWrite
,
Object
to
,
Object
from
)
{
if
(
from
==
null
||
to
==
null
)
{
return
to
;
return
to
;
}
}
HashMap
<
String
,
MethodField
>
mapField
=
getInitTypeField
(
to
.
getClass
());
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
}
/**
/**
* 根据来源数据,往目标中写入数据
* 根据来源数据,往目标中写入数据
...
@@ -420,33 +428,91 @@ public class ObjectHelper {
...
@@ -420,33 +428,91 @@ public class ObjectHelper {
* @param froms 来源对象
* @param froms 来源对象
*/
*/
public
static
void
writeWithFrom
(
Object
to
,
Object
...
froms
)
{
public
static
void
writeWithFrom
(
Object
to
,
Object
...
froms
)
{
for
(
Object
model
:
froms
)
{
for
(
Object
from
:
froms
)
{
if
(
model
instanceof
Map
)
{
if
(
from
instanceof
Map
)
{
Map
map
=
(
Map
)
model
;
writeWithFromMap
(
to
,
(
Map
)
from
);
for
(
Object
key
:
map
.
keySet
())
{
ObjectHelper
.
set
(
to
,
StringHelper
.
toString
(
key
),
map
.
get
(
key
));
}
}
else
{
}
else
{
ObjectHelper
.
writeWithFromClass
(
to
,
model
);
ObjectHelper
.
writeWithFromClass
(
to
,
from
);
}
}
}
}
}
}
/**
/**
* 根据来源类型的字段,将数据写入目标类型的数据
* 将值吸入到到目标数据
*
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public
static
Object
writeWithFromMap
(
Object
to
,
Map
from
)
{
return
writeWithFromMap
(
false
,
to
,
from
);
}
/**
* 将值吸入到到目标数据
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public
static
Object
writeWithFromMap
(
boolean
emptyWrite
,
Object
to
,
Map
from
)
{
for
(
Object
key
:
from
.
keySet
())
{
Object
fromValue
=
from
.
get
(
key
);
if
(
StringHelper
.
isEmpty
(
fromValue
))
{
continue
;
}
ObjectHelper
.
set
(
to
,
StringHelper
.
toString
(
key
),
fromValue
);
}
return
to
;
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据,为空时不写入
*
*
* @param to 目标类型的数据
* @param to 目标类型的数据
* @param from 来源类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
* @return 写入之后的值
*/
*/
public
static
Object
writeWithFromClass
(
Object
to
,
Object
from
)
{
public
static
Object
writeWithFromClass
(
Object
to
,
Object
from
)
{
if
(
from
==
null
)
{
return
writeWithFromClass
(
false
,
to
,
from
);
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public
static
Object
writeWithFromClass
(
boolean
emptyWrite
,
Object
to
,
Object
from
)
{
if
(
from
==
null
||
to
==
null
)
{
return
to
;
return
to
;
}
}
HashMap
<
String
,
MethodField
>
mapField
=
getInitTypeField
(
from
.
getClass
());
HashMap
<
String
,
MethodField
>
mapField
=
getInitTypeField
(
from
.
getClass
());
return
writeWithClass
(
emptyWrite
,
to
,
from
,
mapField
);
}
/**
* 根据字段类型写入值
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @param mapField 需要写入的字段
* @return 写入之后的值
*/
private
static
Object
writeWithClass
(
boolean
emptyWrite
,
Object
to
,
Object
from
,
HashMap
<
String
,
MethodField
>
mapField
)
{
if
(
from
==
null
||
to
==
null
)
{
return
to
;
}
for
(
Map
.
Entry
<
String
,
MethodField
>
field
:
mapField
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
MethodField
>
field
:
mapField
.
entrySet
())
{
String
name
=
field
.
getValue
().
name
;
String
name
=
field
.
getValue
().
name
;
Object
fromValue
=
ObjectHelper
.
get
(
from
,
name
);
Object
fromValue
=
ObjectHelper
.
get
(
from
,
name
);
if
(
StringHelper
.
isEmpty
(
fromValue
))
{
continue
;
}
ObjectHelper
.
set
(
to
,
name
,
fromValue
);
ObjectHelper
.
set
(
to
,
name
,
fromValue
);
}
}
return
to
;
return
to
;
...
...
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