Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Currency and Digital Storage Base Dimensions #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "info.kunalsheth.units:plugin:5.3.3"
classpath "info.kunalsheth.units:plugin:5.4.0"
}
}

Expand Down
4,781 changes: 3,944 additions & 837 deletions demo/build/uom/UnitsOfMeasure.kt

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions demo/jvm/src/main/kotlin/info/kunalsheth/units/sample/Sample.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.kunalsheth.units.sample

import info.kunalsheth.units.generated.*
import info.kunalsheth.units.generated.Byte
import info.kunalsheth.units.math.*


Expand All @@ -27,6 +28,26 @@ fun main(args: Array<String>) {
assert(420.Degree % 1.Turn in 60.Degree `±` 1.Degree)


val gasPrice = 3.96.`$` / 1.Gallon
val carEfficiency = 16.Mile / 1.Gallon
val trip = 30.6.Mile
val gasMoney = gasPrice / carEfficiency * trip

assert(gasMoney in 7.57.`$` `±` 1.`¢`)
assert(gasMoney in 6.11.`£` `±` 1.`¢`)


val hardDrive = 500.giga(Byte)
val price = 54.99.`$`
val download = 15.3.mega(Bit) / Second

val restoreTime = hardDrive / download
val dailyExpense = price / hardDrive * download * Day

assert(restoreTime in 72.Hour..73.Hour)
assert(dailyExpense in 18.`$`..19.`$`)


val speed = 65.Mile / Hour
val time = 27.Minute
val distance = speed * time
Expand Down
14 changes: 11 additions & 3 deletions demo/units-of-measure.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
generateUnitsOfMeasure {
relationships += relation(dimension(L: 1), dimension(T: 1)) +
relation(dimension(L: 1, T: -1), dimension(L: 1, T: -2)) +
relation(dimension(L:2,M:1,T:-3), d(T:1))
relation(dimension(L: 2, M: 1, T: -3), d(T: 1)) +
r(d(C: 1), d(L: 3), d(L: 1)) +
r(d(C: 1), d(B: 1), d(T: 1))

quantities += [ /* the default values are good enough */]

Expand All @@ -16,9 +18,15 @@ generateUnitsOfMeasure {
u("Turn", Math.PI / 0.5, d()),
u("Foot", 0.3048, d(L: 1)),
u("Percent", 0.01, d()),
u("Minute", 60.0, d(T: 1)),
u("Hour", 3600.0, d(T: 1)),
u("Minute", 60, d(T: 1)),
u("Hour", 3600, d(T: 1)),
u("Day", 86400, d(T: 1)),
u("Mile", 1609.34, d(L: 1)),
u("Gallon", 0.00378541, d(L: 3)),
u("\$", 1, d(C: 1)),
u("¢", 0.01, d(C: 1)),
u("£", 1.24, d(C: 1)),
u("Bit", 1.0 / 8, d(B: 1)),
].toSet()
}
sourceSets.main.kotlin.srcDir generateUnitsOfMeasure.generatedSrcDir
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: "kotlin-platform-jvm"

allprojects {
group "info.kunalsheth.units"
version "5.3.3"
version "5.4.0"
repositories.jcenter()
}

Expand Down
5 changes: 3 additions & 2 deletions plugin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Fri Nov 01 02:20:42 PDT 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ open class GenerateUnitsOfMeasureTask @Inject constructor(p: Project) : DefaultT
I = (params["I"] ?: 0) as Int,
Theta = (params["Theta"] ?: 0) as Int,
N = (params["N"] ?: 0) as Int,
J = (params["J"] ?: 0) as Int
J = (params["J"] ?: 0) as Int,
C = (params["C"] ?: 0) as Int,
B = (params["B"] ?: 0) as Int
) else Dimension()

fun d(params: Map<String, Any>?) = dimension(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ object InternationalSystemOfUnits {
private val Temperature = Dimension(Theta = 1)
private val AmountOfSubstance = Dimension(N = 1)
private val LuminousIntensity = Dimension(J = 1)
private val Currency = Dimension(C = 1)
private val DigitalStorage = Dimension(B = 1)

private val Frequency = Dimension(T = -1)
private val Angle = Dimension(A = 1)
Expand Down Expand Up @@ -164,6 +166,8 @@ object InternationalSystemOfUnits {
Quantity("Temperature", Temperature),
Quantity("AmountOfSubstance", AmountOfSubstance),
Quantity("LuminousIntensity", LuminousIntensity),
Quantity("Currency", Currency),
Quantity("DigitalStorage", DigitalStorage),

Quantity("Frequency", Frequency),
Quantity("Angle", Angle),
Expand Down Expand Up @@ -301,6 +305,7 @@ object InternationalSystemOfUnits {
UnitOfMeasure("Kelvin", 1.0, Temperature),
UnitOfMeasure("Mole", 1.0, AmountOfSubstance),
UnitOfMeasure("Candela", 1.0, LuminousIntensity),
UnitOfMeasure("Byte", 1.0, DigitalStorage),

UnitOfMeasure("Hertz", 1.0, Frequency),
UnitOfMeasure("Radian", 1.0, Angle),
Expand Down
16 changes: 12 additions & 4 deletions plugin/src/main/kotlin/info/kunalsheth/units/data/Dimension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ data class Dimension(
val I: Int = 0,
val Theta: Int = 0,
val N: Int = 0,
val J: Int = 0
val J: Int = 0,
val C: Int = 0,
val B: Int = 0
) : Serializable {
val safeName by lazy {
val (numerator, denominator) = mapOf(
Expand All @@ -40,7 +42,9 @@ data class Dimension(
"I" to I,
"Theta" to Theta,
"N" to N,
"J" to J
"J" to J,
"C" to C,
"B" to B
)
.toList()
.partition { (_, power) -> power >= 0 }
Expand All @@ -65,7 +69,9 @@ data class Dimension(
"I" to I,
"Θ" to Theta,
"N" to N,
"J" to J
"J" to J,
"¤" to C, // https://en.wikipedia.org/wiki/Currency_sign_(typography)
"B" to B // https://simple.wikipedia.org/wiki/Byte
).factorizedString
.takeUnless(String::isBlank)
?: "Dimensionless"
Expand All @@ -81,7 +87,9 @@ data class Dimension(
"A" to I,
"K" to Theta,
"mol" to N,
"cd" to J
"cd" to J,
"¤" to C, // https://en.wikipedia.org/wiki/Currency_sign_(typography)
"B" to B // https://simple.wikipedia.org/wiki/Byte
).factorizedString
}

Expand Down
8 changes: 6 additions & 2 deletions plugin/src/main/kotlin/info/kunalsheth/units/data/Relation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ enum class RelationType : (Dimension, Dimension) -> Dimension {
I = a.I - b.I,
Theta = a.Theta - b.Theta,
N = a.N - b.N,
J = a.J - b.J
J = a.J - b.J,
C = a.C - b.C,
B = a.B - b.B
)
},
Multiply {
Expand All @@ -80,7 +82,9 @@ enum class RelationType : (Dimension, Dimension) -> Dimension {
I = a.I + b.I,
Theta = a.Theta + b.Theta,
N = a.N + b.N,
J = a.J + b.J
J = a.J + b.J,
C = a.C + b.C,
B = a.B + b.B
)
}
}