~ let access coordinates with the right type
authorAlexandre Bertails <bertails@w3.org>
Thu, 25 Aug 2011 16:57:19 -0400
changeset 100 0b3b2d6779b8
parent 95 98f200362113
child 101 7562c185d2fd
~ let access coordinates with the right type
src/main/scala/Workout.scala
--- a/src/main/scala/Workout.scala	Thu Aug 25 16:23:38 2011 -0400
+++ b/src/main/scala/Workout.scala	Thu Aug 25 16:57:19 2011 -0400
@@ -37,7 +37,7 @@
                    activity:String,
                    date: Long,
                    distance: Long,
-                   events:scala.collection.immutable.Seq[Event]) {
+                   events:Seq[Event]) {
 
   def asJSON:String = {
     val formattedEvents = events map { _.asJSON } mkString ("[ ", ", ", " ]")
@@ -51,17 +51,22 @@
                  @Key("s") status:Option[String]) {
  
   def c:Option[(Double, Double, Option[Double])] = {
-    val dbList = coordinates.asInstanceOf[Option[com.mongodb.BasicDBList]]
-    
-    null
+    coordinates.asInstanceOf[Option[com.mongodb.BasicDBList]] map { dblist => {
+      (dblist:MongoDBList).toList match {
+        case lon :: lat :: alt :: Nil => (lon.toString.toDouble, lat.toString.toDouble, Some(alt.toString.toDouble))
+        case lon :: lat :: Nil => (lon.toString.toDouble, lat.toString.toDouble, None)
+        case _ => sys.error("here")
+      }
+    } }
   }
   
   def asJSON:String = {
     val tString = """"t": "%s"""" format timestamp.toString
     val cString = {
-//      val c = coordinates.asInstanceOf[com.mongodb.BasicDBList]
-      // coordinates map { case (lon, lat, alt) => ", [%f, %f, %f]" format (lon, lat, alt.get) } getOrElse ""
-      coordinates.asInstanceOf[Option[com.mongodb.BasicDBList]] map { c => """, "c": "%s"""" format c.toString } getOrElse ""
+      c map {
+        case (lon, lat, Some(alt)) => ", [%f, %f, %f]" format (lon, lat, alt)
+        case (lon, lat, None) => ", [%f, %f]" format (lon, lat)
+      } getOrElse ""
     }
     val sString = status map { s =>  """, "s": "%s"""" format s.toString } getOrElse ""
     "{ %s%s%s }" format (tString, cString, sString)