Skip to content

Instantly share code, notes, and snippets.

@jottinger
Created November 4, 2014 17:05
Show Gist options
  • Save jottinger/4a88120b299e2c2e8566 to your computer and use it in GitHub Desktop.
Save jottinger/4a88120b299e2c2e8566 to your computer and use it in GitHub Desktop.
import java.util.UUID
import scala.collection.mutable.{Map => MutableMap}
case class Terms(id:UUID, name:String) {
val words=MutableMap[UUID, Word]()
}
case class Word(id:UUID, word:String, definition:String)
trait TermService {
def getTerms(id:UUID):Terms
def createTerms(name:String):Terms
}
object DefaultTermService extends TermService {
val terms=MutableMap[UUID, Terms]()
override def getTerms(id: UUID): Terms = {
terms(id)
}
override def createTerms(name: String): Terms = {
val collection=Terms(UUID.randomUUID(), name)
terms += (collection.id -> collection)
collection
}
}
object Ontology {
def main(args:Array[String]) = {
val service=DefaultTermService
val term=service createTerms("foo")
val term2=service getTerms term.id
println(term)
println(term2)
println(service.getTerms(UUID.randomUUID))
}
}
// when run:
Terms(b11c898d-ac44-4c7a-a77e-43edd1f786da,foo)
Terms(b11c898d-ac44-4c7a-a77e-43edd1f786da,foo)
Exception in thread "main" java.util.NoSuchElementException: key not found: 68dc0cd1-9101-41dc-af0e-ab67a7704f92
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:58)
at scala.collection.mutable.HashMap.apply(HashMap.scala:64)
at DefaultTermService$.getTerms(Ontology.scala:20)
at Ontology$.main(Ontology.scala:37)
at Ontology.main(Ontology.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment