Created
June 8, 2015 14:01
-
-
Save hibikir/5793ffe80c545f9971d1 to your computer and use it in GitHub Desktop.
Test code for the implicits + future article
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
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