@@ -17,6 +17,7 @@ public class BasicTable extends AbstractEntity implements Table{
17
17
private List <String > colNames = new ArrayList <String >();
18
18
private Map <String , Integer > colNamesIndex = new HashMap <String , Integer >();
19
19
private int [] colCompresses = null ;
20
+ private int colRows ;
20
21
21
22
public BasicTable (ExtendedDataInput in ) throws IOException {
22
23
int rows = in .readInt ();
@@ -60,6 +61,7 @@ public BasicTable(ExtendedDataInput in) throws IOException{
60
61
}
61
62
if (vector .rows () != rows && vector .rows ()!= 1 )
62
63
throw new IOException ("The number of rows for column " + colNames .get (i ) + " is not consistent with other columns" );
64
+ this .colRows = rows ;
63
65
columns .add (vector );
64
66
}
65
67
if (collection != null )
@@ -70,10 +72,11 @@ public BasicTable(final List<String> colNames, final List<Vector> cols) {
70
72
if (colNames .size () != cols .size ()){
71
73
throw new Error ("The length of column name and column data is unequal." );
72
74
}
73
- int rowsCount = cols .get (0 ).rows ();
75
+
76
+ this .colRows = cols .get (0 ).rows ();
74
77
for (int i =0 ;i <cols .size ();i ++) {
75
78
Vector v = cols .get (i );
76
- if (v .rows () != rowsCount )
79
+ if (v .rows () != this . colRows )
77
80
throw new Error ("The length of column " + colNames .get (i ) + " must be the same as the first column length." );
78
81
}
79
82
this .setColName (colNames );
@@ -126,14 +129,17 @@ public void setColName (final List<String> colNames) {
126
129
* @param newColName
127
130
*/
128
131
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." );
131
137
132
138
if (this .colNames .contains (originalColName )) {
133
139
int index = colNames .indexOf (originalColName );
134
140
colNames .set (index , newColName );
135
141
} 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." );
137
143
}
138
144
}
139
145
@@ -165,7 +171,7 @@ public int rows() {
165
171
if (columns ()<=0 )
166
172
return 0 ;
167
173
else
168
- return columns . get ( 0 ). rows () ;
174
+ return this . colRows ;
169
175
}
170
176
171
177
@ Override
@@ -378,22 +384,35 @@ public void addColumn(String colName, Vector col) {
378
384
if (Objects .isNull (colName ) || Objects .isNull (col ))
379
385
throw new RuntimeException ("The param 'colName' or 'col' in table cannot be null." );
380
386
387
+ if (colName .isEmpty ())
388
+ throw new RuntimeException ("The param 'colName' cannot be empty." );
389
+
381
390
if (colNames .contains (colName ))
382
391
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
+
383
396
colNames .add (colName );
384
397
colNamesIndex .put (colName , colNamesIndex .size ());
385
398
columns .add (col );
399
+ this .colRows = col .rows ();
386
400
}
387
401
388
402
@ Override
389
403
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 ();
398
417
}
399
418
}
0 commit comments