Skip to content

Commit 26cd0b9

Browse files
author
hongwei.quhw
committed
支持负数
1 parent 637c1c7 commit 26cd0b9

File tree

5 files changed

+156
-10
lines changed

5 files changed

+156
-10
lines changed

rdf-file-core/src/main/java/com/alipay/rdf/file/format/NColumnFormat.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public String serialize(String field, FileColumnMeta colMeta, FileConfig fileCon
3636
} else {
3737
try {
3838
BigInteger value = new BigDecimal(field.toString().trim())
39-
.multiply(new BigDecimal(
40-
RdfFileUtil.alignLeft("1", colMeta.getRange().getSecondAttr() + 1, "0")))
41-
.toBigInteger();
39+
.multiply(new BigDecimal(RdfFileUtil.alignLeft("1", colMeta.getRange().getSecondAttr() + 1, "0"))).toBigInteger();
4240

43-
field = RdfFileUtil.alignRight(String.valueOf(value),
44-
colMeta.getRange().getFirstAttr(), '0');
41+
//负数
42+
if (value.compareTo(new BigInteger("0")) < 0) {
43+
field = RdfFileUtil.alignRight(String.valueOf(value.negate()), colMeta.getRange().getFirstAttr(), '0', true);
44+
} else {
45+
field = RdfFileUtil.alignRight(String.valueOf(value), colMeta.getRange().getFirstAttr(), '0');
46+
}
4547
} catch (NumberFormatException e) {
4648
throw new RdfFileException("rdf-file#NColumnFormat field = " + field
4749
+ ", columnName=" + colMeta.getName() + " 数组转换出错",
@@ -58,9 +60,7 @@ public String deserialize(String field, FileColumnMeta colMeta, FileConfig fileC
5860
BigDecimal value = null;
5961
//有小数点
6062
if (colMeta.getRange().getSecondAttr() > 0) {
61-
value = new BigDecimal(field).divide(new BigDecimal(
62-
RdfFileUtil.alignLeft("1", colMeta.getRange().getSecondAttr() + 1, "0")));
63-
63+
value = new BigDecimal(field).divide(new BigDecimal(RdfFileUtil.alignLeft("1", colMeta.getRange().getSecondAttr() + 1, "0")));
6464
} else {
6565
value = new BigDecimal(field);
6666
}

rdf-file-core/src/main/java/com/alipay/rdf/file/util/RdfFileUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public static Object newInstance(String clazz) {
202202
}
203203
}
204204

205+
public static String alignRight(String str, int size, char padChar) {
206+
return alignRight(str, size, padChar, false);
207+
}
208+
205209
/**
206210
* 基金格式中对数值补位
207211
*
@@ -210,8 +214,18 @@ public static Object newInstance(String clazz) {
210214
* @param padChar
211215
* @return
212216
*/
213-
public static String alignRight(String str, int size, char padChar) {
214-
String val = alignRight(str, size, String.valueOf(padChar));
217+
public static String alignRight(String str, int size, char padChar, boolean negate) {
218+
int alignSize = size;
219+
220+
if (negate) {
221+
alignSize--;
222+
}
223+
224+
String val = alignRight(str, alignSize, String.valueOf(padChar));
225+
226+
if (negate) {
227+
val = "-" + val;
228+
}
215229

216230
if (val.length() != size) {
217231
throw new RdfFileException(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.alipay.rdf.file.format;
2+
3+
import com.alipay.rdf.file.exception.RdfFileException;
4+
import com.alipay.rdf.file.meta.FileColumnMeta;
5+
import com.alipay.rdf.file.meta.FileColumnRangeMeta;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* @author: hongwei.quhwØ
11+
* @date: 2020-07-10 16:08
12+
*/
13+
public class NColumnFormatTest {
14+
private NColumnFormat nColumnFormat = new NColumnFormat();
15+
16+
@Test
17+
public void test1() {
18+
19+
FileColumnRangeMeta range = new FileColumnRangeMeta(4, 2);
20+
21+
FileColumnMeta colMeta = new FileColumnMeta(0, "test", "test", null, true, range, null, null, null);
22+
23+
try {
24+
nColumnFormat.serialize("-12.22", colMeta, null);
25+
} catch (RdfFileException e) {
26+
Assert.assertEquals("数值1222补位后-1222长度5模板定义长度4", e.getMessage());
27+
}
28+
29+
String value = nColumnFormat.serialize("-0.22", colMeta, null);
30+
Assert.assertEquals("-022", value);
31+
String ret = nColumnFormat.deserialize(value, colMeta, null);
32+
Assert.assertEquals("-0.22", ret);
33+
34+
35+
range = new FileColumnRangeMeta(4, 0);
36+
37+
colMeta = new FileColumnMeta(0, "test", "test", null, true, range, null, null, null);
38+
39+
try {
40+
nColumnFormat.serialize("-1222", colMeta, null);
41+
} catch (RdfFileException e) {
42+
Assert.assertEquals("数值1222补位后-1222长度5模板定义长度4", e.getMessage());
43+
}
44+
45+
value = nColumnFormat.serialize("-2", colMeta, null);
46+
Assert.assertEquals("-002", value);
47+
ret = nColumnFormat.deserialize(value, colMeta, null);
48+
Assert.assertEquals("-2", ret);
49+
50+
}
51+
}

rdf-file-core/src/test/java/com/alipay/rdf/file/writer/FundFileWriterTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.HashMap;
88
import java.util.Map;
99

10+
import com.alipay.rdf.file.interfaces.FileReader;
1011
import org.junit.After;
1112
import org.junit.Before;
1213
import org.junit.Test;
@@ -88,6 +89,64 @@ public void testWriter() throws Exception {
8889
reader.close();
8990
}
9091

92+
@Test
93+
public void testWriterNegative() throws Exception {
94+
String filePath = temporaryFolder.getRoot().getAbsolutePath();
95+
System.out.println(filePath);
96+
97+
FileConfig config = new FileConfig(new File(filePath, "test.txt").getAbsolutePath(),
98+
"/writer/template/fund2.cfg", new StorageConfig("nas"));
99+
FileWriter fileWriter = FileFactory.createWriter(config);
100+
101+
Map<String, Object> head = new HashMap<String, Object>();
102+
head.put("msgRecipient", "xxx");
103+
head.put("sendDate", DateUtil.parse("20151204", "yyyyMMdd"));
104+
head.put("summaryTableNo", "aa");
105+
head.put("fileTypeCode", "bb");
106+
head.put("recipient", "ll");
107+
head.put("totalCount", 1);
108+
109+
fileWriter.writeHead(head);
110+
111+
Map<String, Object> row = new HashMap<String, Object>();
112+
row.put("TransactionCfmDate", DateUtil.parse("20151204", "yyyyMMdd"));
113+
row.put("FundCode", "中国1");
114+
row.put("AvailableVol", -42.11);
115+
fileWriter.writeRow(row);
116+
117+
fileWriter.writeTail(new HashMap<String, Object>());
118+
119+
fileWriter.close();
120+
121+
FileReader fileReader = FileFactory.createReader(config);
122+
row = fileReader.readRow(HashMap.class);
123+
Assert.assertEquals(-42.11, row.get("AvailableVol"));
124+
125+
//校验文件
126+
BufferedReader reader = new BufferedReader(
127+
new InputStreamReader(new FileInputStream(new File(filePath, "test.txt")), "UTF-8"));
128+
Assert.assertEquals("OFDCFDAT", reader.readLine());
129+
Assert.assertEquals("20 ", reader.readLine());
130+
Assert.assertEquals("H0 ", reader.readLine());
131+
Assert.assertEquals("xxx ", reader.readLine());
132+
Assert.assertEquals("20151204", reader.readLine());
133+
Assert.assertEquals("aa ", reader.readLine());
134+
Assert.assertEquals("bb", reader.readLine());
135+
Assert.assertEquals("H0 ", reader.readLine());
136+
Assert.assertEquals("ll ", reader.readLine());
137+
Assert.assertEquals("003", reader.readLine());
138+
Assert.assertEquals("TransactionCfmDate", reader.readLine());
139+
Assert.assertEquals("FundCode", reader.readLine());
140+
Assert.assertEquals("AvailableVol", reader.readLine());
141+
Assert.assertEquals("00000001", reader.readLine());
142+
143+
Assert.assertEquals("20151204中国1 -04211", reader.readLine());
144+
145+
Assert.assertEquals("OFDCFEND", reader.readLine());
146+
147+
reader.close();
148+
}
149+
91150
@After
92151
public void after() {
93152
temporaryFolder.delete();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"head":[
3+
"identity|信息标识|[8,0]|default:OFDCFDAT",
4+
"version|协议版本号|[4,0]|default:20",
5+
"msgCreator|信息创建人|[9,0]|default:H0",
6+
"msgRecipient|信息接收人|[9,0]",
7+
"sendDate|传送发生日期|[8,0]|Date:yyyyMMdd",
8+
"summaryTableNo|汇总表号|[3,0]",
9+
"fileTypeCode|文件类型代码 |[2,0]",
10+
"sender|发送人|[8,0]|default:H0",
11+
"recipient|接收人|[8,0]"
12+
],
13+
"body":[
14+
"TransactionCfmDate|对帐日期|[8,0]|Date:yyyyMMdd",
15+
"FundCode|基金代码|[8,0]",
16+
"AvailableVol|基金可用份数|double|[6,2]"
17+
],
18+
"tail":[
19+
"fileEnd|数据文件尾部字符|default:OFDCFEND|[8,0]"
20+
],
21+
"protocol":"FUND"
22+
}

0 commit comments

Comments
 (0)