Skip to content

Commit c902d40

Browse files
awalter17imagejan
authored andcommitted
Override list add methods to update rowCount
1 parent 5f3b823 commit c902d40

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/main/java/net/imagej/table/AbstractTable.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package net.imagej.table;
3333

3434
import java.util.ArrayList;
35+
import java.util.Collection;
3536

3637
import org.scijava.util.SizableArrayList;
3738

@@ -421,6 +422,40 @@ public T get(final String colHeader, final int row) {
421422
return get(col).get(row);
422423
}
423424

425+
// -- List methods --
426+
427+
@Override
428+
public boolean add(final C column) {
429+
if (column.size() > rowCount) rowCount = column.size();
430+
scaleColumns();
431+
return super.add(column);
432+
}
433+
434+
@Override
435+
public void add(final int col, final C column) {
436+
super.add(col, column);
437+
if (column.size() > rowCount) rowCount = column.size();
438+
scaleColumns();
439+
}
440+
441+
@Override
442+
public boolean addAll(final Collection<? extends C> c) {
443+
for (final C column : c) {
444+
if (column.size() > rowCount) rowCount = column.size();
445+
}
446+
scaleColumns();
447+
return super.addAll(c);
448+
}
449+
450+
@Override
451+
public boolean addAll(final int col, final Collection<? extends C> c) {
452+
for (final C column : c) {
453+
if (column.size() > rowCount) rowCount = column.size();
454+
}
455+
scaleColumns();
456+
return super.addAll(col, c);
457+
}
458+
424459
// -- Internal methods --
425460

426461
protected abstract C createColumn(final String header);

src/main/java/net/imagej/table/Table.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ public interface Table<C extends Column<? extends T>, T> extends List<C> {
270270
/**
271271
* Appends the specified column to the end of the table.
272272
* <p>
273-
* No checking is done to ensure the new column has the same number of rows as
274-
* the other existing columns.
273+
* Updates the row count if this column has more rows than current table and
274+
* scales the existing columns to have the same number of rows.
275275
* </p>
276276
*/
277277
@Override
@@ -298,8 +298,8 @@ public interface Table<C extends Column<? extends T>, T> extends List<C> {
298298
* table, in the order that they are returned by the specified collection's
299299
* iterator.
300300
* <p>
301-
* No checking is done to ensure the new columns have the same number of rows
302-
* as the other existing columns.
301+
* Updates the row count if necessary, and scales the columns to match the row
302+
* count if necessary.
303303
* </p>
304304
*
305305
* @return <tt>true</tt> if the table changed as a result of the call
@@ -311,8 +311,8 @@ public interface Table<C extends Column<? extends T>, T> extends List<C> {
311311
* Inserts all of the columns in the specified collection into this list at
312312
* the specified position.
313313
* <p>
314-
* No checking is done to ensure the new columns have the same number of rows
315-
* as the other existing columns.
314+
* Updates the row count if necessary, and scales the columns to match the row
315+
* count if necessary.
316316
* </p>
317317
*
318318
* @return <tt>true</tt> if the table changed as a result of the call
@@ -371,8 +371,8 @@ public interface Table<C extends Column<? extends T>, T> extends List<C> {
371371
/**
372372
* Inserts the specified column at the specified position in the table.
373373
* <p>
374-
* No checking is done to ensure the new column has the same number of rows as
375-
* the other existing columns.
374+
* Updates the row count if this column has more rows than current table and
375+
* scales the existing columns to have the same number of rows.
376376
* </p>
377377
*/
378378
@Override

0 commit comments

Comments
 (0)