Skip to content

Commit 21ce0f7

Browse files
awalter17imagejan
authored andcommitted
Make table methods more generic
1 parent 7f2cb1e commit 21ce0f7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @author Curtis Rueden
4242
* @param <T> The type of data stored in the table.
4343
*/
44-
public abstract class AbstractTable<C extends Column<T>, T> extends
44+
public abstract class AbstractTable<C extends Column<? extends T>, T> extends
4545
SizableArrayList<C> implements Table<C, T>
4646
{
4747

@@ -398,14 +398,14 @@ public int getRowIndex(final String header) {
398398
@Override
399399
public void set(final int col, final int row, final T value) {
400400
check(col, row);
401-
get(col).set(row, value);
401+
assign((Column<?>) get(col), row, value);
402402
}
403403

404404
@Override
405405
public void set(final String colHeader, final int row, final T value) {
406406
final int col = colIndex(colHeader);
407407
checkRow(row, 1);
408-
get(col).set(row, value);
408+
assign((Column<?>) get(col), row, value);
409409
}
410410

411411
@Override
@@ -495,6 +495,22 @@ private int colIndex(final String header) {
495495
return col;
496496
}
497497

498+
/**
499+
* Generics-friendly helper method for {@link #set(int, int, Object)} and
500+
* {@link #set(String, int, Object)}.
501+
*/
502+
private <U> void assign(final Column<U> column, final int row,
503+
final Object value)
504+
{
505+
if (value != null && !column.getType().isInstance(value)) {
506+
throw new IllegalArgumentException("value of type " + value.getClass() +
507+
" is not a " + column.getType());
508+
}
509+
@SuppressWarnings("unchecked")
510+
final U typedValue = (U) value;
511+
column.set(row, typedValue);
512+
}
513+
498514
/**
499515
* Returns true iff both objects are null, or the objects are equal via
500516
* {@link Object#equals}.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @param <C> The type of column used by the table.
4444
* @param <T> The type of data stored in the table.
4545
*/
46-
public interface Table<C extends Column<T>, T> extends List<C> {
46+
public interface Table<C extends Column<? extends T>, T> extends List<C> {
4747

4848
/** Gets the number of columns in the table. */
4949
int getColumnCount();

0 commit comments

Comments
 (0)