Created
May 1, 2012 00:04
-
-
Save michaeldiamant/2563818 to your computer and use it in GitHub Desktop.
Monadic style order resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait MonadicStyleNewOrderResolver extends NewOrderResolutionErrors { | |
val symbolDao: SymbolDao | |
val marketTakerDao: MarketTakerDao | |
val marketMakerDao: MarketMakerDao | |
def resolve(order: NewOrder): Validation[String, ResolvedNewOrder] = | |
for { | |
symbol <- symbolDao.findByName(order.symbolName) | |
.toSuccess(UnknownSymbol) | |
marketTaker <- marketTakerDao.findByExternalId(order.marketTakerId) | |
.toSuccess(UnknownMarketTakerId) | |
account <- marketTaker.accounts.find(_.id == order.accountId) | |
.toSuccess(UnauthorizedAccount) | |
marketMaker <- marketMakerDao.findByExternalId(order.marketMakerId) | |
.toSuccess(UnknownMarketMakerId) | |
_ <- marketTaker.symbols.find(_ == symbol) | |
.toSuccess(UnauthorizedSymbol) | |
_ <- (order.quantity > 0).option(order.quantity) | |
.toSuccess(InvalidQuantity) | |
_ <- (order.price > 0).option(order.price) | |
.toSuccess(InvalidPrice) | |
} yield | |
ResolvedNewOrder( | |
account = account, | |
symbol = symbol, | |
marketMaker = marketMaker, | |
price = order.price, | |
quantity = order.quantity, | |
side = order.side | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment