Table Collection


/ Published in: Groovy
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. class Table {
  2.  
  3. def colNames = []
  4. def rowNames = []
  5. def cells = [:]
  6. def topLeft = ''
  7.  
  8. def addCol(colName) {
  9. if (!(colName in colNames)) colNames << colName
  10. }
  11.  
  12. def addRow(rowName) {
  13. if (!(rowName in rowNames)) rowNames << rowName
  14. }
  15.  
  16. def get(rowName, colName) {
  17. cells.get( computeKey(rowName, colName) )
  18. }
  19.  
  20. def put(rowName, colName, value) {
  21. if (!(rowName in rowNames)) rowNames << rowName
  22. if (!(colName in colNames)) colNames << colName
  23. def endValue = value
  24. if (value instanceof Closure) {
  25. endValue = value.call(get(rowName, colName))
  26. }
  27. cells.put(computeKey(rowName, colName), endValue)
  28. }
  29.  
  30. def fill(value) {
  31. rowNames.each{rowName ->
  32. colNames.each{colName -> put(rowName, colName, value)}
  33. }
  34. }
  35.  
  36. def fillIfNull(value) {
  37. rowNames.each{rowName ->
  38. colNames.each{colName -> put(rowName, colName){ it ?: value } }
  39. }
  40. }
  41.  
  42. def getRowCount() {
  43. rowNames.size()
  44. }
  45.  
  46. def getColCount() {
  47. colNames.size()
  48. }
  49.  
  50. def toListOfLists(withRowNames=true, withColNames=true) {
  51. def result = withColNames ? [[topLeft, *colNames]] : []
  52. rowNames.each{rowName ->
  53. def row = withRowNames ? [rowName] : []
  54. colNames.each{colName ->
  55. row << get(rowName, colName)
  56. }
  57. result << row
  58. }
  59. result
  60. }
  61.  
  62. def each(Closure closure) {
  63. rowNames.each{rowName ->
  64. colNames.each{colName ->
  65. closure.call(rowName, colName, get(rowName, colName))
  66. }
  67. }
  68. }
  69.  
  70. private computeKey(rowName, colName) {
  71. [rowName, colName]
  72. }
  73.  
  74. }
  75.  
  76.  
  77.  
  78. class TableTests extends GroovyTestCase {
  79.  
  80. def table = new Table(colNames: ['c1', 'c2'], rowNames: ['r1', 'r2'])
  81.  
  82. void testHasNullAsDefaultCellValue() {
  83. assertNull table.get('r1', 'c1')
  84. }
  85.  
  86. void testCountsRows() {
  87. assertEquals 2, table.rowCount
  88. }
  89.  
  90. void testCountsColumns() {
  91. assertEquals 2, table.colCount
  92. }
  93.  
  94. void testAddsRow() {
  95. table.addRow('r3')
  96. assertEquals 3, table.rowCount
  97. }
  98.  
  99. void testAddsColumn() {
  100. table.addCol('c3')
  101. assertEquals 3, table.colCount
  102. }
  103.  
  104. void testHasUniqueColumnNames() {
  105. table.addCol('c1')
  106. assertEquals(['c1', 'c2'], table.colNames)
  107. }
  108.  
  109. void testHasUniqueRowNames() {
  110. table.addRow('r1')
  111. assertEquals(['r1', 'r2'], table.rowNames)
  112. }
  113.  
  114. void testAddsColumnOnDemand() {
  115. table.put('r1', 'c3', '')
  116. assertEquals(['c1', 'c2', 'c3'], table.colNames)
  117. }
  118.  
  119. void testAddsRowOnDemand() {
  120. table.put('r3', 'c1', '')
  121. assertEquals(['r1', 'r2', 'r3'], table.rowNames)
  122. }
  123.  
  124. void testHasNullDefaultValueInCells() {
  125. assertNull table.get('r1', 'c1')
  126. }
  127.  
  128. void testPutsValueInCell() {
  129. table.put('r1', 'c1', 'value')
  130. assertEquals 'value', table.get('r1', 'c1')
  131. }
  132.  
  133. void testPutsClosureResultInCell() {
  134. table.put('r1', 'c1', 'value')
  135. table.put('r1', 'c1'){ it + '2'}
  136. assertEquals 'value2', table.get('r1', 'c1')
  137. }
  138.  
  139. void testFillsCells() {
  140. table.fill(1)
  141. assertEquals( [[1, 1], [1, 1]], table.toListOfLists(false, false) )
  142. }
  143.  
  144. void testFillsNullCells() {
  145. table.put('r1', 'c1', 2)
  146. table.fillIfNull(1)
  147. assertEquals( [[2, 1], [1, 1]], table.toListOfLists(false, false) )
  148. }
  149.  
  150. void testConvertsContentToListOfLists() {
  151. table.put('r1', 'c1', 'r1c1')
  152. table.put('r1', 'c2', 'r1c2')
  153. table.put('r2', 'c1', 'r2c1')
  154. table.put('r2', 'c2', 'r2c2')
  155. table.topLeft = 'x'
  156. assertEquals( [['x', 'c1', 'c2'], ['r1', 'r1c1', 'r1c2'], ['r2', 'r2c1', 'r2c2']], table.toListOfLists() )
  157. }
  158.  
  159. void testIteratesOverCells() {
  160. table.fill(1)
  161. def result = []
  162. table.each{rowName, colName, value -> result << [rowName, colName, value]}
  163. assertEquals( [['r1', 'c1', 1], ['r1', 'c2', 1], ['r2', 'c1', 1], ['r2', 'c2', 1]], result)
  164. }
  165.  
  166. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.