
commonMain.org.daiv.tick.Timeable.kt Maven / Gradle / Ivy
package org.daiv.tick
import kotlinx.serialization.Serializable
import org.daiv.time.isoTime
import org.daiv.util.binarySearchByEnd
import org.daiv.util.binarySearchByStart
import kotlin.reflect.KClass
fun List.second() = get(1)
fun List.secondToLast() = get(size - 2)
expect fun Long.toXString(): String
data class SimpleTimeable(override val time: Long) : Timeable {
override fun toString(): String {
return "${time.isoTime()} - $time"
}
}
interface CurrentDataCollection : DataCollection {
val isCurrent: Boolean
}
interface DataCollection : Comparable {
val start: Long
val end: Long
override fun compareTo(other: DataCollection): Int = start.compareTo(other.start)
fun stopTime() = end - 1L
}
class DefaultListable(
override val clazz: KClass,
override val list: List = emptyList(),
val asName: T.() -> String
) : Listable {
override fun toName(t: T): String {
return t.asName()
}
}
interface Listable {
val list: List
fun toName(t: T): String
val clazz: KClass
fun find(name: String): T? {
return list.find { toName(it) == name }
}
fun listable(vararg elements: T): Listable = object : Listable by this {
override val list: List = elements.toList()
}
companion object {
val any = object : Listable {
override val list: List = emptyList()
override fun toName(t: Any): String {
return t.toString()
}
override val clazz: KClass = Any::class
}
}
}
interface StringListable : Listable {
override val clazz: KClass
get() = String::class
override val list: List
get() = emptyList()
override fun find(name: String): String? {
return name
}
override fun toName(t: String): String {
return t
}
}
/**
*
*/
interface Timeable : Comparable {
val time: Long
override fun compareTo(other: Timeable) = time.compareTo(other.time)
fun toTimeableClass() = if (this is SimpleTimeable) this else SimpleTimeable(time)
fun toXString(add: String = ""): String {
return "${time.toXString()}: $add"
}
fun toXLongString(add: String = ""): String {
return "${time.isoTime()}: $add"
}
}
/**
* returns the specified index inclusive [time]
* if list is empty, 0 is returned
*/
fun List.binarySearchByStart(time: Long): Int = binarySearchByStart(time) { it.time }
/**
* returns the specified index inclusive [time]
* if list is empty, 0 is returned
*/
fun List.binarySearchByEnd(time: Long): Int = binarySearchByEnd(time) { it.time }
fun List.subList(from: Long, to: Long): List {
return subList(binarySearchByStart(from) { it.time }, binarySearchByEnd(to) { it.time })
}
fun timeableListOf(vararg elements: Int) = elements.map { SimpleTimeable(it.toLong()) }
interface Valueable {
val value: Double
operator fun minus(other: Valueable) = value - other.value
operator fun minus(other: Double) = value - other
operator fun plus(other: Valueable) = value + other.value
operator fun plus(other: Double) = value + other
operator fun times(other: Valueable) = value * other.value
operator fun times(other: Double) = value * other
fun compareValueTo(other: Valueable) = this.value.compareTo(other.value)
companion object {
val comparator: Comparator = object : Comparator {
override fun compare(a: Valueable, b: Valueable): Int {
return a.compareValueTo(b)
}
}
}
}
interface ValueTimeable : Valueable, Timeable
interface Nameable {
val name: String
}
interface StartTime {
fun firstTime(): Long?
}
interface LastTime {
fun lastTime(): Long?
}
interface StartEndTime : StartTime, LastTime
interface FromToAccessor {
fun read(from: Long, to: Long, max: Int = Int.MAX_VALUE): List
}
interface StartTimeFromToAccessor : FromToAccessor, StartTime
interface LastBeforeReader {
fun readLastBefore(numberToRead: Int, time: Long): List
fun readNextAfter(numberToRead: Int, time: Long): List
}
interface ReadTimeable {
fun read(fileData: D): List
}
interface FileReader {
fun read(fileData: FileRefable): List
}
interface DataAccessor : LastTime, FromToAccessor, StartTimeFromToAccessor, StartEndTime
interface LastReadDataAccessor : DataAccessor, LastBeforeReader
interface FileRefFactory {
fun createFile(fileName: String): FileRef
fun createFile(dir: FileRef, fileName: String): FileRef
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy