@@ -22,6 +22,9 @@ public class BasicTable extends AbstractEntity implements Table{
2222
2323 private static final Logger log = LoggerFactory .getLogger (BasicTable .class );
2424
25+ public BasicTable () {
26+ }
27+
2528 public BasicTable (ExtendedDataInput in ) throws IOException {
2629 int rows = in .readInt ();
2730 int cols = in .readInt ();
@@ -71,20 +74,59 @@ public BasicTable(ExtendedDataInput in) throws IOException{
7174 }
7275
7376 public BasicTable (final List <String > colNames , final List <Vector > cols ) {
77+ if (Objects .isNull (colNames ))
78+ throw new RuntimeException ("The param 'colNames' in table cannot be null." );
79+ if (Objects .isNull (cols ))
80+ throw new RuntimeException ("The param 'cols' in table cannot be null." );
81+
82+ Utils .validateColumnNames (colNames );
83+
7484 if (colNames .size () != cols .size ()){
7585 throw new Error ("The length of column name and column data is unequal." );
7686 }
7787
78- int rowsCount = cols . get ( 0 ). rows () ;
88+ int rowsCount = - 1 ;
7989 for (int i =0 ;i <cols .size ();i ++) {
8090 Vector v = cols .get (i );
81- if (v .rows () != rowsCount )
91+ if (Objects .isNull (v ))
92+ throw new RuntimeException ("Column [" + colNames .get (i ) + "] is null." );
93+ if (i == 0 )
94+ rowsCount = v .rows ();
95+ else if (v .rows () != rowsCount )
8296 throw new Error ("The length of column " + colNames .get (i ) + " must be the same as the first column length." );
8397 }
8498 this .setColName (colNames );
8599 this .setColumns (cols );
86100 }
87101
102+ /**
103+ * @param colNames
104+ * @param cols: only supports List input;
105+ */
106+ public BasicTable (final List <String > colNames , final Collection <?> cols ) {
107+ this (colNames , Utils .inferAndConvertJavaColumns (colNames , cols ));
108+ }
109+
110+ public BasicTable (final List <String > colNames , final Object [] cols ) {
111+ this (colNames , Utils .inferAndConvertJavaColumns (colNames , cols ));
112+ }
113+
114+ public BasicTable (final List <String > colNames , final List <?> cols , final DATA_TYPE [] colTypes ) {
115+ this (colNames , cols , colTypes , null );
116+ }
117+
118+ public BasicTable (final List <String > colNames , final List <?> cols , final DATA_TYPE [] colTypes , final int [] colExtraParams ) {
119+ this (colNames , Utils .convertColumns (colNames , cols , colTypes , colExtraParams ));
120+ }
121+
122+ public BasicTable (final List <String > colNames , final Object [] cols , final DATA_TYPE [] colTypes ) {
123+ this (colNames , cols == null ? null : Arrays .asList (cols ), colTypes );
124+ }
125+
126+ public BasicTable (final List <String > colNames , final Object [] cols , final DATA_TYPE [] colTypes , final int [] colExtraParams ) {
127+ this (colNames , cols == null ? null : Arrays .asList (cols ), colTypes , colExtraParams );
128+ }
129+
88130 public void setColumnCompressTypes (int [] colCompresses ) {
89131 if (colCompresses !=null && colCompresses .length != columns .size ()) {
90132 throw new RuntimeException ("Compress type size must match column size " + columns .size ()+"." );
@@ -121,6 +163,7 @@ public int[] getColumnCompressTypes() {
121163 * @param colNames
122164 */
123165 public void setColName (final List <String > colNames ) {
166+ Utils .validateColumnNames (colNames );
124167 this .colNames .clear ();
125168 colNamesIndex .clear ();
126169 for (String name : colNames ){
@@ -184,6 +227,8 @@ public String getColumnName(int index){
184227 }
185228
186229 public String getString (){
230+ if (columns () == 0 )
231+ return "" ;
187232 int rows = Math .min (Utils .DISPLAY_ROWS ,rows ());
188233 int strColMaxWidth = Utils .DISPLAY_WIDTH /Math .min (columns (),Utils .DISPLAY_COLS )+5 ;
189234 int length =0 ;
@@ -376,12 +421,30 @@ public void addColumn(String colName, Vector col) {
376421 if (colNames .contains (colName ))
377422 throw new RuntimeException ("The table already contains column '" + colName + "'." );
378423
379- if (Objects .nonNull (this .columns ) && Objects .nonNull (this .columns .get (0 )) && this .getColumn (0 ).rows () != col .rows ())
424+ if (Objects .nonNull (this .columns ) && ! this . columns . isEmpty () && Objects .nonNull (this .columns .get (0 )) && this .getColumn (0 ).rows () != col .rows ())
380425 throw new RuntimeException ("The length of column " + colName + " must be the same as the first column length: " + this .getColumn (0 ).rows () +"." );
381-
426+
382427 colNames .add (colName );
383428 colNamesIndex .put (colName , colNamesIndex .size ());
384429 columns .add (col );
430+ if (colCompresses != null ) {
431+ colCompresses = Arrays .copyOf (colCompresses , colCompresses .length + 1 );
432+ colCompresses [colCompresses .length - 1 ] = Vector .COMPRESS_LZ4 ;
433+ }
434+ }
435+
436+ public void addColumn (String colName , List <?> col ) {
437+ if (Objects .isNull (colName ) || Objects .isNull (col ))
438+ throw new RuntimeException ("The param 'colName' or 'col' in table cannot be null." );
439+
440+ addColumn (colName , Utils .inferAndConvertJavaColumn (colName , col ));
441+ }
442+
443+ public void addColumn (String colName , Object [] col ) {
444+ if (Objects .isNull (colName ) || Objects .isNull (col ))
445+ throw new RuntimeException ("The param 'colName' or 'col' in table cannot be null." );
446+
447+ addColumn (colName , Utils .inferAndConvertJavaColumn (colName , col ));
385448 }
386449
387450 /**
0 commit comments