Created
August 18, 2013 04:33
-
-
Save mdedetrich/6259909 to your computer and use it in GitHub Desktop.
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
package com.monetise.grater.variables | |
import com.github.nscala_time.time.Imports._ | |
import com.monetise.grater.variables.Value._ | |
import com.monetise.grater.accounttypes.AccountTypeData | |
import scala.collection.mutable.ListBuffer | |
import scala.language.implicitConversions | |
sealed abstract class Variable | |
case object SavingsAmount extends Variable | |
case object SavingsTerm extends Variable | |
case object ProductType extends Variable | |
case object AccountType extends Variable | |
case object AccountPurpose extends Variable | |
case object Geography extends Variable | |
object Value{ | |
sealed abstract class SingleValue | |
case class IntegerT(int:Int) extends SingleValue{} | |
case class DecimalT(float:Float) extends SingleValue{} | |
case class StringT(s:String) extends SingleValue{} | |
case class YearRangeT(p:Period) extends SingleValue{} | |
case class Location(g:com.monetise.grater.geography.Australia) extends SingleValue{} | |
case class AccountTypeT(at:AccountTypeData) extends SingleValue{} | |
case class Price(float:Float) extends SingleValue {} | |
} | |
sealed abstract class Value {} | |
case class ValueS(v:SingleValue) extends Value | |
case class ValueList(values:List[SingleValue]) extends Value {} | |
sealed abstract class SerializedValue | |
case class StringV(s:String) extends SerializedValue | |
case class ListV(l:List[StringV]) extends SerializedValue | |
class ValueSerializer(v:Value) { | |
def serialize(implicit defaultSingle:(SingleValue=>StringV)):SerializedValue = { | |
v match { | |
case ValueS(sv) => defaultSingle(sv) | |
case ValueList(vl) => { | |
val l= new ListBuffer[StringV]() | |
for (v <- vl) { | |
l.append(StringV(defaultSingle(v).s)) | |
} | |
ListV(l.toList) | |
} | |
} | |
} | |
} | |
object ValueImplicits { | |
implicit def Integer2IntT(i:Int) = ValueS(IntegerT(i)) | |
implicit def Float2DecimalT(f:Float) = ValueS(DecimalT(f)) | |
implicit def Double2DecimalT(d:Double) = ValueS(DecimalT(d.asInstanceOf[Float])) | |
implicit def String2StringT(s:String):SingleValue = StringT(s) | |
implicit def rawr(s:String):Value = ValueS(String2StringT(s)) | |
// implicit def String2StringT(s:String) = ValueS(StringT(s)) | |
implicit def YearRangeT2YearRange(p:Period) = ValueS(YearRangeT(p)) | |
implicit def String2SerializedValue(s:String) = StringV(s) | |
implicit def Location2Geography(l:Location) = ValueS(l) | |
implicit def LocationOuter(g:com.monetise.grater.geography.Australia) = Location2Geography(Location(g)) | |
implicit def AccountType2AccountTypeT(a:AccountTypeT) = ValueS(a) | |
implicit def AccountTypeOuter(a:AccountTypeData) = AccountType2AccountTypeT(AccountTypeT(a)) | |
implicit def PriceOuter(p:Price) = ValueS(p) | |
implicit def ListL[A >: SingleValue] (l:List[A]):Value = ValueList(l.map(a => { | |
// implicit def rawr(s:java.lang.String):SingleValue = StringT(s) | |
// a.asInstanceOf[SingleValue] | |
var b:SingleValue = a match { | |
case c:String => String2StringT(c) | |
// Other types go here | |
} | |
b | |
})) | |
// implicit def StringL(l:List[String]) = ValueList(l.map(String2StringT(_).v)) | |
// implicit def IntegerL(l:List[Int]) = ValueList(l.map(Integer2IntT(_).v)) | |
// implicit def FloatL(l:List[Float]) = ValueList(l.map(Float2DecimalT(_).v)) | |
// implicit def DoubleL(l:List[Double]) = ValueList(l.map(Double2DecimalT(_).v)) | |
// implicit def YearL(l:List[Period]) = ValueList(l.map(YearRangeT2YearRange(_).v)) | |
// implicit def AccountL(l:List[AccountTypeData]) = ValueList(l.map(AccountTypeOuter(_).v)) | |
} | |
object Variable { | |
type InputValue = Map[Variable,Value] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment