diff --git a/.gitignore b/.gitignore
index 89292d4..5bf5bb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+bugger0ff
*.class
.DS_Store
.idea/*
diff --git a/README.md b/README.md
index aa01d77..6948655 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ Today we look at how easily you can write Scala that works with Java code as wel
Implicit Java
-This week you will find some Java classes as well as the usual Scala ones. You will be implementing functions on the UserLookup Scala class, which itself queries a Java implemented DataSource class through its findUsersmethod, from which will be returned a Java collection of User's. As usual work though by getting the tests to pass one at a time, and keep an eye on the couple of comments which hint at the direction you may want to take.
+This week you will find some Java classes as well as the usual Scala ones. You will be implementing functions on the UserLookup Scala class, which itself queries a Java implemented DataSource class through its findUsers method, from which will be returned a Java collection of User's. As usual work though by getting the tests to pass one at a time, and keep an eye on the couple of comments which hint at the direction you may want to take.
Building the project
diff --git a/src/main/scala/dojo/ImplicitJava.scala b/src/main/scala/dojo/ImplicitJava.scala
index 9fdd07c..b466f2f 100644
--- a/src/main/scala/dojo/ImplicitJava.scala
+++ b/src/main/scala/dojo/ImplicitJava.scala
@@ -4,6 +4,14 @@ import com.google.common.base.Predicate
object ImplicitJava {
- def funcToPred(function: (User) => Boolean) : Predicate[User] = null
+ implicit def funcToPred(function: (User) => Boolean) : Predicate[User] = {
+ new Predicate[User] {
+ def apply(p1: User): Boolean = function(p1)
+ }
+ }
+
+ implicit def userToExtendedUser(user:User) : ExtendedUser = {
+ new ExtendedUser(user.getId, user.getName, user.isMale, user.getAge)
+ }
}
diff --git a/src/main/scala/dojo/UserLookup.scala b/src/main/scala/dojo/UserLookup.scala
index 5799b36..812094b 100644
--- a/src/main/scala/dojo/UserLookup.scala
+++ b/src/main/scala/dojo/UserLookup.scala
@@ -1,6 +1,8 @@
package dojo
import java.util.{ArrayList, List}
+import ImplicitJava._
+import scala.collection.JavaConversions._
class UserLookup(dataSource :DataSource) extends JUserLookup {
@@ -9,15 +11,29 @@ class UserLookup(dataSource :DataSource) extends JUserLookup {
can you do it in the Scala way by passing in a function?
Is there a way to imlicit[ly] convert a function to a Predicate?
*/
- def olderThan(age :Int): List[User] = new ArrayList[User]()
+ def olderThan(age :Int): List[User] = {
+ dataSource
+ .findUsers((user:User) => (user.getAge > age))
+ }
/*
Are there standard JavaConversions to make it easier to work with Java collections?
*/
- def namesYoungerThan(age: Int):List[String] = new ArrayList[String]()
+ def namesYoungerThan(age: Int):List[String] = { null
+ dataSource
+ .findUsers((user:User) => (user.getAge < age))
+ .map(item => item.getName)
+ }
- def allFemale(): List[String] = new ArrayList[String]()
+ def allFemale(): List[String] = {
+ dataSource
+ .findUsers((user:User) => (!user.isMale))
+ .map(item => item.getName)
+ }
- def allEligible() = new ArrayList[User]()
+ def allEligible() = {
+ dataSource
+ .findUsers((user:User) => user.isEligible)
+ }
}