|
41 | 41 | * @author Curtis Rueden |
42 | 42 | * @param <T> The type of data stored in the table. |
43 | 43 | */ |
44 | | -public abstract class AbstractTable<C extends Column<T>, T> extends |
| 44 | +public abstract class AbstractTable<C extends Column<? extends T>, T> extends |
45 | 45 | SizableArrayList<C> implements Table<C, T> |
46 | 46 | { |
47 | 47 |
|
@@ -398,14 +398,14 @@ public int getRowIndex(final String header) { |
398 | 398 | @Override |
399 | 399 | public void set(final int col, final int row, final T value) { |
400 | 400 | check(col, row); |
401 | | - get(col).set(row, value); |
| 401 | + assign((Column<?>) get(col), row, value); |
402 | 402 | } |
403 | 403 |
|
404 | 404 | @Override |
405 | 405 | public void set(final String colHeader, final int row, final T value) { |
406 | 406 | final int col = colIndex(colHeader); |
407 | 407 | checkRow(row, 1); |
408 | | - get(col).set(row, value); |
| 408 | + assign((Column<?>) get(col), row, value); |
409 | 409 | } |
410 | 410 |
|
411 | 411 | @Override |
@@ -495,6 +495,22 @@ private int colIndex(final String header) { |
495 | 495 | return col; |
496 | 496 | } |
497 | 497 |
|
| 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 | + |
498 | 514 | /** |
499 | 515 | * Returns true iff both objects are null, or the objects are equal via |
500 | 516 | * {@link Object#equals}. |
|
0 commit comments