diff --git a/src/main/java/com/blinkfox/minitable/MiniTable.java b/src/main/java/com/blinkfox/minitable/MiniTable.java index 7b9f1ed..101c2b1 100644 --- a/src/main/java/com/blinkfox/minitable/MiniTable.java +++ b/src/main/java/com/blinkfox/minitable/MiniTable.java @@ -53,6 +53,16 @@ private void init() { this.maxColMap = new HashMap(); } + /** + * 将集合中的元素添加到表格中的表头数据中. + * + * @param headers 表头数据 + * @return MiniTable对象 + */ + public MiniTable addHeaders(List headers) { + return this.appendRows(RowType.HEADER, headers.toArray()); + } + /** * 向表格中添加表头数据. * @@ -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) { @@ -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("向表格中插入某行数据的列数与之前的列数不同,请检查!"); } // 遍历传入的数据集合,并将其存入到行数据对象中. diff --git a/src/test/java/com/blinkfox/minitable/MiniTableTest.java b/src/test/java/com/blinkfox/minitable/MiniTableTest.java index c7f0e0b..d1d1a40 100644 --- a/src/test/java/com/blinkfox/minitable/MiniTableTest.java +++ b/src/test/java/com/blinkfox/minitable/MiniTableTest.java @@ -1,5 +1,8 @@ package com.blinkfox.minitable; +import java.util.ArrayList; +import java.util.List; + import org.junit.Assert; import org.junit.Test; @@ -31,6 +34,14 @@ public class MiniTableTest { */ @Test public void render() { + // 构造一行List集合类型的普通数据,用于测试List集合数据的添加. + List rowDatas = new ArrayList(); + rowDatas.add("WangWu"); + rowDatas.add("male"); + rowDatas.add(48); + rowDatas.add("wangwu@163.com"); + rowDatas.add("15809876236"); + String table = new MiniTable("The Title") .addHeaders("Name", "Sex", "Age", "Email", "Phone") .addDatas("LiLei", "male", 25, "lilei@gmail.com", "13809345219") @@ -38,7 +49,7 @@ public void render() { .addHeaders("Name", "Sex", "Age", "Email", "Phone") .addDatas("ZhangSan", "female", 32, "zhangsan@gmail.com", "13920199836") .addDatas("Lisi", "male", 28, "lisi@qq.com", "13635781534") - .addDatas("WangWu", "male", 48, "wangwu@163.com", "15809876236") + .addDatas(rowDatas) .render(); Assert.assertEquals(TABLE, table); } @@ -58,14 +69,18 @@ public void renderWithException() { */ @Test public void renderWithLongTitle() { + List headers = new ArrayList(); + 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); }