Skip to content

Commit

Permalink
增加了两个重载方法
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkfox committed Jan 17, 2019
1 parent 87630dd commit bce5e3c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/main/java/com/blinkfox/minitable/MiniTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ private void init() {
this.maxColMap = new HashMap<Integer, Integer>();
}

/**
* 将集合中的元素添加到表格中的表头数据中.
*
* @param headers 表头数据
* @return MiniTable对象
*/
public MiniTable addHeaders(List<?> headers) {
return this.appendRows(RowType.HEADER, headers.toArray());
}

/**
* 向表格中添加表头数据.
*
Expand All @@ -66,7 +76,17 @@ public MiniTable addHeaders(Object... objects) {
/**
* 向表格中添加一行普通数据.
*
* @param objects 表头数据
* @param datas 普通行数据
* @return MiniTable对象
*/
public MiniTable addDatas(List<?> datas) {
return this.appendRows(RowType.DATA, datas.toArray());
}

/**
* 向表格中添加一行普通数据.
*
* @param objects 普通行数据
* @return MiniTable对象
*/
public MiniTable addDatas(Object... objects) {
Expand All @@ -83,7 +103,7 @@ private MiniTable appendRows(RowType rowType, Object... objects) {
int len;
if (objects != null && (len = objects.length) > 0) {
if (this.maxColMap.size() > len) {
throw new IllegalArgumentException("向表格中插入某行数据的列数比之前的列数多了,请检查!");
throw new IllegalArgumentException("向表格中插入某行数据的列数与之前的列数不同,请检查!");
}

// 遍历传入的数据集合,并将其存入到行数据对象中.
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/com/blinkfox/minitable/MiniTableTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.blinkfox.minitable;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -31,14 +34,22 @@ public class MiniTableTest {
*/
@Test
public void render() {
// 构造一行List集合类型的普通数据,用于测试List集合数据的添加.
List<Object> rowDatas = new ArrayList<Object>();
rowDatas.add("WangWu");
rowDatas.add("male");
rowDatas.add(48);
rowDatas.add("[email protected]");
rowDatas.add("15809876236");

String table = new MiniTable("The Title")
.addHeaders("Name", "Sex", "Age", "Email", "Phone")
.addDatas("LiLei", "male", 25, "[email protected]", "13809345219")
.addDatas("hanMeiMei", "female", 23, "[email protected]", "13515343853")
.addHeaders("Name", "Sex", "Age", "Email", "Phone")
.addDatas("ZhangSan", "female", 32, "[email protected]", "13920199836")
.addDatas("Lisi", "male", 28, "[email protected]", "13635781534")
.addDatas("WangWu", "male", 48, "[email protected]", "15809876236")
.addDatas(rowDatas)
.render();
Assert.assertEquals(TABLE, table);
}
Expand All @@ -58,14 +69,18 @@ public void renderWithException() {
*/
@Test
public void renderWithLongTitle() {
List<Object> headers = new ArrayList<Object>();
headers.add(12345);
headers.add("abcde");

String table = new MiniTable("This is a too long title.")
.addHeaders("col11", "col12")
.addHeaders(headers)
.render();
Assert.assertEquals(""
+ "+---------------+\n"
+ "| This is a too |\n"
+ "+-------+-------+\n"
+ "| col11 | col12 |\n"
+ "| 12345 | abcde |\n"
+ "+-------+-------+\n", table);
}

Expand Down

0 comments on commit bce5e3c

Please sign in to comment.