Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 5ae1774

Browse files
Add setCell method the DataTble
1 parent 23ff269 commit 5ae1774

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/main/java/com/google/visualization/datasource/datatable/DataTable.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,31 @@ public List<TableCell> getColumnCells(String columnId) {
316316
public TableCell getCell(int rowIndex, int colIndex) {
317317
return getRow(rowIndex).getCell(colIndex);
318318
}
319+
320+
/**
321+
* Replaces an existing cell at a specified position in this table with the specified cell.
322+
* The value type of the new cell must match the type of the existing one.
323+
*
324+
* @param rowIndex The row index.
325+
* @param colIndex The column index.
326+
* @param cell The cell to be stored at the specified position.
327+
*
328+
* @return The cell that was replaced.
329+
*
330+
* @throws TypeMismatchException Thrown if the new cell value type doesn't match
331+
* the table column value type.
332+
* @throws IndexOutOfBoundsException Thrown if the position is out of range.
333+
*/
334+
public TableCell setCell(int rowIndex, int colIndex, TableCell cell)
335+
throws TypeMismatchException, IndexOutOfBoundsException {
336+
TableRow row = rows.get(rowIndex);
337+
if (!row.getCell(colIndex).getType().equals(cell.getType())) {
338+
throw new TypeMismatchException("New cell value type does not match expected value type." +
339+
" Expected type: " + row.getCell(colIndex).getType() +
340+
" but was: " + cell.getType().toString());
341+
}
342+
return row.setCell(colIndex, cell);
343+
}
319344

320345
/**
321346
* Returns the value in the cell at the specified row and column indexes.

src/main/java/com/google/visualization/datasource/datatable/TableRow.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ public List<TableCell> getCells() {
115115
public TableCell getCell(int index) {
116116
return cells.get(index);
117117
}
118+
119+
/**
120+
* Package protected function.
121+
* Replaces the cell at the specified position in this row with the specified cell.
122+
* The value type of the new cell must match the that of the replaced one.
123+
*
124+
* @param index The index of the cell to replace.
125+
* @param cell The cell to be stored at the specified position.
126+
*
127+
* @return The cell that was replaced.
128+
*
129+
* @throws IndexOutOfBoundsException Thrown if the index out of range.
130+
*/
131+
TableCell setCell(int index, TableCell cell) throws IndexOutOfBoundsException {
132+
return cells.set(index, cell);
133+
}
118134

119135
/**
120136
* Retrieves a custom property. Returns null if it does not exist.

src/test/java/com/google/visualization/datasource/datatable/DataTableTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,48 @@ public void testGetCellAndGetValue() {
333333
TableCell cell = testData.getCell(1, 0);
334334
assertEquals("$ccc", cell.getFormattedValue());
335335
}
336+
337+
public void testReplaceCell() throws Exception {
338+
DataTable dataTable = testData.clone();
339+
340+
TableCell newNumberValueCell = new TableCell(new NumberValue(5699), "5699$");
341+
newNumberValueCell.setCustomProperty("testProperty", "A test property");
342+
TableCell newBooleanValueCell = new TableCell(false);
343+
TableCell newDateValueCell = new TableCell(new DateValue(2011, 3, 6), "03/06/2011");
344+
345+
dataTable.setCell(1, 1, newNumberValueCell);
346+
dataTable.setCell(2, 2, newBooleanValueCell);
347+
dataTable.setCell(2, 3, newDateValueCell);
348+
349+
assertEquals(newNumberValueCell.getValue(), dataTable.getValue(1, 1));
350+
assertEquals(newNumberValueCell.getFormattedValue(),
351+
dataTable.getCell(1,1).getFormattedValue());
352+
assertEquals(newNumberValueCell.getCustomProperty("testProperty"),
353+
dataTable.getCell(1,1).getCustomProperty("testProperty"));
354+
assertEquals(newBooleanValueCell.getValue(), dataTable.getValue(2, 2));
355+
assertEquals(newDateValueCell.getValue(), dataTable.getValue(2, 3));
356+
assertEquals(newDateValueCell.getFormattedValue(),
357+
dataTable.getCell(2,3).getFormattedValue());
358+
359+
try {
360+
dataTable.setCell(3, 3, new TableCell(100));
361+
fail();
362+
} catch (TypeMismatchException e) {
363+
// Expected behavior.
364+
}
365+
366+
try {
367+
dataTable.setCell(1, 1, new TableCell(false));
368+
fail();
369+
} catch (TypeMismatchException e) {
370+
// Expected behavior.
371+
}
372+
373+
try {
374+
dataTable.setCell(20, 20, new TableCell(100));
375+
fail();
376+
} catch (IndexOutOfBoundsException e) {
377+
// Expected behavior.
378+
}
379+
}
336380
}

0 commit comments

Comments
 (0)