Return to Snippet

Revision: 5195
at February 18, 2008 04:15 by roock


Initial Code
class Table {

	def colNames = []
	def rowNames = []
	def cells = [:]
	def topLeft = ''
	
	def addCol(colName) {
		if (!(colName in colNames)) colNames << colName
	}
	
	def addRow(rowName) {
		if (!(rowName in rowNames)) rowNames << rowName
	}
	
	def get(rowName, colName) {
		cells.get( computeKey(rowName, colName) )
	}
	
	def put(rowName, colName, value) {
		if (!(rowName in rowNames)) rowNames << rowName
		if (!(colName in colNames)) colNames << colName
		def endValue = value
		if (value instanceof Closure) {
			endValue = value.call(get(rowName, colName))
		}
		cells.put(computeKey(rowName, colName), endValue)
	}
	
	def fill(value) {
		rowNames.each{rowName ->
			colNames.each{colName -> put(rowName, colName, value)}
		}
	}
	
	def fillIfNull(value) {
		rowNames.each{rowName ->
			colNames.each{colName -> put(rowName, colName){ it ?: value } }
		}
	}
	
	def getRowCount() {
		rowNames.size()
	}
	
	def getColCount() {
		colNames.size()
	}
	
	def toListOfLists(withRowNames=true, withColNames=true) {
		def result = withColNames ? [[topLeft, *colNames]] : []
		rowNames.each{rowName ->
			def row = withRowNames ? [rowName] : []
			colNames.each{colName ->
				row << get(rowName, colName)
			}
			result << row
		}
		result
	}
	
	def each(Closure closure) {
		rowNames.each{rowName ->
			colNames.each{colName ->
				closure.call(rowName, colName, get(rowName, colName))
			}
		}
	}
	
	private computeKey(rowName, colName) {
		[rowName, colName]
	}
	
}



class TableTests extends GroovyTestCase {

	def table = new Table(colNames: ['c1', 'c2'], rowNames: ['r1', 'r2'])
	
	void testHasNullAsDefaultCellValue() {
		assertNull table.get('r1', 'c1')
	}

	void testCountsRows() {
		assertEquals 2, table.rowCount
	}		
	
	void testCountsColumns() {
		assertEquals 2, table.colCount
	}		
	
	void testAddsRow() {
		table.addRow('r3')
		assertEquals 3, table.rowCount
	}
		
	void testAddsColumn() {
		table.addCol('c3')
		assertEquals 3, table.colCount
	}
	
	void testHasUniqueColumnNames() {
		table.addCol('c1')
		assertEquals(['c1', 'c2'], table.colNames)
	}
	
	void testHasUniqueRowNames() {
		table.addRow('r1')
		assertEquals(['r1', 'r2'], table.rowNames)
	}
	
	void testAddsColumnOnDemand() {
		table.put('r1', 'c3', '')
		assertEquals(['c1', 'c2', 'c3'], table.colNames)
	}
	
	void testAddsRowOnDemand() {
		table.put('r3', 'c1', '')
		assertEquals(['r1', 'r2', 'r3'], table.rowNames)
	}
	
	void testHasNullDefaultValueInCells() {
		assertNull table.get('r1', 'c1')
	}
	
	void testPutsValueInCell() {
		table.put('r1', 'c1', 'value')
		assertEquals 'value', table.get('r1', 'c1')
	}
	
	void testPutsClosureResultInCell() {
		table.put('r1', 'c1', 'value')
		table.put('r1', 'c1'){ it + '2'}
		assertEquals 'value2', table.get('r1', 'c1')
	}

	void testFillsCells() {
		table.fill(1)
		assertEquals( [[1, 1], [1, 1]], table.toListOfLists(false, false) )
	}
	
	void testFillsNullCells() {
		table.put('r1', 'c1', 2)
		table.fillIfNull(1)
		assertEquals( [[2, 1], [1, 1]], table.toListOfLists(false, false) )
	}
	
	void testConvertsContentToListOfLists() {
		table.put('r1', 'c1', 'r1c1')
		table.put('r1', 'c2', 'r1c2')
		table.put('r2', 'c1', 'r2c1')
		table.put('r2', 'c2', 'r2c2')
		table.topLeft = 'x'
		assertEquals( [['x', 'c1', 'c2'], ['r1', 'r1c1', 'r1c2'], ['r2', 'r2c1', 'r2c2']], table.toListOfLists() )
	}
	
	void testIteratesOverCells() {
		table.fill(1)
		def result = []
		table.each{rowName, colName, value -> result << [rowName, colName, value]}
		assertEquals( [['r1', 'c1', 1], ['r1', 'c2', 1], ['r2', 'c1', 1], ['r2', 'c2', 1]], result)
	}
	
}

Initial URL

                                

Initial Description
Table as a MAP with 2D keys (row and column).

Initial Title
Table Collection

Initial Tags
table

Initial Language
Groovy