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

Stabilized toString output for MoneyContext and MoneyNumeric #218

Merged
merged 1 commit into from
Apr 7, 2017
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ scala> val exchangeRates = List(USD / CAD(1.05), USD / MXN(12.50), USD / JPY(100
exchangeRates: List[squants.market.CurrencyExchangeRate] = List(USD/CAD 1.05, USD/MXN 12.5, USD/JPY 100.0)

scala> implicit val moneyContext = defaultMoneyContext withExchangeRates exchangeRates
moneyContext: squants.market.MoneyContext = MoneyContext(USD,Set(XAG, EUR, CAD, CHF, AUD, BTC, INR, XAU, SEK, DKK, JPY, NOK, GBP, NZD, RUB, CZK, MYR, CNY, ARS, KRW, CLP, HKD, BRL, USD, MXN),List(USD/CAD 1.05, USD/MXN 12.5, USD/JPY 100.0),true)
moneyContext: squants.market.MoneyContext = MoneyContext(DefaultCurrency(USD),Currencies(ARS,AUD,BRL,BTC,CAD,CHF,CLP,CNY,CZK,DKK,EUR,GBP,HKD,INR,JPY,KRW,MXN,MYR,NOK,NZD,RUB,SEK,USD,XAG,XAU),ExchangeRates(USD/CAD 1.05,USD/JPY 100.0,USD/MXN 12.5),AllowIndirectConversions(true))

scala> val energyPrice = USD(102.20) / MegawattHours(1)
energyPrice: squants.market.Price[squants.energy.Energy] = 102.2 USD/1.0 MWh
Expand Down Expand Up @@ -856,7 +856,7 @@ implicit val moneyContext = defaultMoneyContext

```scala
scala> implicit val moneyNum = new MoneyNumeric()
moneyNum: squants.market.MoneyConversions.MoneyNumeric = squants.market.MoneyConversions$MoneyNumeric@14bb3847
moneyNum: squants.market.MoneyConversions.MoneyNumeric = MoneyNumeric(MoneyContext(DefaultCurrency(USD),Currencies(ARS,AUD,BRL,BTC,CAD,CHF,CLP,CNY,CZK,DKK,EUR,GBP,HKD,INR,JPY,KRW,MXN,MYR,NOK,NZD,RUB,SEK,USD,XAG,XAU),ExchangeRates(),AllowIndirectConversions(true)))

scala> val sum = List(USD(100), USD(10)).sum
sum: squants.market.Money = 110.0 USD
Expand Down Expand Up @@ -996,7 +996,7 @@ import squants.time.TimeConversions._

```scala
scala> implicit val moneyContext = defaultMoneyContext
moneyContext: squants.market.MoneyContext = MoneyContext(USD,Set(XAG, EUR, CAD, CHF, AUD, BTC, INR, XAU, SEK, DKK, JPY, NOK, GBP, NZD, RUB, CZK, MYR, CNY, ARS, KRW, CLP, HKD, BRL, USD, MXN),List(),true)
moneyContext: squants.market.MoneyContext = MoneyContext(DefaultCurrency(USD),Currencies(ARS,AUD,BRL,BTC,CAD,CHF,CLP,CNY,CZK,DKK,EUR,GBP,HKD,INR,JPY,KRW,MXN,MYR,NOK,NZD,RUB,SEK,USD,XAG,XAU),ExchangeRates(),AllowIndirectConversions(true))

scala> val energyPrice: Price[Energy] = 45.25.money / megawattHour
energyPrice: squants.market.Price[squants.energy.Energy] = 45.25 USD/1.0 MWh
Expand Down
6 changes: 6 additions & 0 deletions shared/src/main/scala/squants/market/Money.scala
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,11 @@ object MoneyConversions {
def toFloat(x: Money) = x.value.toFloat
def toDouble(x: Money) = x.value
def compare(x: Money, y: Money) = if (x.value > y.value) 1 else if (x.value < y.value) -1 else 0

/**
* Custom implementation using SortedSets to ensure consistent output
* @return String representation of this instance
*/
override def toString: String = s"MoneyNumeric($mc)"
}
}
14 changes: 13 additions & 1 deletion shared/src/main/scala/squants/market/MoneyContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package squants.market

import scala.collection.SortedSet

/**
* MoneyContext
*
Expand All @@ -23,7 +25,6 @@ package squants.market
*
* @author garyKeorkunian
* @since 0.1
*
* @param defaultCurrency Currency used when none is supplied to the Money factory
* @param rates Collection of Exchange Rates used for currency conversions
*/
Expand All @@ -33,6 +34,17 @@ case class MoneyContext(
rates: Seq[CurrencyExchangeRate],
allowIndirectConversions: Boolean = true) {

/**
* Custom implementation using SortedSets to ensure consistent output
* @return String representation of this instance
*/
override def toString: String = string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could just map over currencies to get their symbols and make a SortedSet from that. And do the same for rates. That would avoid needing to create Ordering instances for Currency and CurrencyExchangeRate.

Something like:

val cSet = currencies.map { _.code }.sorted.mkString(',')

Copy link
Contributor Author

@garyKeorkunian garyKeorkunian Apr 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, much cleaner. I needed to throw a toSeq call in the pipeline to provide sortability without the Ordering implicit. I'll have a new push soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SortedSet was overkill for the internal implementation, for sure.

private lazy val string = {
val cSet = currencies.map(_.toString).toSeq.sorted.mkString(",")
val rSet = rates.map(_.toString).sorted.mkString(",")
s"MoneyContext(DefaultCurrency(${defaultCurrency.code}),Currencies($cSet),ExchangeRates($rSet),AllowIndirectConversions($allowIndirectConversions))"
}

/**
* Returns an Option on an exchange rate if a direct rate exists, otherwise None
*
Expand Down