@@ -17,6 +17,7 @@ public class BasicTable extends AbstractEntity implements Table{
1717 private List <String > colNames = new ArrayList <String >();
1818 private Map <String , Integer > colNamesIndex = new HashMap <String , Integer >();
1919 private int [] colCompresses = null ;
20+ private int colRows ;
2021
2122 public BasicTable (ExtendedDataInput in ) throws IOException {
2223 int rows = in .readInt ();
@@ -60,6 +61,7 @@ public BasicTable(ExtendedDataInput in) throws IOException{
6061 }
6162 if (vector .rows () != rows && vector .rows ()!= 1 )
6263 throw new IOException ("The number of rows for column " + colNames .get (i ) + " is not consistent with other columns" );
64+ this .colRows = rows ;
6365 columns .add (vector );
6466 }
6567 if (collection != null )
@@ -70,10 +72,11 @@ public BasicTable(final List<String> colNames, final List<Vector> cols) {
7072 if (colNames .size () != cols .size ()){
7173 throw new Error ("The length of column name and column data is unequal." );
7274 }
73- int rowsCount = cols .get (0 ).rows ();
75+
76+ this .colRows = cols .get (0 ).rows ();
7477 for (int i =0 ;i <cols .size ();i ++) {
7578 Vector v = cols .get (i );
76- if (v .rows () != rowsCount )
79+ if (v .rows () != this . colRows )
7780 throw new Error ("The length of column " + colNames .get (i ) + " must be the same as the first column length." );
7881 }
7982 this .setColName (colNames );
@@ -126,14 +129,17 @@ public void setColName (final List<String> colNames) {
126129 * @param newColName
127130 */
128131 public void replaceColName (String originalColName , String newColName ) {
129- if (Utils .isEmpty (originalColName ))
130- throw new RuntimeException ("colName cannot be null." );
132+ if (Utils .isEmpty (originalColName ) || Utils .isEmpty (newColName ))
133+ throw new RuntimeException ("The param 'newColName' cannot be null or empty." );
134+
135+ if (!this .colNames .contains (originalColName ) && this .colNames .contains (newColName ))
136+ throw new RuntimeException ("The newColName '" + newColName +"' already exists in table. Column names cannot be duplicated." );
131137
132138 if (this .colNames .contains (originalColName )) {
133139 int index = colNames .indexOf (originalColName );
134140 colNames .set (index , newColName );
135141 } else {
136- throw new RuntimeException ("colName '" + originalColName +"' does not exist in table." );
142+ throw new RuntimeException ("The param originalColName '" + originalColName +"' does not exist in table." );
137143 }
138144 }
139145
@@ -165,7 +171,7 @@ public int rows() {
165171 if (columns ()<=0 )
166172 return 0 ;
167173 else
168- return columns . get ( 0 ). rows () ;
174+ return this . colRows ;
169175 }
170176
171177 @ Override
@@ -378,22 +384,35 @@ public void addColumn(String colName, Vector col) {
378384 if (Objects .isNull (colName ) || Objects .isNull (col ))
379385 throw new RuntimeException ("The param 'colName' or 'col' in table cannot be null." );
380386
387+ if (colName .isEmpty ())
388+ throw new RuntimeException ("The param 'colName' cannot be empty." );
389+
381390 if (colNames .contains (colName ))
382391 throw new RuntimeException ("The table already contains column '" + colName + "'." );
392+
393+ if (this .colRows != 0 && col .rows () != this .colRows )
394+ throw new RuntimeException ("The length of column " + colName + " must be the same as the first column length: " + this .colRows +"." );
395+
383396 colNames .add (colName );
384397 colNamesIndex .put (colName , colNamesIndex .size ());
385398 columns .add (col );
399+ this .colRows = col .rows ();
386400 }
387401
388402 @ Override
389403 public void replaceColumn (String colName , Vector col ) {
390- if (colNames .contains (colName )) {
391- int index = colNames .indexOf (colName );
392- columns .set (index , col );
393- } else {
394- colNames .add (colName );
395- columns .add (col );
396- colNamesIndex .put (colName , colNamesIndex .size ());
397- }
404+ if (Objects .isNull (colName ) || Objects .isNull (col ))
405+ throw new RuntimeException ("The param 'colName' or 'col' in table cannot be null." );
406+
407+ if (!colNames .contains (colName ))
408+ throw new RuntimeException ("The column '" + colName + "' to be replaced doesn't exist in the table." );
409+
410+ if (this .colRows != 0 && col .rows () != this .colRows )
411+ throw new RuntimeException ("The length of column " + colName + " must be the same as the first column length: " + this .colRows +"." );
412+
413+ colNames .add (colName );
414+ colNamesIndex .put (colName , colNamesIndex .size ());
415+ columns .add (col );
416+ this .colRows = col .rows ();
398417 }
399418}
0 commit comments