Skip to content

Instantly share code, notes, and snippets.

@michaelkebe
Created May 15, 2011 19:50
Show Gist options
  • Save michaelkebe/973474 to your computer and use it in GitHub Desktop.
Save michaelkebe/973474 to your computer and use it in GitHub Desktop.
Scala Client for SQLSpaces
import info.collide.sqlspaces.client.TupleSpace
import info.collide.sqlspaces.commons._
object ScalaDom {
import scala.xml._
import org.w3c.dom.{Document => JDocument, Node => JNode}
import javax.xml.parsers.DocumentBuilderFactory
def dom(n: Node): JDocument = {
val doc = DocumentBuilderFactory
.newInstance
.newDocumentBuilder
.getDOMImplementation
.createDocument(null, null, null)
def build(node: Node, parent: JNode): Unit = {
val jnode: JNode = node match {
case e: Elem => {
val jn = doc.createElement(e.label)
e.attributes foreach { a => jn.setAttribute(a.key, a.value.mkString) }
jn
}
case a: Atom[_] => doc.createTextNode(a.text)
case c: Comment => doc.createComment(c.commentText)
case er: EntityRef => doc.createEntityReference(er.entityName)
case pi: ProcInstr => doc.createProcessingInstruction(pi.target, pi.proctext)
}
parent.appendChild(jnode)
node.child.map { build(_, jnode) }
}
build(n, doc)
doc
}
}
implicit def scalaElem2DomDocument(elem: scala.xml.Elem) = new {
def toDom = ScalaDom.dom(elem)
}
abstract sealed class FieldDataType
case object IntegerFieldDataType extends FieldDataType
case object StringFieldDataType extends FieldDataType
class ScalaTupleSpaceWrapper(ts: TupleSpace) {
private def scalaTuple2tsTuple[T1](t:Tuple1[T1]) = {
val tst = new Tuple
tst add t._1
tst
}
private def scalaTuple2tsTuple[T1, T2](t:Tuple2[T1, T2]) = {
val tst = new Tuple
tst add t._1; tst add t._2
tst
}
private def scalaTuple2tsTuple[T1, T2, T3](t:Tuple3[T1, T2, T3]) = {
val tst = new Tuple
tst add t._1; tst add t._2; tst add t._3
tst
}
private def scalaTuple2tsTuple[T1, T2, T3, T4](t:Tuple4[T1, T2, T3, T4]) = {
val tst = new Tuple
tst add t._1; tst add t._2; tst add t._3; tst add t._4
tst
}
def <<[T](t: Tuple1[T]) = {
ts write scalaTuple2tsTuple(t)
}
def <<[T1, T2](t: Tuple2[T1, T2]) = {
ts write scalaTuple2tsTuple(t)
}
def <<[T1, T2, T3](t: Tuple3[T1, T2, T3]) = {
ts write scalaTuple2tsTuple(t)
}
def <<[T1, T2, T3, T4](t: Tuple4[T1, T2, T3, T4]) = {
ts write scalaTuple2tsTuple(t)
}
def typedTake[T1](t: Tuple, dummy: Int = 0): Option[Tuple1[T1]] = {
val rt = ts take t
if (rt == null) None
else Some(Tuple1(getFieldAs[T1](rt, 0)))
}
def typedTake[T1, T2](t: Tuple)(implicit m: Manifest[T1]): Option[Tuple2[T1, T2]] = {
val rt = ts take t
if (rt == null) None
else Some((getFieldAs[T1](rt, 0), getFieldAs[T2](rt, 1)))
}
def typedTake[T1, T2, T3](t: Tuple): Option[Tuple3[T1, T2, T3]] = {
val rt = ts take t
if (rt == null) None
else Some((getFieldAs[T1](rt, 0), getFieldAs[T2](rt, 1), getFieldAs[T3](rt, 2)))
}
private def getFieldAs[T](t: Tuple, i: Int) = t.getField(i).getValue.asInstanceOf[T]
}
implicit def tupleSpace2ScalaTupleSpaceWrapper(ts: TupleSpace) = new ScalaTupleSpaceWrapper(ts)
def checkTupleStringInt(t: Tuple2[String, Int]) = ()
val ts = new TupleSpace
ts << Tuple1("A")
ts << ("B", 2)
ts << ("C", 3, 3.14)
ts << ("D", 4, 9.81, true)
ts << ("Long", 123456789012345L)
ts << ("Float", 1.41F)
ts << ("Byte", 16.toByte)
//ts << ("Char", 32.toChar)
ts << ("XML", <foo bar="baz">:D<!--comment--></foo>.toDom)
val tt = new Tuple(Field.createWildCardField)
val tuples = ts takeAll tt
assert(tuples.length == 8)
ts << Tuple1(3.14)
val tt1 = new Tuple(classOf[Double])
val t1 = ts.typedTake[Double](tt1)
assert(t1.isDefined)
assert(t1.get._1 == 3.14)
ts << ("T", 23)
val tt2 = new Tuple("T", classOf[Int])
val t2 = ts.typedTake[String, Int](tt2)
assert(t2.isDefined)
assert(t2.get._1 == "T" && t2.get._2 == 23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment