From dc5e9526d73ee9b449c5032d9df9e59e091396c8 Mon Sep 17 00:00:00 2001 From: Peter Crotty Date: Tue, 24 Apr 2012 12:42:13 +0100 Subject: [PATCH 1/3] Add missing space to readme. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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

From 4ccfdd7a923026b34e53558c8d5c7229ad6adda0 Mon Sep 17 00:00:00 2001 From: Peter Crotty Date: Tue, 24 Apr 2012 14:18:16 +0100 Subject: [PATCH 2/3] My solutions for tests without making Extended User have a constructor which takes in a User. --- src/main/scala/dojo/ImplicitJava.scala | 10 +++++++++- src/main/scala/dojo/UserLookup.scala | 24 ++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) 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) + } } From 0b639c3b24a6e45e479297adb10fd38b1254e8d1 Mon Sep 17 00:00:00 2001 From: Peter Crotty Date: Mon, 1 Apr 2013 22:45:17 +0100 Subject: [PATCH 3/3] Checking in all before giving back pc. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 89292d4..5bf5bb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +bugger0ff *.class .DS_Store .idea/*