Skip to content

Commit 3dc0114

Browse files
committed
[Feature] add for new
1 parent 5957970 commit 3dc0114

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+949
-201
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626

2727
- Fluent 流式写法
2828

29-
- 基于 java 注解
29+
- 基于 java 注解,支持自定义的转换和灵活配置
3030

31-
- 字段类型转换的灵活支持,内置 8 大基本类型以及 String 类型转换
31+
- 内置 8 大基本类型以及 String 类型转换
32+
33+
- 解决 Excel 直接打开,utf-8 乱码问题
34+
35+
- 内置支持集合、数组、Map 的存取
3236

3337
# 快速开始
3438

@@ -44,7 +48,7 @@ maven 3.x
4448
<dependency>
4549
<groupId>com.github.houbb</groupId>
4650
<artifactId>csv</artifactId>
47-
<version>0.0.2</version>
51+
<version>0.0.3</version>
4852
</dependency>
4953
```
5054

@@ -141,6 +145,8 @@ public void commonTest() {
141145

142146
# 拓展阅读
143147

144-
[CSV 引导类方法说明](doc/user/01-csv-引导类.md)
148+
[01-CSV 引导类方法说明](doc/user/01-csv-引导类.md)
149+
150+
[02-CSV 字段注解的使用](doc/user/02-csv-注解使用.md)
145151

146-
[CSV 字段注解的使用](doc/user/02-csv-注解使用.md)
152+
[03-CSV 集合相关支持](doc/user/03-csv-支持集合类.md)

doc/CHANGE_LOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@
1414
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
1515
|:---|:---|:---|:---|:--|
1616
| 1 | A | 基本功能的实现 | 2019-6-1 13:55:32 | |
17+
18+
# release_0.0.2
19+
20+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
21+
|:---|:---|:---|:---|:--|
22+
| 1 | O | 注解相关测试用例的完善 | 2019-6-10 13:55:32 | |
23+
24+
# release_0.0.3
25+
26+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
27+
|:---|:---|:---|:---|:--|
28+
| 1 | A | 支持集合、数组、Map 等常见集合 | 2019-6-17 19:42:30 | |

doc/user/03-csv-支持集合类.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 集合类
2+
3+
有时候对象中会包含数组、Map、Collection 等常见集合。
4+
5+
为了存储的便利性,默认提供集合的相关支持。
6+
7+
特性和普通字段保持一致,如果指定注解转换,则以注解为准。
8+
9+
## 使用示例
10+
11+
- UserCollection.java
12+
13+
用于演示集合的对象
14+
15+
```java
16+
public class UserCollection {
17+
18+
private String[] arrays;
19+
20+
private LinkedList<String> lists;
21+
22+
private Map<String, String> maps;
23+
24+
private Set<String> sets;
25+
26+
//Getter/Setter/toString()
27+
}
28+
```
29+
30+
## 存储
31+
32+
- 待存储对象的构建
33+
34+
```java
35+
/**
36+
* 构建基于集合的测试列表
37+
* @return 列表
38+
* @since 0.0.3
39+
*/
40+
private List<UserCollection> buildCollectionList() {
41+
UserCollection user = new UserCollection();
42+
String[] arrays = new String[]{"a", "b", "c"};
43+
LinkedList<String> lists = new LinkedList<>(Arrays.asList(arrays));
44+
Map<String, String> maps = new HashMap<>();
45+
maps.put("key", "value");
46+
maps.put("key2", "value2");
47+
Set<String> sets = new HashSet<>();
48+
sets.add("set1");
49+
sets.add("set2");
50+
51+
user.setLists(lists);
52+
user.setArrays(arrays);
53+
user.setMaps(maps);
54+
user.setSets(sets);
55+
return Arrays.asList(user);
56+
}
57+
```
58+
59+
- 执行存储
60+
61+
```java
62+
public void collectionTest() {
63+
final String path = "src\\test\\resources\\collection.csv";
64+
CsvWriteBs.newInstance(path)
65+
.write(buildCollectionList());
66+
}
67+
```
68+
69+
- 存储效果
70+
71+
```
72+
arrays,lists,maps,sets
73+
a|b,a|b|c,key2=value2|key=value,set1|set2
74+
```
75+
76+
## 读取
77+
78+
- 测试类
79+
80+
```java
81+
public void collectionTest() {
82+
final String path = "src\\test\\resources\\collection.csv";
83+
List<UserCollection> userList = CsvReadBs.newInstance(path)
84+
.read(UserCollection.class);
85+
System.out.println(userList);
86+
}
87+
```
88+
89+
- 测试日志
90+
91+
```
92+
[UserCollection{arrays=[a, b], lists=[a, b, c], maps={key=value, key2=value2}, sets=[set2, set1]}]
93+
```
94+
95+
# 注意
96+
97+
为了保证 csv 以 `,` 分隔的统一性。
98+
99+
集合使用 `|` 进行分隔,其中 map 的 key/value 分隔,用到了 `=`
100+
101+
在使用时要注意,不要包含上述的符号,否则会出现解析错乱。

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<project.compiler.level>1.7</project.compiler.level>
2626

2727
<!--============================== INTER ==============================-->
28-
<heaven.version>0.1.7</heaven.version>
28+
<heaven.version>0.1.14</heaven.version>
2929

3030
<!--============================== OTHER ==============================-->
3131
<junit.version>4.12</junit.version>

src/main/java/com/github/houbb/csv/api/IReadConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.houbb.csv.api;
22

3+
import java.lang.reflect.Field;
4+
35
/**
46
* 读取转换器,将 string 转换为 field
57
* @author binbin.hou
@@ -11,10 +13,9 @@ public interface IReadConverter<T> {
1113
/**
1214
* 执行转换
1315
* @param value 字符串值
14-
* @param fieldType 字段类型
16+
* @param field 字段类型
1517
* @return 结果
1618
*/
17-
T convert(final String value,
18-
final Class fieldType);
19+
T convert(final String value, final Field field);
1920

2021
}

src/main/java/com/github/houbb/csv/support/convert/read/BooleanReadConverter.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/github/houbb/csv/support/convert/read/ByteReadConverter.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/github/houbb/csv/support/convert/read/CharacterReadConverter.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.github.houbb.csv.support.convert.read;
22

33
import com.github.houbb.csv.api.IReadConverter;
4+
import com.github.houbb.csv.support.convert.read.collection.ArrayReadConverter;
5+
import com.github.houbb.csv.support.convert.read.collection.CollectionReadConverter;
6+
import com.github.houbb.csv.support.convert.read.collection.MapReadConverter;
7+
import com.github.houbb.csv.support.convert.read.type.ITypeConverter;
8+
import com.github.houbb.csv.support.convert.read.type.impl.*;
49
import com.github.houbb.heaven.annotation.ThreadSafe;
510
import com.github.houbb.heaven.support.instance.impl.InstanceFactory;
11+
import com.github.houbb.heaven.support.instance.impl.Instances;
612
import com.github.houbb.heaven.util.lang.ObjectUtil;
713
import com.github.houbb.heaven.util.lang.StringUtil;
814
import com.github.houbb.heaven.util.lang.reflect.ClassTypeUtil;
915
import com.github.houbb.heaven.util.lang.reflect.PrimitiveUtil;
16+
import com.github.houbb.heaven.util.util.ArrayUtil;
1017

18+
import java.lang.reflect.Field;
1119
import java.util.HashMap;
1220
import java.util.Map;
1321

@@ -17,12 +25,12 @@
1725
* @since 0.0.1
1826
*/
1927
@ThreadSafe
20-
public class CommonReadConverter implements IReadConverter<Object> {
28+
public class CommonReadConverter implements IReadConverter<Object>, ITypeConverter<Object> {
2129

2230
/**
2331
* 转换器映射关系
2432
*/
25-
private static final Map<Class, IReadConverter> CONVERTER_MAP = new HashMap<>();
33+
private static final Map<Class, ITypeConverter> CONVERTER_MAP = new HashMap<>();
2634

2735
static {
2836
CONVERTER_MAP.put(String.class, InstanceFactory.getInstance().singleton(StringReadConverter.class));
@@ -37,25 +45,54 @@ public class CommonReadConverter implements IReadConverter<Object> {
3745
}
3846

3947
@Override
40-
public Object convert(String value, Class fieldType) {
48+
public Object convert(String value, final Field field) {
4149
//1. 为空判断
4250
if(StringUtil.isEmpty(value)) {
4351
return null;
4452
}
4553

46-
//2. 维护一个映射关系
47-
Class refType = fieldType;
48-
if(fieldType.isPrimitive()) {
49-
refType = PrimitiveUtil.getReferenceType(fieldType);
54+
// 获取当前字段类型
55+
Class refType = field.getType();
56+
57+
//2 特殊集合的处理
58+
// 2.1 数组
59+
if(ClassTypeUtil.isArray(refType)) {
60+
return Instances.singletion(ArrayReadConverter.class).convert(value, field);
61+
}
62+
// 2.2 map
63+
if(ClassTypeUtil.isMap(refType)) {
64+
return Instances.singletion(MapReadConverter.class).convert(value, field);
5065
}
51-
IReadConverter readConverter = CONVERTER_MAP.get(refType);
66+
// 2.3 collection
67+
if(ClassTypeUtil.isCollection(refType)) {
68+
return Instances.singletion(CollectionReadConverter.class).convert(value, field);
69+
}
70+
71+
// 3. 基本类型
72+
return convert(value, refType);
73+
}
74+
75+
76+
@Override
77+
public Object convert(String value, final Class type) {
78+
//1. 快速返回
79+
if(StringUtil.isEmpty(value)
80+
|| ObjectUtil.isNull(type)) {
81+
return null;
82+
}
83+
84+
// 2. 根据类型处理
85+
Class actualType = type;
86+
if(actualType.isPrimitive()) {
87+
actualType = PrimitiveUtil.getReferenceType(actualType);
88+
}
89+
ITypeConverter readConverter = CONVERTER_MAP.get(actualType);
5290
if(ObjectUtil.isNotNull(readConverter)) {
53-
return readConverter.convert(value, fieldType);
91+
return readConverter.convert(value, actualType);
5492
}
5593

5694
//3. 不属于基本类型,则直接返回 null
5795
return null;
5896
}
5997

60-
6198
}

src/main/java/com/github/houbb/csv/support/convert/read/DoubleReadConverter.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)