Scoop API

scoop:scoop.ui.component:0.1.0

src/table/source.scoop

Selected production source · src/table/source.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.ViewKey
6public interface TableSource<Row> {
7 public val count: Long
8 public fun keyAt(index: Long): ViewKey
9 public fun indexOfKey(key: ViewKey): Long?
10 public fun rowAt(index: Long): Row
13public class TableRecord<Row> public constructor(public val key: ViewKey, public val value: Row)
15/** Immutable rows with one explicit index, independent of display text or page positions. */
16public class TableRows<Row> public constructor(
17 private val rows: Array<TableRecord<Row>>
18): TableSource<Row> {
19 private val positions = HashMap<ViewKey, Long>()
20 init {
21 for (index in 0L ..< rows.size) {
22 val key = rows[index].key
23 requireComponent(
24 key != ViewKey.Position && !positions.containsKey(key),
25 "table rows require unique explicit keys"
26 )
27 positions.put(key, index)
28 }
29 }
30 public override val count: Long get() = rows.size
31 public override fun keyAt(index: Long): ViewKey = rows[index].key
32 public override fun indexOfKey(key: ViewKey): Long? = positions[key]
33 public override fun rowAt(index: Long): Row = rows[index].value
36public enum TableData<Row> {
37 Local(TableSource<Row>), Provided(TableSource<Row>, Long)
40public class TableResult<Row> internal constructor(
41 public val rows: TableSource<Row>,
42 public val totalRows: Long,
43 public val pageCount: Long
46internal fun <Row> tableDataSource(data: TableData<Row>): TableSource<Row> =
47 when (data) {
48 case Local(val source) -> source
49 case Provided(val source, _) -> source
50 }
52internal class TableSlice<Row>(
53 private val source: TableSource<Row>,
54 private val start: Long,
55 public override val count: Long
56): TableSource<Row> {
57 private fun offset(index: Long): Long {
58 requireComponent(index >= 0L && index < count, "table row index is out of range")
59 return start + index
60 }
61 public override fun keyAt(index: Long): ViewKey = source.keyAt(offset(index))
62 public override fun rowAt(index: Long): Row = source.rowAt(offset(index))
63 public override fun indexOfKey(key: ViewKey): Long? {
64 val at = source.indexOfKey(key) ?: return None
65 return if (at >= start && at - start < count)
66 Some(at - start)
67 else
68 None
69 }
72internal class TableOrder<Row>(
73 private val source: TableSource<Row>,
74 private val indices: Array<Long>
75): TableSource<Row> {
76 private val positions = HashMap<ViewKey, Long>()
77 init {
78 for (index in 0L ..< indices.size)
79 positions.put(source.keyAt(indices[index]), index)
80 }
81 public override val count: Long get() = indices.size
82 public override fun keyAt(index: Long): ViewKey = source.keyAt(indices[index])
83 public override fun indexOfKey(key: ViewKey): Long? = positions[key]
84 public override fun rowAt(index: Long): Row = source.rowAt(indices[index])