Skip to content

Instantly share code, notes, and snippets.

@hibikir
Created June 8, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hibikir/5793ffe80c545f9971d1 to your computer and use it in GitHub Desktop.
Save hibikir/5793ffe80c545f9971d1 to your computer and use it in GitHub Desktop.
Test code for the implicits + future article
case class Employee(id:Long, name:String)
case class Role(name:String,department:String)
case class EmployeeWithRole(id:Long,name:String,role:Role)
trait EmployeeGrabbedBabber{
def rawEmployee(id:Int) :Employee
def rawRole(e:Employee) :Role
def employee(id:Int)(implicit e:ExecutionContext) :Future[Employee]
def role(employee:Employee)(implicit e:ExecutionContext) : Future[Role]
}
/* This is test code that fakes some delays. Don't sleep on threads on production code! */
object DAO extends EmployeeGrabbedBabber{
private def delay[T,V](f:(T)=>V,millis:Int)(t:T)(implicit ec: ExecutionContext):Future[V] = {
Future{
Thread.sleep(millis)
f(t)
}
}
def rawEmployee(id:Int) = new Employee(id,"Bob"+id)
def rawRole(e:Employee) = new Role(s"{whatever ${e.name} does", "Accounting")
def employee(id:Int)(implicit e:ExecutionContext) = delay(rawEmployee _,200)(id)(e)
def role(employee:Employee)(implicit e:ExecutionContext) = delay(rawRole _ ,100)(employee)(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment