From 126e59f81a134723c8717393c45e384a968ceef9 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 8 Feb 2020 22:01:56 +0300 Subject: [PATCH] Bottom type check for tables --- .../kotlin/hep/dataforge/tables/ColumnTable.kt | 4 ++-- .../kotlin/hep/dataforge/tables/ColumnTableBuilder.kt | 10 +++++----- .../commonMain/kotlin/hep/dataforge/tables/RowTable.kt | 6 +++--- .../commonMain/kotlin/hep/dataforge/tables/Table.kt | 8 ++++---- .../jvmMain/kotlin/hep/dataforge/tables/CastColumn.kt | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTable.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTable.kt index 07ea5d94..c48b30f8 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTable.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTable.kt @@ -5,7 +5,7 @@ import kotlin.reflect.KClass /** * @param C bottom type for all columns in the table */ -class ColumnTable(override val columns: Collection>) : Table { +class ColumnTable(override val columns: Collection>) : Table { private val rowsNum = columns.first().size init { @@ -21,7 +21,7 @@ class ColumnTable(override val columns: Collection>) : Table { } } -internal class VirtualRow(val table: Table, val index: Int) : Row { +internal class VirtualRow(val table: Table, val index: Int) : Row { override fun getValue(column: String, type: KClass): T? = table.getValue(index, column, type) } diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTableBuilder.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTableBuilder.kt index b453c957..78a32366 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTableBuilder.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnTableBuilder.kt @@ -3,10 +3,10 @@ package hep.dataforge.tables import hep.dataforge.meta.Meta import kotlin.reflect.KClass -class ColumnTableBuilder(val size: Int) : Table { - private val _columns = ArrayList>() +class ColumnTableBuilder(val size: Int) : Table { + private val _columns = ArrayList>() - override val columns: List> get() = _columns + override val columns: List> get() = _columns override val rows: List get() = (0 until size).map { VirtualRow(this, it) } @@ -19,7 +19,7 @@ class ColumnTableBuilder(val size: Int) : Table { /** * Add a fixed column to the end of the table */ - fun add(column: Column<*>) { + fun add(column: Column) { require(column.size == this.size) { "Required column size $size, but found ${column.size}" } _columns.add(column) } @@ -27,7 +27,7 @@ class ColumnTableBuilder(val size: Int) : Table { /** * Insert a column at [index] */ - fun insert(index: Int, column: Column<*>) { + fun insert(index: Int, column: Column) { require(column.size == this.size) { "Required column size $size, but found ${column.size}" } _columns.add(index, column) } diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/RowTable.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/RowTable.kt index c640aade..bc2ca808 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/RowTable.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/RowTable.kt @@ -4,13 +4,13 @@ import hep.dataforge.meta.Meta import kotlin.reflect.KClass -class RowTable(override val rows: List, override val header: TableHeader) : Table { +class RowTable(override val rows: List, override val header: List>) : Table { override fun getValue(row: Int, column: String, type: KClass): T? = rows[row].getValue(column, type) - override val columns: List> get() = header.map { RotTableColumn(it) } + override val columns: List> get() = header.map { RotTableColumn(it) } - private inner class RotTableColumn(val header: ColumnHeader) : Column { + private inner class RotTableColumn(val header: ColumnHeader) : Column { override val name: String get() = header.name override val type: KClass get() = header.type override val meta: Meta get() = header.meta diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt index 4961f9bb..96d7964d 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt @@ -16,9 +16,9 @@ internal fun KClass.cast(value: Any?): T? { typealias TableHeader = List> -interface Table { +interface Table { fun getValue(row: Int, column: String, type: KClass): T? - val columns: Collection> + val columns: Collection> val header: TableHeader get() = columns.toList() val rows: List @@ -30,7 +30,7 @@ interface Table { operator fun Collection>.get(name: String): Column<*>? = find { it.name == name } -inline operator fun Table.get(row: Int, column: String): T? = getValue(row, column, T::class) +inline operator fun Table.get(row: Int, column: String): T? = getValue(row, column, T::class) interface ColumnHeader { val name: String @@ -38,7 +38,7 @@ interface ColumnHeader { val meta: Meta } -operator fun Table.get(row: Int, column: Column): T? = getValue(row, column.name, column.type) +operator fun Table.get(row: Int, column: Column): T? = getValue(row, column.name, column.type) interface Column : ColumnHeader { val size: Int diff --git a/dataforge-tables/src/jvmMain/kotlin/hep/dataforge/tables/CastColumn.kt b/dataforge-tables/src/jvmMain/kotlin/hep/dataforge/tables/CastColumn.kt index 9b9801c8..1ac7e138 100644 --- a/dataforge-tables/src/jvmMain/kotlin/hep/dataforge/tables/CastColumn.kt +++ b/dataforge-tables/src/jvmMain/kotlin/hep/dataforge/tables/CastColumn.kt @@ -25,7 +25,7 @@ class CastColumn(val origin: Column<*>, override val type: KClass) : override fun get(index: Int): T? = type.cast(origin[index]) } -class ColumnProperty(val table: Table, val type: KClass) : ReadOnlyProperty> { +class ColumnProperty(val table: Table, val type: KClass) : ReadOnlyProperty> { override fun getValue(thisRef: Any?, property: KProperty<*>): Column { val name = property.name return (table.columns[name] ?: error("Column with name $name not found in the table")).cast(type)