Removed all classes and methods from multiplatform-utils that aren't called by fints4k

This commit is contained in:
dankito 2022-02-12 13:33:50 +01:00
parent d56833e1ef
commit fe69174db9
22 changed files with 1 additions and 637 deletions

View File

@ -1,7 +1,5 @@
package net.dankito.banking.fints.model package net.dankito.banking.fints.model
import net.dankito.utils.multiplatform.BigDecimal
open class Amount( open class Amount(
val string: String val string: String
@ -15,10 +13,6 @@ open class Amount(
internal constructor() : this("") // for object deserializers internal constructor() : this("") // for object deserializers
open val bigDecimal: BigDecimal
get() = BigDecimal(this.string.replace(',', '.'))
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other !is Amount) return false if (other !is Amount) return false

View File

@ -1,7 +1,5 @@
package net.dankito.banking.fints.model package net.dankito.banking.fints.model
import net.dankito.utils.multiplatform.BigDecimal
open class Money( open class Money(
val amount: Amount, val amount: Amount,
@ -15,9 +13,6 @@ open class Money(
open val bigDecimal: BigDecimal
get() = amount.bigDecimal
open val displayString: String open val displayString: String
get() = "$amount $currency" get() = "$amount $currency"

View File

@ -1,26 +0,0 @@
package net.dankito.utils.multiplatform
expect fun Collection<BigDecimal>.sum(): BigDecimal
expect class BigDecimal {
companion object {
val Zero: BigDecimal
}
constructor(decimal: String)
constructor(double: Double)
val isPositive: Boolean
fun negated(): BigDecimal
fun format(countDecimalPlaces: Int): String
}

View File

@ -2,7 +2,7 @@ package net.dankito.utils.multiplatform
val Char.isLowerCase: Boolean val Char.isLowerCase: Boolean
get() = toLowerCase() == this get() = lowercaseChar() == this
val Char.isUpperCase: Boolean val Char.isUpperCase: Boolean
get() = isLowerCase == false get() = isLowerCase == false

View File

@ -1,23 +0,0 @@
package net.dankito.utils.multiplatform
expect class File(path: String) {
constructor(folder: File, filename: String)
// have to specify it as method as property would conflict with java.io.File's getAbsolutePath
fun getAbsolutePath(): String
val filename: String
val fileExtension: String
val parent: File?
fun mkdirs(): Boolean
fun delete(): Boolean
}

View File

@ -1,14 +0,0 @@
package net.dankito.utils.multiplatform
expect class Freezer() {
companion object {
fun <T> freeze(obj: T): T
fun isFrozen(obj: Any): Boolean
}
}

View File

@ -1,12 +0,0 @@
package net.dankito.utils.multiplatform
open class ObjectHolder<T>(
var value: T
) {
override fun toString(): String {
return value?.toString() ?: "Value is null"
}
}

View File

@ -1,174 +0,0 @@
package net.dankito.utils.multiplatform
import net.dankito.utils.multiplatform.log.Logger
import net.dankito.utils.multiplatform.log.LoggerFactory
open class Stopwatch(createStarted: Boolean = true) {
companion object {
private val log = LoggerFactory.getLogger(Stopwatch::class)
fun measureDuration(task: () -> Unit): Long {
return measureDuration(Stopwatch(), task)
}
private fun measureDuration(stopwatch: Stopwatch, task: () -> Unit): Long {
task()
return stopwatch.stop()
}
fun <T> logDuration(loggedAction: String, task: () -> T): T {
return logDuration(loggedAction, log, task)
}
fun <T> logDuration(loggedAction: String, logger: Logger, task: () -> T): T {
val stopwatch = Stopwatch()
val result = task()
stopwatch.stopAndLog(loggedAction, logger)
return result
}
suspend fun measureDurationSuspendable(task: suspend () -> Unit): Long {
val stopwatch = Stopwatch()
task()
return stopwatch.stop()
}
suspend fun <T> logDurationSuspendable(loggedAction: String, task: suspend () -> T): T {
return logDurationSuspendable(loggedAction, log, task)
}
suspend fun <T> logDurationSuspendable(loggedAction: String, logger: Logger, task: suspend () -> T): T {
val stopwatch = Stopwatch()
val result = task()
stopwatch.stopAndLog(loggedAction, logger)
return result
}
}
constructor() : this(true)
var isRunning = false
protected set
var startedAt = 0L
protected set
var elapsedNanos = 0L
protected set
init {
if (createStarted) {
start()
}
}
open fun getCurrentTimeNanoSeconds(): Long {
return Date.nanoSecondsSinceEpoch
}
open fun start() {
if (isRunning == false) {
startedAt = getCurrentTimeNanoSeconds()
isRunning = true
}
}
open fun stop(): Long {
if (isRunning) {
val stoppedAt = getCurrentTimeNanoSeconds()
isRunning = false
elapsedNanos = stoppedAt - startedAt
}
return elapsedNanos
}
open fun stopAndPrint(): String {
stop()
return formatElapsedTime()
}
open fun stopAndLog(loggedAction: String): Long {
return stopAndLog(loggedAction, log)
}
open fun stopAndLog(loggedAction: String, logger: Logger): Long {
stop()
logElapsedTime(loggedAction, logger)
return elapsedNanos
}
// open fun elapsed(desiredUnit: TimeUnit): Long {
// return desiredUnit.convert(elapsedNanos, TimeUnit.NANOSECONDS)
// }
open fun formatElapsedTime(): String {
return formatElapsedTime(elapsedNanos)
}
protected open fun formatElapsedTime(elapsedNanos: Long): String {
val durationMicroseconds = elapsedNanos / 1000
val durationMillis = durationMicroseconds / 1000
val millis = durationMillis % 1000
val durationSeconds = durationMillis / 1000
val seconds = durationSeconds % 60
val minutes = durationSeconds / 60
return if (minutes > 0) {
StringHelper.format("%02d:%02d.%03d min", minutes, seconds, millis)
}
else if (seconds > 0) {
StringHelper.format("%02d.%03d s", seconds, millis)
}
else {
StringHelper.format("%02d.%03d ms", millis, (durationMicroseconds % 1000))
}
}
open fun logElapsedTime(loggedAction: String) {
logElapsedTime(loggedAction, log)
}
open fun logElapsedTime(loggedAction: String, logger: Logger) {
val formattedElapsedTime = formatElapsedTime()
logger.info("$loggedAction took $formattedElapsedTime")
}
override fun toString(): String {
if (isRunning) {
val elapsedNanos = getCurrentTimeNanoSeconds() - startedAt
return "Running, ${formatElapsedTime(elapsedNanos)} elapsed"
}
return "Stopped, ${formatElapsedTime()} elapsed"
}
}

View File

@ -1,16 +0,0 @@
package net.dankito.utils.multiplatform.os
open class DeviceInfo(
open val manufacturer: String,
open val deviceModel: String,
open val osName: String,
open val osVersion: String,
open val osArch: String
) {
override fun toString(): String {
return "$manufacturer $deviceModel: $osName $osVersion"
}
}

View File

@ -1,8 +0,0 @@
package net.dankito.utils.multiplatform.os
expect class DeviceInfoRetriever actual constructor() {
fun getDeviceInfo(): DeviceInfo
}

View File

@ -1,19 +0,0 @@
package net.dankito.utils.multiplatform.os
open class Os(open val osHelper: OsHelper = OsHelper()) {
open val osType: OsType
get() = osHelper.osType
open val isRunningOnJvm: Boolean
get() = osType == OsType.JVM
open val isRunningOnAndroid: Boolean
get() = osType == OsType.Android
open val isRunningOniOS: Boolean
get() = osType == OsType.iOS
}

View File

@ -1,67 +0,0 @@
package net.dankito.utils.multiplatform
import platform.Foundation.*
fun NSDecimalNumber.toBigDecimal(): BigDecimal {
return BigDecimal(this.stringValue) // TODO: find a better way than double string conversion
}
actual fun Collection<BigDecimal>.sum(): BigDecimal {
return this.fold(NSDecimalNumber.zero) { acc, e -> acc.decimalNumberByAdding(e.decimal) }.toBigDecimal()
}
actual class BigDecimal(val decimal: NSDecimalNumber) : Comparable<BigDecimal> { // it's almost impossible to derive from NSDecimalNumber so i keep it as property
actual companion object {
actual val Zero = BigDecimal(0.0)
}
actual constructor(double: Double) : this(NSDecimalNumber(double))
actual constructor(decimal: String) : this(decimal.toDouble())
actual val isPositive: Boolean
get() = this >= Zero
actual fun negated(): BigDecimal {
val negated = decimal.decimalNumberByMultiplyingBy(NSDecimalNumber(1.toULong(), 0, true))
return BigDecimal(negated)
}
actual fun format(countDecimalPlaces: Int): String {
val formatter = NSNumberFormatter()
formatter.minimumFractionDigits = countDecimalPlaces.toULong()
formatter.maximumFractionDigits = countDecimalPlaces.toULong()
return formatter.stringFromNumber(this.decimal) ?: ""
}
override fun compareTo(other: BigDecimal): Int {
return decimal.compare(other.decimal).toCompareToResult()
}
override fun equals(other: Any?): Boolean {
if (other is BigDecimal) {
return this.compareTo(other) == 0
}
return super.equals(other)
}
override fun hashCode(): Int {
return decimal.hashCode()
}
override fun toString(): String {
return decimal.description ?: decimal.descriptionWithLocale(NSLocale.currentLocale)
}
}

View File

@ -1,39 +0,0 @@
package net.dankito.utils.multiplatform
import platform.Foundation.*
actual class File actual constructor(path: String) : NSURL(fileURLWithPath = path) {
actual constructor(folder: File, filename: String)
: this(folder.URLByAppendingPathComponent(filename)?.path ?: "")
actual fun getAbsolutePath(): String {
return absoluteString ?: absoluteURL?.absoluteString ?: path ?: ""
}
actual val filename: String
get() = lastPathComponent ?: ""
actual val fileExtension: String
get() = this.pathExtension ?: filename.substringAfterLast('.', "")
actual val parent: File?
get() = this.URLByDeletingLastPathComponent?.absoluteString?.let { File(it) }
actual fun mkdirs(): Boolean {
return NSFileManager.defaultManager.createDirectoryAtURL(this, true, null, null)
}
actual fun delete(): Boolean {
return NSFileManager.defaultManager.removeItemAtURL(this, null)
}
override fun description(): String? {
return getAbsolutePath()
}
}

View File

@ -1,21 +0,0 @@
package net.dankito.utils.multiplatform
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
actual class Freezer {
actual companion object {
actual fun <T> freeze(obj: T): T {
return obj.freeze()
}
actual fun isFrozen(obj: Any): Boolean {
return obj.isFrozen
}
}
}

View File

@ -1,11 +0,0 @@
package net.dankito.utils.multiplatform.os
actual class DeviceInfoRetriever {
actual fun getDeviceInfo(): DeviceInfo {
// TODO:
return DeviceInfo("", "", "", "", "")
}
}

View File

@ -1,65 +0,0 @@
package net.dankito.utils.multiplatform
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import net.dankito.utils.multiplatform.serialization.BigDecimalDeserializer
fun java.math.BigDecimal.toBigDecimal(): BigDecimal {
return BigDecimal(this.toPlainString()) // TODO: find a better way than double string conversion
}
actual fun Collection<BigDecimal>.sum(): BigDecimal {
return this.fold(java.math.BigDecimal.ZERO) { acc, e -> (acc + e) }.toBigDecimal()
}
@JsonDeserialize(using = BigDecimalDeserializer::class)
actual class BigDecimal actual constructor(decimal: String) : java.math.BigDecimal(decimal) {
actual companion object {
actual val Zero = BigDecimal(0.0)
}
internal constructor() : this("0") // for object deserializers
actual constructor(double: Double) : this(java.math.BigDecimal.valueOf(double).toPlainString()) // for object deserializers
actual val isPositive: Boolean
get() = this >= ZERO
actual fun negated(): BigDecimal {
return BigDecimal(super.negate().toString())
}
actual fun format(countDecimalPlaces: Int): String {
return String.format("%.0${countDecimalPlaces}f", this)
}
override fun equals(other: Any?): Boolean {
if (other is BigDecimal) {
return this.compareTo(other) == 0
}
return super.equals(other)
}
/* TODO: where are these methods coming from? */
override fun toByte(): Byte {
return 0 // will never be called; where is this method coming from?
}
override fun toChar(): Char {
return 0.toChar() // will never be called; where is this method coming from?
}
override fun toShort(): Short {
return 0 // will never be called; where is this method coming from?
}
}

View File

@ -1,10 +0,0 @@
package net.dankito.utils.multiplatform
fun String?.toFile(): java.io.File? {
return this?.let { File(it) }
}
fun java.util.Date.toDate(): Date {
return Date(this.time)
}

View File

@ -1,49 +0,0 @@
package net.dankito.utils.multiplatform
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import net.dankito.utils.multiplatform.serialization.FileDeserializer
fun java.io.File.toFile(): File {
return File(this.absolutePath)
}
@JsonDeserialize(using = FileDeserializer::class)
actual class File actual constructor(path: String) : java.io.File(path) {
actual constructor(folder: File, filename: String)
: this(java.io.File(folder, filename).absolutePath)
internal constructor() : this("") // for object deserializers
actual override fun getAbsolutePath(): String {
return super.getAbsolutePath()
}
actual val filename: String
get() = super.getName()
actual val fileExtension: String
get() = this.extension
actual val parent: File?
get() = this.parentFile?.absolutePath?.let { File(it) }
actual override fun mkdirs(): Boolean {
return super.mkdirs()
}
actual override fun delete(): Boolean {
return super.delete()
}
override fun toString(): String {
return absolutePath
}
}

View File

@ -1,18 +0,0 @@
package net.dankito.utils.multiplatform
actual class Freezer {
actual companion object {
actual fun <T> freeze(obj: T): T {
return obj // no op
}
actual fun isFrozen(obj: Any): Boolean {
return false // freezing is only possible on Kotlin/Native
}
}
}

View File

@ -1,11 +0,0 @@
package net.dankito.utils.multiplatform.os
actual class DeviceInfoRetriever {
actual fun getDeviceInfo(): DeviceInfo {
// TODO: retrieve manufacturer and device model
return DeviceInfo("", "", System.getProperty("os.name", ""), System.getProperty("os.version", ""), System.getProperty("os.arch", ""))
}
}

View File

@ -1,21 +0,0 @@
package net.dankito.utils.multiplatform.serialization
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import net.dankito.utils.multiplatform.BigDecimal
open class BigDecimalDeserializer : StdDeserializer<BigDecimal>(BigDecimal::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): BigDecimal? {
val doubleString = parser.readValueAs(String::class.java)
if (doubleString.isNullOrBlank() || doubleString == "null") {
return null
}
return BigDecimal(doubleString)
}
}

View File

@ -1,21 +0,0 @@
package net.dankito.utils.multiplatform.serialization
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import net.dankito.utils.multiplatform.File
open class FileDeserializer : StdDeserializer<File>(File::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): File? {
val path = parser.readValueAs(String::class.java)
if (path == null || path == "null") {
return null
}
return File(path)
}
}