@@ -392,7 +392,7 @@ protected void parseAwfulCSV(BufferedReader reader,
392392 count = 0 ;
393393 // starting a new column, make sure we have room
394394 col ++;
395- checkColumn (col );
395+ ensureColumn (col );
396396
397397 } else { // just a regular character, add it
398398 if (count == c .length ) {
@@ -1309,15 +1309,15 @@ public String[] removeTitleRow() {
13091309
13101310 public void setColumnTitles (String [] titles ) {
13111311 if (titles != null ) {
1312- checkColumn (titles .length - 1 );
1312+ ensureColumn (titles .length - 1 );
13131313 }
13141314 columnTitles = titles ;
13151315 columnIndices = null ; // remove the cache
13161316 }
13171317
13181318
13191319 public void setColumnTitle (int column , String title ) {
1320- checkColumn (column );
1320+ ensureColumn (column );
13211321 if (columnTitles == null ) {
13221322 columnTitles = new String [getColumnCount ()];
13231323 }
@@ -1449,6 +1449,37 @@ public TableRow addRow() {
14491449 }
14501450
14511451
1452+ public TableRow addRow (TableRow source ) {
1453+ int row = rowCount ;
1454+ // Make sure there are enough columns to add this data
1455+ ensureBounds (row , source .getColumnCount () - 1 );
1456+
1457+ for (int col = 0 ; col < columns .length ; col ++) {
1458+ switch (columnTypes [col ]) {
1459+ case CATEGORICAL :
1460+ case INT :
1461+ setInt (row , col , source .getInt (col ));
1462+ break ;
1463+ case LONG :
1464+ setLong (row , col , source .getLong (col ));
1465+ break ;
1466+ case FLOAT :
1467+ setFloat (row , col , source .getFloat (col ));
1468+ break ;
1469+ case DOUBLE :
1470+ setDouble (row , col , source .getDouble (col ));
1471+ break ;
1472+ case STRING :
1473+ setString (row , col , source .getString (col ));
1474+ break ;
1475+ default :
1476+ throw new RuntimeException ("no types" );
1477+ }
1478+ }
1479+ return new RowPointer (this , row );
1480+ }
1481+
1482+
14521483 public TableRow addRow (Object [] columnData ) {
14531484 setRow (getRowCount (), columnData );
14541485 return new RowPointer (this , rowCount - 1 );
@@ -1609,7 +1640,7 @@ protected void setRowCol(int row, int col, String piece) {
16091640
16101641
16111642 public void setRow (int row , Object [] pieces ) {
1612- checkSize (row , pieces .length - 1 );
1643+ ensureBounds (row , pieces .length - 1 );
16131644 // pieces.length may be less than columns.length, so loop over pieces
16141645 for (int col = 0 ; col < pieces .length ; col ++) {
16151646 setRowCol (row , col , pieces [col ]);
@@ -1820,6 +1851,10 @@ public void setDouble(int column, double value) {
18201851 public void setDouble (String columnName , double value ) {
18211852 table .setDouble (row , columnName , value );
18221853 }
1854+
1855+ public int getColumnCount () {
1856+ return table .getColumnCount ();
1857+ }
18231858 }
18241859
18251860
@@ -2008,6 +2043,14 @@ private void immutable() {
20082043 throw new IllegalArgumentException ("This TableRow cannot be modified." );
20092044 }
20102045
2046+ public int getColumnCount () {
2047+ try {
2048+ return rs .getMetaData ().getColumnCount ();
2049+ } catch (SQLException e ) {
2050+ e .printStackTrace ();
2051+ return -1 ;
2052+ }
2053+ }
20112054 };
20122055 }
20132056
@@ -2049,7 +2092,7 @@ public void setInt(int row, int column, int value) {
20492092 setString (row , column , String .valueOf (value ));
20502093
20512094 } else {
2052- checkSize (row , column );
2095+ ensureBounds (row , column );
20532096 if (columnTypes [column ] != INT &&
20542097 columnTypes [column ] != CATEGORICAL ) {
20552098 throw new IllegalArgumentException ("Column " + column + " is not an int column." );
@@ -2126,7 +2169,7 @@ public void setLong(int row, int column, long value) {
21262169 setString (row , column , String .valueOf (value ));
21272170
21282171 } else {
2129- checkSize (row , column );
2172+ ensureBounds (row , column );
21302173 if (columnTypes [column ] != LONG ) {
21312174 throw new IllegalArgumentException ("Column " + column + " is not a 'long' column." );
21322175 }
@@ -2202,7 +2245,7 @@ public void setFloat(int row, int column, float value) {
22022245 setString (row , column , String .valueOf (value ));
22032246
22042247 } else {
2205- checkSize (row , column );
2248+ ensureBounds (row , column );
22062249 if (columnTypes [column ] != FLOAT ) {
22072250 throw new IllegalArgumentException ("Column " + column + " is not a float column." );
22082251 }
@@ -2277,7 +2320,7 @@ public void setDouble(int row, int column, double value) {
22772320 setString (row , column , String .valueOf (value ));
22782321
22792322 } else {
2280- checkSize (row , column );
2323+ ensureBounds (row , column );
22812324 if (columnTypes [column ] != DOUBLE ) {
22822325 throw new IllegalArgumentException ("Column " + column + " is not a 'double' column." );
22832326 }
@@ -2409,7 +2452,7 @@ public void setMissingString(String value) {
24092452
24102453
24112454 public void setString (int row , int column , String value ) {
2412- checkSize (row , column );
2455+ ensureBounds (row , column );
24132456 if (columnTypes [column ] != STRING ) {
24142457 throw new IllegalArgumentException ("Column " + column + " is not a String column." );
24152458 }
@@ -2457,7 +2500,7 @@ public String[] getStringRow(int row) {
24572500 * @param column the column to search
24582501 */
24592502 public int findRowIndex (String value , int column ) {
2460- checkBounds (- 1 , column );
2503+ checkColumn ( column );
24612504 if (columnTypes [column ] == STRING ) {
24622505 String [] stringData = (String []) columns [column ];
24632506 if (value == null ) {
@@ -2507,7 +2550,7 @@ public int[] findRowIndices(String value, int column) {
25072550 int [] outgoing = new int [rowCount ];
25082551 int count = 0 ;
25092552
2510- checkBounds (- 1 , column );
2553+ checkColumn ( column );
25112554 if (columnTypes [column ] == STRING ) {
25122555 String [] stringData = (String []) columns [column ];
25132556 if (value == null ) {
@@ -2583,7 +2626,7 @@ public Iterator<TableRow> findRows(String value, String columnName) {
25832626 * @param column the column to search
25842627 */
25852628 public int matchRowIndex (String regexp , int column ) {
2586- checkBounds (- 1 , column );
2629+ checkColumn ( column );
25872630 if (columnTypes [column ] == STRING ) {
25882631 String [] stringData = (String []) columns [column ];
25892632 for (int row = 0 ; row < rowCount ; row ++) {
@@ -2625,7 +2668,7 @@ public int[] matchRowIndices(String regexp, int column) {
26252668 int [] outgoing = new int [rowCount ];
26262669 int count = 0 ;
26272670
2628- checkBounds (- 1 , column );
2671+ checkColumn ( column );
26292672 if (columnTypes [column ] == STRING ) {
26302673 String [] stringData = (String []) columns [column ];
26312674 for (int row = 0 ; row < rowCount ; row ++) {
@@ -2727,7 +2770,7 @@ public void replaceAll(String orig, String replacement) {
27272770
27282771
27292772 public void replaceAll (String regex , String replacement , int column ) {
2730- checkBounds (- 1 , column );
2773+ checkColumn ( column );
27312774 if (columnTypes [column ] == STRING ) {
27322775 String [] stringData = (String []) columns [column ];
27332776 for (int row = 0 ; row < rowCount ; row ++) {
@@ -2829,36 +2872,52 @@ public void trim(String columnName) {
28292872 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28302873
28312874
2832- protected void checkColumn (int col ) {
2875+ /** Make sure this is a legit column, and if not, expand the table. */
2876+ protected void ensureColumn (int col ) {
28332877 if (col >= columns .length ) {
28342878 setColumnCount (col + 1 );
28352879 }
28362880 }
28372881
28382882
2839- protected void checkRow (int row ) {
2883+ /** Make sure this is a legit row, and if not, expand the table. */
2884+ protected void ensureRow (int row ) {
28402885 if (row >= rowCount ) {
28412886 setRowCount (row + 1 );
28422887 }
28432888 }
28442889
28452890
2846- protected void checkSize (int row , int col ) {
2847- checkRow (row );
2848- checkColumn (col );
2891+ /** Make sure this is a legit row and column. If not, expand the table. */
2892+ protected void ensureBounds (int row , int col ) {
2893+ ensureRow (row );
2894+ ensureColumn (col );
28492895 }
28502896
28512897
2852- protected void checkBounds (int row , int column ) {
2898+ /** Throw an error if this row doesn't exist. */
2899+ protected void checkRow (int row ) {
28532900 if (row < 0 || row >= rowCount ) {
28542901 throw new ArrayIndexOutOfBoundsException ("Row " + row + " does not exist." );
28552902 }
2903+ }
2904+
2905+
2906+ /** Throw an error if this column doesn't exist. */
2907+ protected void checkColumn (int column ) {
28562908 if (column < 0 || column >= columns .length ) {
28572909 throw new ArrayIndexOutOfBoundsException ("Column " + column + " does not exist." );
28582910 }
28592911 }
28602912
28612913
2914+ /** Throw an error if this entry is out of bounds. */
2915+ protected void checkBounds (int row , int column ) {
2916+ checkRow (row );
2917+ checkColumn (column );
2918+ }
2919+
2920+
28622921 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28632922
28642923
0 commit comments