Skip to content

Commit

Permalink
Merge pull request #166 from typelevel/stone
Browse files Browse the repository at this point in the history
add stone mass unit
  • Loading branch information
darkfrog26 authored Jan 5, 2017
2 parents b7054b4 + 01bef1c commit 9af3074
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion shared/src/main/scala/squants/mass/Mass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final class Mass private (val value: Double, val unit: MassUnit)
def toPounds = to(Pounds)
def toKilopounds = to(Kilopounds)
def toMegapounds = to(Megapounds)
def toStone = to(Stone)
def toTroyGrains = to(TroyGrains)
def toPennyweights = to(Pennyweights)
def toTroyOunces = to(TroyOunces)
Expand All @@ -65,7 +66,7 @@ object Mass extends Dimension[Mass] with BaseDimension {
def name = "Mass"
def primaryUnit = Grams
def siUnit = Kilograms
def units = Set(Micrograms, Milligrams, Grams, Kilograms, Tonnes, Ounces, Pounds, Kilopounds, Megapounds,
def units = Set(Micrograms, Milligrams, Grams, Kilograms, Tonnes, Ounces, Pounds, Kilopounds, Megapounds, Stone,
TroyGrains, Pennyweights, TroyOunces, TroyPounds, Tolas, Carats)
def dimensionSymbol = "M"
}
Expand Down Expand Up @@ -121,6 +122,11 @@ object Megapounds extends MassUnit {
val symbol = "Mlb"
}

object Stone extends MassUnit {
val conversionFactor = Pounds.conversionFactor * 14d
val symbol = "st"
}

object TroyGrains extends MassUnit {
val conversionFactor = 64.79891 * Milligrams.conversionFactor
val symbol = "gr"
Expand Down Expand Up @@ -166,6 +172,7 @@ object MassConversions {
lazy val pound = Pounds(1)
lazy val kilopound = Kilopounds(1)
lazy val megapound = Megapounds(1)
lazy val stone = Stone(1)
lazy val troyGrain = TroyGrains(1)
lazy val pennyweight = Pennyweights(1)
lazy val troyOunce = TroyOunces(1)
Expand All @@ -186,6 +193,7 @@ object MassConversions {
def pounds = Pounds(n)
def kilopounds = Kilopounds(n)
def megapounds = Megapounds(n)
def stone = Stone(n)
def troyGrains = TroyGrains(n)
def dwt = Pennyweights(n)
def pennyweights = Pennyweights(n)
Expand Down
8 changes: 7 additions & 1 deletion shared/src/test/scala/squants/mass/MassSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class MassSpec extends FlatSpec with Matchers {
Pounds(10.22).toPounds should be(10.22)
Kilopounds(10.22).toKilopounds should be(10.22)
Megapounds(10.22).toMegapounds should be(10.22)
Stone(10.22).toStone should be(10.22)
TroyGrains(10.22).toTroyGrains should be(10.22)
Pennyweights(10.22).toPennyweights should be(10.22)
TroyOunces(10.22).toTroyOunces should be(10.22)
TroyPounds(10.22).toTroyPounds should be(10.22)
Tolas(10.22).toTolas should be(10.22)
Carats(10.22).toCarats should be(10.22)

}

it should "create values from properly formatted Strings" in {
Expand All @@ -54,6 +54,7 @@ class MassSpec extends FlatSpec with Matchers {
Mass("10.22 lb").get should be(Pounds(10.22))
Mass("10.22 klb").get should be(Kilopounds(10.22))
Mass("10.22 Mlb").get should be(Megapounds(10.22))
Mass("10.22 st").get should be(Stone(10.22))
Mass("10.22 gr").get should be(TroyGrains(10.22))
Mass("10.22 dwt").get should be(Pennyweights(10.22))
Mass("10.22 oz t").get should be(TroyOunces(10.22))
Expand Down Expand Up @@ -82,6 +83,7 @@ class MassSpec extends FlatSpec with Matchers {
Grams(1000) should be(Kilograms(1))
Kilograms(0.45359237) should be(Pounds(1))
Ounces(16) should be(Pounds(1))
Pounds(14) should be(Stone(1))

TroyOunces(12) should be(TroyPounds(1))
TroyGrains(180) should be(Tolas(1))
Expand All @@ -100,6 +102,7 @@ class MassSpec extends FlatSpec with Matchers {
Tonnes(1).toString(Tonnes) should be("1.0 t")
Pounds(1).toString(Pounds) should be("1.0 lb")
Ounces(1).toString(Ounces) should be("1.0 oz")
Stone(1).toString(Stone) should be("1.0 st")

TroyGrains(1).toString(TroyGrains) should be("1.0 gr")
Pennyweights(1).toString(Pennyweights) should be("1.0 dwt")
Expand Down Expand Up @@ -153,6 +156,7 @@ class MassSpec extends FlatSpec with Matchers {
tonne should be(Tonnes(1))
pound should be(Pounds(1))
ounce should be(Ounces(1))
stone should be(Stone(1))

troyGrain should be(TroyGrains(1))
pennyweight should be(Pennyweights(1))
Expand All @@ -176,6 +180,7 @@ class MassSpec extends FlatSpec with Matchers {
d.tonnes should be(Tonnes(d))
d.pounds should be(Pounds(d))
d.ounces should be(Ounces(d))
d.stone should be(Stone(d))

d.troyGrains should be(TroyGrains(d))
d.dwt should be(Pennyweights(d))
Expand Down Expand Up @@ -207,6 +212,7 @@ class MassSpec extends FlatSpec with Matchers {
"10.45 t".toMass.get should be(Tonnes(10.45))
"10.45 lb".toMass.get should be(Pounds(10.45))
"10.45 oz".toMass.get should be(Ounces(10.45))
"10.45 st".toMass.get should be(Stone(10.45))
"10.45 gr".toMass.get should be(TroyGrains(10.45))
"10.45 dwt".toMass.get should be(Pennyweights(10.45))
"10.45 oz t".toMass.get should be(TroyOunces(10.45))
Expand Down

0 comments on commit 9af3074

Please sign in to comment.