Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
187d025c
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
187d025c
编写于
9月 23, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
map converters
上级
1ee4ac1b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
89 addition
and
6 deletion
+89
-6
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
...mework/core/convert/support/GenericConversionService.java
+1
-0
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArrayGenericConverter.java
...ore/convert/support/MapToStringArrayGenericConverter.java
+58
-0
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
...k/core/convert/support/GenericConversionServiceTests.java
+30
-6
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
浏览文件 @
187d025c
...
@@ -192,6 +192,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
...
@@ -192,6 +192,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
addGenericConverter
(
Collection
.
class
,
String
.
class
,
new
CollectionToStringGenericConverter
(
this
));
addGenericConverter
(
Collection
.
class
,
String
.
class
,
new
CollectionToStringGenericConverter
(
this
));
addGenericConverter
(
Collection
.
class
,
Object
.
class
,
new
CollectionToObjectGenericConverter
(
this
));
addGenericConverter
(
Collection
.
class
,
Object
.
class
,
new
CollectionToObjectGenericConverter
(
this
));
addGenericConverter
(
Map
.
class
,
Map
.
class
,
new
MapToMapGenericConverter
(
this
));
addGenericConverter
(
Map
.
class
,
Map
.
class
,
new
MapToMapGenericConverter
(
this
));
addGenericConverter
(
Map
.
class
,
String
[].
class
,
new
MapToStringArrayGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Object
[].
class
,
new
StringToArrayGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Object
[].
class
,
new
StringToArrayGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Collection
.
class
,
new
StringToCollectionGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Collection
.
class
,
new
StringToCollectionGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Map
.
class
,
new
StringToMapGenericConverter
(
this
));
addGenericConverter
(
String
.
class
,
Map
.
class
,
new
StringToMapGenericConverter
(
this
));
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArrayGenericConverter.java
0 → 100644
浏览文件 @
187d025c
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.core.convert.support
;
import
java.lang.reflect.Array
;
import
java.util.Map
;
import
org.springframework.core.convert.TypeDescriptor
;
final
class
MapToStringArrayGenericConverter
implements
GenericConverter
{
private
final
GenericConversionService
conversionService
;
public
MapToStringArrayGenericConverter
(
GenericConversionService
conversionService
)
{
this
.
conversionService
=
conversionService
;
}
public
Object
convert
(
Object
source
,
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
Map
sourceMap
=
(
Map
)
source
;
TypeDescriptor
sourceKeyType
=
sourceType
.
getMapKeyTypeDescriptor
();
TypeDescriptor
sourceValueType
=
sourceType
.
getMapValueTypeDescriptor
();
TypeDescriptor
targetElementType
=
targetType
.
getElementTypeDescriptor
();
boolean
keysCompatible
=
false
;
if
(
sourceKeyType
.
isAssignableTo
(
targetElementType
))
{
keysCompatible
=
true
;
}
boolean
valuesCompatible
=
false
;
if
(
sourceValueType
.
isAssignableTo
(
targetElementType
))
{
valuesCompatible
=
true
;
}
Object
array
=
Array
.
newInstance
(
targetElementType
.
getType
(),
sourceMap
.
size
());
MapEntryConverter
converter
=
new
MapEntryConverter
(
sourceKeyType
,
sourceValueType
,
targetElementType
,
targetElementType
,
keysCompatible
,
valuesCompatible
,
conversionService
);
int
i
=
0
;
for
(
Object
entry
:
sourceMap
.
entrySet
())
{
Map
.
Entry
mapEntry
=
(
Map
.
Entry
)
entry
;
Object
key
=
mapEntry
.
getKey
();
Object
value
=
mapEntry
.
getValue
();
String
property
=
converter
.
convertKey
(
key
)
+
"="
+
converter
.
convertValue
(
value
);
Array
.
set
(
array
,
i
,
property
);
i
++;
}
return
array
;
}
}
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
浏览文件 @
187d025c
...
@@ -27,6 +27,7 @@ import java.util.ArrayList;
...
@@ -27,6 +27,7 @@ import java.util.ArrayList;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.LinkedHashSet
;
import
java.util.LinkedList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -307,6 +308,29 @@ public class GenericConversionServiceTests {
...
@@ -307,6 +308,29 @@ public class GenericConversionServiceTests {
assertEquals
(
FooEnum
.
BAZ
,
map
.
get
(
2
));
assertEquals
(
FooEnum
.
BAZ
,
map
.
get
(
2
));
}
}
@Test
public
void
convertMapToStringArray
()
throws
Exception
{
Map
<
String
,
String
>
foo
=
new
LinkedHashMap
<
String
,
String
>();
foo
.
put
(
"1"
,
"BAR"
);
foo
.
put
(
"2"
,
"BAZ"
);
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
String
[]
result
=
conversionService
.
convert
(
foo
,
String
[].
class
);
assertEquals
(
"1=BAR"
,
result
[
0
]);
assertEquals
(
"2=BAZ"
,
result
[
1
]);
}
@Test
public
void
convertMapToStringArrayWithElementConversion
()
throws
Exception
{
Map
<
Integer
,
FooEnum
>
foo
=
new
LinkedHashMap
<
Integer
,
FooEnum
>();
foo
.
put
(
1
,
FooEnum
.
BAR
);
foo
.
put
(
2
,
FooEnum
.
BAZ
);
conversionService
.
addConverter
(
new
ObjectToStringConverter
());
String
[]
result
=
(
String
[])
conversionService
.
convert
(
foo
,
new
TypeDescriptor
(
getClass
()
.
getField
(
"genericMap"
)),
TypeDescriptor
.
valueOf
(
String
[].
class
));
assertEquals
(
"1=BAR"
,
result
[
0
]);
assertEquals
(
"2=BAZ"
,
result
[
1
]);
}
@Test
@Test
public
void
convertStringToArray
()
{
public
void
convertStringToArray
()
{
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
...
@@ -403,8 +427,8 @@ public class GenericConversionServiceTests {
...
@@ -403,8 +427,8 @@ public class GenericConversionServiceTests {
@Test
@Test
public
void
convertStringArrayToMapWithElementConversion
()
throws
Exception
{
public
void
convertStringArrayToMapWithElementConversion
()
throws
Exception
{
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToEnumConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToEnumConverterFactory
());
Map
result
=
(
Map
)
conversionService
.
convert
(
new
String
[]
{
"1=BAR"
,
"2=BAZ"
},
TypeDescriptor
Map
result
=
(
Map
)
conversionService
.
convert
(
new
String
[]
{
"1=BAR"
,
"2=BAZ"
},
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
assertEquals
(
FooEnum
.
BAR
,
result
.
get
(
1
));
assertEquals
(
FooEnum
.
BAR
,
result
.
get
(
1
));
...
@@ -421,10 +445,10 @@ public class GenericConversionServiceTests {
...
@@ -421,10 +445,10 @@ public class GenericConversionServiceTests {
@Test
@Test
public
void
convertStringToMapWithElementConversion
()
throws
Exception
{
public
void
convertStringToMapWithElementConversion
()
throws
Exception
{
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToEnumConverterFactory
());
conversionService
.
addConverterFactory
(
new
StringToEnumConverterFactory
());
Map
result
=
(
Map
)
conversionService
.
convert
(
"1=BAR 2=BAZ"
,
TypeDescriptor
Map
result
=
(
Map
)
conversionService
.
convert
(
"1=BAR 2=BAZ"
,
TypeDescriptor
.
valueOf
(
String
.
class
),
.
valueOf
(
String
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
assertEquals
(
FooEnum
.
BAR
,
result
.
get
(
1
));
assertEquals
(
FooEnum
.
BAR
,
result
.
get
(
1
));
assertEquals
(
FooEnum
.
BAZ
,
result
.
get
(
2
));
assertEquals
(
FooEnum
.
BAZ
,
result
.
get
(
2
));
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录