scoop:scoop.ui.component:0.1.0
src/table/source.scoop
Selected production source · src/table/source.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.ViewKey6
public interface TableSource<Row> {7
public val count: Long8
public fun keyAt(index: Long): ViewKey9
public fun indexOfKey(key: ViewKey): Long?10
public fun rowAt(index: Long): Row11
}13
public 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. */16
public 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].key23
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.size31
public override fun keyAt(index: Long): ViewKey = rows[index].key32
public override fun indexOfKey(key: ViewKey): Long? = positions[key]33
public override fun rowAt(index: Long): Row = rows[index].value34
}36
public enum TableData<Row> {37
Local(TableSource<Row>), Provided(TableSource<Row>, Long)38
}40
public class TableResult<Row> internal constructor(41
public val rows: TableSource<Row>,42
public val totalRows: Long,43
public val pageCount: Long44
)46
internal fun <Row> tableDataSource(data: TableData<Row>): TableSource<Row> =47
when (data) {48
case Local(val source) -> source49
case Provided(val source, _) -> source50
}52
internal class TableSlice<Row>(53
private val source: TableSource<Row>,54
private val start: Long,55
public override val count: Long56
): 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 + index60
}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 None65
return if (at >= start && at - start < count)66
Some(at - start)67
else68
None69
}70
}72
internal 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.size82
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])85
}