From 4686111cb5fa5c550ef0c6c390c544430ccccec5 Mon Sep 17 00:00:00 2001 From: "Marius B. Kotsbak" Date: Tue, 7 May 2019 14:10:58 +0200 Subject: [PATCH] #314: add missing operations with Price, Quantity and numbers --- shared/src/main/scala/squants/Quantity.scala | 2 ++ shared/src/main/scala/squants/package.scala | 4 +++ .../test/scala/squants/market/PriceSpec.scala | 31 ++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/shared/src/main/scala/squants/Quantity.scala b/shared/src/main/scala/squants/Quantity.scala index 86439d59..2b34ee20 100644 --- a/shared/src/main/scala/squants/Quantity.scala +++ b/shared/src/main/scala/squants/Quantity.scala @@ -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 diff --git a/shared/src/main/scala/squants/package.scala b/shared/src/main/scala/squants/package.scala index 87b7d9b7..6c4ff3c3 100644 --- a/shared/src/main/scala/squants/package.scala +++ b/shared/src/main/scala/squants/package.scala @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/shared/src/test/scala/squants/market/PriceSpec.scala b/shared/src/test/scala/squants/market/PriceSpec.scala index a20bd200..ff03eed3 100644 --- a/shared/src/test/scala/squants/market/PriceSpec.scala +++ b/shared/src/test/scala/squants/market/PriceSpec.scala @@ -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)) @@ -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))