Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bugger0ff
*.class
.DS_Store
.idea/*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Today we look at how easily you can write Scala that works with Java code as wel
<h4 style="font-size: 13pt; line-height: normal; margin-bottom: 4px; margin-left: 0px; margin-right: 0px; margin-top: 25px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="" name="ScalaDojo04-implicitjava-ImplicitJava"></a>Implicit Java</h4>
<div style="font-size: 10pt; line-height: 13pt; margin-bottom: 10px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
This week you will find some Java classes as well as the usual Scala ones. You will be implementing functions on the&nbsp;<em>UserLookup</em>&nbsp;Scala class, which itself queries a Java implemented&nbsp;<em>DataSource</em>&nbsp;class through its&nbsp;<em>findUsers</em>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.</div>
This week you will find some Java classes as well as the usual Scala ones. You will be implementing functions on the&nbsp;<em>UserLookup</em>&nbsp;Scala class, which itself queries a Java implemented&nbsp;<em>DataSource</em>&nbsp;class through its&nbsp;<em>findUsers</em> 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.</div>
<h4 style="font-size: 13pt; line-height: normal; margin-bottom: 4px; margin-left: 0px; margin-right: 0px; margin-top: 25px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="" name="ScalaDojo04-implicitjava-Buildingtheproject"></a>Building the project</h4>
<div style="font-size: 10pt; line-height: 13pt; margin-bottom: 10px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/dojo/ImplicitJava.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
24 changes: 20 additions & 4 deletions src/main/scala/dojo/UserLookup.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dojo

import java.util.{ArrayList, List}
import ImplicitJava._
import scala.collection.JavaConversions._

class UserLookup(dataSource :DataSource) extends JUserLookup {

Expand All @@ -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)
}

}