0

I need to read a flag to validate if I'll send or not an email, this flag is on a LinkedHashMap, so I'm trying to go over it looking for an ID, then I'll have to ask if is true or false, and I'm trying this:

    while(iterator.hasNext()){

         LinkedHashMap<String, Object> fileMap = (LinkedHashMap<String, Object>) iterator.next();
         userid = (Long)fileMap.get("USERID");
         file.findAllFiles(userid);

         if(file.findAllFiles(userid).contains(haveFiles = true)){
             //send email
         }else{
             //do something
         }

     }

Is it correct?

dds
  • 2,335
  • 1
  • 31
  • 45
rpassenger
  • 11
  • 2

2 Answers2

0

Assuming it works up to the point of:

if(file.findAllFiles(userid).contains(haveFiles = true))

Then I think the issue is in that condition itself. What you have written is roughly equivalent of writing

??? haveFiles = true;    
if(file.findAllFiles(userid).contains(haveFiles))

(where ??? can be either Object, Boolean or boolean, don't know from your code snapshot)

I bet what you might want to do is something like

Boolean haveFiles = file.findAllFiles(userid).get("haveFiles");
if (Boolean.TRUE.equals(haveFiles) {

What you have done calls the contains method which receives any Object and returns true if that object is among the values of the map. You probably don't want that, I guess what you want to do is to retrieve the value associated to the "haveFiles" key into the map, for that you have to use the get method.

PS: Also, if file.findAllFiles(userid) method is truly calling some DB stuff you might want to invoke it only once (the first time looks totally unnecesary to me).

Claudio
  • 1,848
  • 12
  • 26
0

I think your if expression has a logical error.

haveFiles = true

will always return true, reducing your expression to :

file.findAllFiles(userid).contains(true)

since (haveFiles = true) assigns the value true to the variable haveFiles.

You should rather be doing something on the following lines :

Map<Flag, Boolean> keyValueMap = file.findAllFiles(userid)
if(keyValueMap.contains(Flag.HAVE_FILES) == true){
     //send email
 }else{
     //do something
 }

..asuming you the file.findAllFiles() returns a map object with flags and their boolean status and Flag is an enum containing all the supported flags.

The expression

(keyValueMap.contains(Flag.HAVE_FILES) == true)

will evaluate to true only when keyValueMap contains an entry for Flag.HAVE_FILES and the value is true.

DeKay
  • 56
  • 1
  • 3