How are you doing to be using this check, precisely? Too often, exists and contains are used in quite clunky ways and you can eliminate them entirely with more idiomatic Scala
For example, your question implies you are doing something a little like this:
val fileCheck = { dbFile: SourceFile => dbFile.name == diskFile.name && dbFile.path == diskFile.path }
if (list1 exists fileCheck ) {
var Files = list1 filter fileCheck
for (file <- Files) {
// Do something with file
}
}
You can achieve this much more cleanly with a for comprehension which uses a guard to filter files matching your condition, as in
for (file <- list1 if file.name == diskFile.name && file.path == diskFile.path) {
// Do Something with file
}
On the other hand, if you wanted to do something different if there is no such match, like this:
val fileCheck = { dbFile: SourceFile => dbFile.name == diskFile.name && dbFile.path == diskFile.path }
if (list1 exists fileCheck ) {
val Files = list1 filter fileCheck
for (file <- Files) {
// Do something with file
}
} else {
// Meh! No matching files.
}
Then you could, for example, use the for comprehension with yield to give up the list of matching files
val files = for (file <- list1 if file.name == diskFile.name && file.path == diskFile.path) yield file
And do one thing if it is empty (Nil) and another if it is not. I still prefer that to using exists to perform the check and then reusing the check parameters to filter the list. There are many expressive ways to do this but it depends on the context.
Note: the for comprehension is more functional, less imperative (particularly when used with yield)
EDIT:
OK, the above was written before you posted your code. Firstly, in this case, I'd say you should probably stick with val dbFile = dbFiles.filter(...
; a list comprehension doesn't get you anything extra here, so using filter is more clear.
Secondly, you should use match. Match is almost always better than any chain of if...else if...else. A simple if...else is fine but else if is error prone.
Now, you could do it like this
val dbFile = dbFiles.filter(dbFile => dbFile.file.name == diskFile.name && dbFile.file.path == diskFile.path
dbFiles.length match {
case 0 => //Insert file into db
case 1 => //Update existing db record
case _ => //Must be more than one! Aroogah! Aroogah!
}
Which is nice and simple and will match every case. But Christian, consider this: if you have multiple db records, all with the same name and path and there's a real file that corresponds, surely all you need to do is keep one of those records and update it, while deleting the others? Let me show you how do do this with match
dbFile match {
case Nil => // Empty list, insert file into db
case first :: others => { // At least one match
for (errorFile <- others) {
SourceFile.delete(errorFile.id) // Assuming you have this or similar method
}
SourceFile.update(DBSourceFile(first.id, diskFile))
}
}
This works because first :: others
will match both a list of one or more. If there is only one element, others will be Nil, so the for comprehension will do nothing . Otherwise, it will contain the list of additional entries. If you care about which of the multiple entries you keep, you probably want either the first or the last entry; you can do that by adding a sort (presumably by id) to the line that builds dbFile
So you get to use a for comprehension in the end ;)