Skip to content

Commit

Permalink
Merge pull request #343 from mkotsbak/Fix_#314
Browse files Browse the repository at this point in the history
#314: add missing operations with Price, Quantity and numbers
  • Loading branch information
garyKeorkunian authored Sep 5, 2019
2 parents 3aa9816 + 4686111 commit 7d4378e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions shared/src/main/scala/squants/Quantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ abstract class Quantity[A <: Quantity[A]] extends Serializable with Ordered[A] {
def times(that: Double): A = unit(this.value * that)
def *(that: Double): A = times(that)

def *(that: Price[A]): Money = that * this

/**
* Divide this quantity by some number
* @param that Double
Expand Down
4 changes: 4 additions & 0 deletions shared/src/main/scala/squants/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ package object squants {
implicit class SquantifiedDouble(d: Double) {
def *[A <: Quantity[A]](that: A): A = that * d
def *[A](that: SVector[A]): SVector[A] = that * d
def *[A <: Quantity[A]](that: Price[A]): Price[A] = that * d
def /(that: Time): Frequency = Each(d) / that
def per(that: Time): Frequency = /(that)
}
Expand All @@ -100,6 +101,7 @@ package object squants {
implicit class SquantifiedLong(l: Long) {
def *[A <: Quantity[A]](that: A): A = that * l.toDouble
def *[A](that: SVector[A]): SVector[A] = that * l.toDouble
def *[A <: Quantity[A]](that: Price[A]): Price[A] = that * l.toDouble
def /(that: Time) = Each(l) / that
def per(that: Time): Frequency = /(that)
}
Expand All @@ -115,6 +117,7 @@ package object squants {
implicit class SquantifiedInt(l: Int) {
def *[A <: Quantity[A]](that: A): A = that * l.toDouble
def *[A](that: SVector[A]): SVector[A] = that * l.toDouble
def *[A <: Quantity[A]](that: Price[A]): Price[A] = that * l.toDouble
def /(that: Time) = Each(l) / that
def per(that: Time): Frequency = /(that)
}
Expand All @@ -130,6 +133,7 @@ package object squants {
implicit class SquantifiedBigDecimal(bd: BigDecimal) {
def *[A <: Quantity[A]](that: A): A = that * bd.toDouble
def *[A](that: SVector[A]): SVector[A] = that * bd.toDouble
def *[A <: Quantity[A]](that: Price[A]): Price[A] = that * bd.toDouble
def /(that: Time) = Each(bd) / that
def per(that: Time): Frequency = /(that)
}
Expand Down
31 changes: 30 additions & 1 deletion shared/src/test/scala/squants/market/PriceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,42 @@ class PriceSpec extends FlatSpec with Matchers {
p3 should be(p1 - p2)
}

it should "properly multiply by a Double" in {
it should "properly multiply by an Integer" in {
val p1 = Price(Money(9, USD), Meters(1))
val p2 = Price(Money(3, USD), Meters(1))
p1 should be(p2 * 3)
}

it should "properly be multiplied by an Integer" in {
val p1 = Price(Money(9, USD), Meters(1))
val p2 = Price(Money(3, USD), Meters(1))
p1 should be(3 * p2)
}

it should "properly multiply by a Double" in {
val p1 = Price(Money(9.0, USD), Meters(1))
val p2 = Price(Money(3.0, USD), Meters(1))
p1 should be(p2 * 3.0)
}

it should "properly be multiplied by a Double" in {
val p1 = Price(Money(9.0, USD), Meters(1))
val p2 = Price(Money(3.0, USD), Meters(1))
p1 should be(3.0 * p2)
}

it should "properly multiply by a BigDecimal" in {
val p1 = Price(Money(9, USD), Meters(1))
val p2 = Price(Money(3, USD), Meters(1))
p1 should be(p2 * BigDecimal(3))
}

it should "properly multiplied by a BigDecimal" in {
val p1 = Price(Money(9, USD), Meters(1))
val p2 = Price(Money(3, USD), Meters(1))
p1 should be(BigDecimal(3) * p2)
}

it should "properly divide by a Double" in {
val p1 = Price(Money(9, USD), Meters(1))
val p2 = Price(Money(3, USD), Meters(1))
Expand Down Expand Up @@ -81,6 +105,11 @@ class PriceSpec extends FlatSpec with Matchers {
p * Meters(10) should be(Money(100, USD))
}

it should "return Money when Quantity is multiplied by this" in {
val p = Price(Money(10, USD), Meters(1))
Meters(10) * p should be(Money(100, USD))
}

it should "return Quantity when divided by Money" in {
val p = Price(Money(10, USD), Meters(1))
Money(40, USD) / p should be(Meters(4))
Expand Down

0 comments on commit 7d4378e

Please sign in to comment.