From a685a953fa327b0650fc7e6998e4a6588b0baf9f Mon Sep 17 00:00:00 2001 From: Peva Blanchard Date: Sun, 23 Jul 2023 11:27:09 +0200 Subject: [PATCH] remove redundant class --- .../ui/toolwindow/DisplayedNumber.kt | 54 ------------------- .../toolwindow/FloatingPointRepresentation.kt | 39 ++++++++++++++ .../ui/toolwindow/InventoryTableModel.kt | 5 +- .../ui/toolwindow/DisplayedNumberTest.kt | 2 +- 4 files changed, 42 insertions(+), 58 deletions(-) delete mode 100644 src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumber.kt diff --git a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumber.kt b/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumber.kt deleted file mode 100644 index 686023beb..000000000 --- a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumber.kt +++ /dev/null @@ -1,54 +0,0 @@ -package ch.kleis.lcaplugin.ui.toolwindow - -import arrow.core.flatten -import arrow.core.replicate -import kotlin.math.* - -class DisplayedNumber( - value: Double, - nbSignificantDigits: Int = 3 -) { - private val repr = FloatingPointRepresentation.of(value, nbSignificantDigits) - - override fun toString(): String { - val isPositive = repr.isPositive - val digits = repr.digits - val nbSignificantDigits = repr.nbSignificantDigits - return when (val positionalExponent = repr.positionalExponent) { - in -2..-1 -> { - val hd = listOf(0).replicate(-positionalExponent - 1).flatten() - .joinToString("") - .let { "0.$it" } - val tl = digits.take(nbSignificantDigits - positionalExponent + 1) - .dropLastWhile { it == 0 } - .joinToString("") - val sign = if (isPositive) "" else "-" - "$sign$hd$tl" - } - in 0..2 -> { - val midpoint = min(positionalExponent + 1, nbSignificantDigits) - val hd = digits.subList(0, midpoint) - .joinToString("") - val tl = digits.subList(midpoint, nbSignificantDigits) - .dropLastWhile { it == 0 } - .joinToString("") - .let { if (it.isEmpty()) "" else ".$it" } - val sign = if (isPositive) "" else "-" - "$sign$hd$tl" - } - else -> { - val hd = digits.subList(0, 1) - .joinToString("") - val tl = digits.subList(1, nbSignificantDigits) - .dropLastWhile { it == 0 } - .joinToString("") - .let { if (it.isEmpty()) "" else ".$it" } - val sign = if (isPositive) "" else "-" - val e = "E$positionalExponent" - "$sign$hd$tl$e" - } - } - } -} - - diff --git a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/FloatingPointRepresentation.kt b/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/FloatingPointRepresentation.kt index c199ff95f..2016f9730 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/FloatingPointRepresentation.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/FloatingPointRepresentation.kt @@ -58,4 +58,43 @@ data class FloatingPointRepresentation( return FloatingPointRepresentation(isPositive, digits, positionalExponent, nbSignificantDigits) } } + + override fun toString(): String { + return when (positionalExponent) { + in -2..-1 -> { + val hd = listOf(0).replicate(-positionalExponent - 1).flatten() + .joinToString("") + .let { "0.$it" } + val tl = digits.take(nbSignificantDigits - positionalExponent + 1) + .dropLastWhile { it == 0 } + .joinToString("") + val sign = if (isPositive) "" else "-" + "$sign$hd$tl" + } + + in 0..2 -> { + val midpoint = min(positionalExponent + 1, nbSignificantDigits) + val hd = digits.subList(0, midpoint) + .joinToString("") + val tl = digits.subList(midpoint, nbSignificantDigits) + .dropLastWhile { it == 0 } + .joinToString("") + .let { if (it.isEmpty()) "" else ".$it" } + val sign = if (isPositive) "" else "-" + "$sign$hd$tl" + } + + else -> { + val hd = digits.subList(0, 1) + .joinToString("") + val tl = digits.subList(1, nbSignificantDigits) + .dropLastWhile { it == 0 } + .joinToString("") + .let { if (it.isEmpty()) "" else ".$it" } + val sign = if (isPositive) "" else "-" + val e = "E$positionalExponent" + "$sign$hd$tl$e" + } + } + } } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/InventoryTableModel.kt b/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/InventoryTableModel.kt index 8417742fb..28ffcfe75 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/InventoryTableModel.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/InventoryTableModel.kt @@ -2,7 +2,6 @@ package ch.kleis.lcaplugin.ui.toolwindow import ch.kleis.lcaplugin.core.assessment.Inventory import ch.kleis.lcaplugin.core.lang.value.MatrixColumnIndex -import ch.kleis.lcaplugin.core.matrix.ImpactFactorMatrix import javax.swing.event.TableModelListener import javax.swing.table.TableModel @@ -59,7 +58,7 @@ class InventoryTableModel( val quantity = inventory.supply.quantityOf(outputProduct) if (columnIndex == 1) { - return DisplayedNumber(quantity.amount).toString() + return FloatingPointRepresentation.of(quantity.amount).toString() } if (columnIndex == 2) { return "${quantity.unit.symbol}" @@ -67,7 +66,7 @@ class InventoryTableModel( val inputProduct = sortedControllablePorts[columnIndex - 3] val ratio = inventory.impactFactors.valueRatio(outputProduct, inputProduct).amount - return DisplayedNumber(quantity.amount * ratio).toString() + return FloatingPointRepresentation.of(quantity.amount * ratio).toString() } override fun setValueAt(aValue: Any?, rowIndex: Int, columnIndex: Int) { diff --git a/src/test/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumberTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumberTest.kt index f72e82d65..30fed0e48 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumberTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DisplayedNumberTest.kt @@ -60,7 +60,7 @@ class DisplayedNumberTest( @Test fun run() { // when - val actual = DisplayedNumber(value).toString() + val actual = FloatingPointRepresentation.of(value).toString() // then assertEquals(expected, actual)