views:

76

answers:

3

i'm having issues trying to get the result I wish. Basically what I want to do is have a Boolean object which will allow me to have 3 choices, if a mailer is old i want it to be set to false (meaning does not contain "mapQ.cmd" and "add-coid.cmd" file)

if a mailer is new I want it to set to true (if it is new it will contain "mapQ.cmd" and "add-coid.cmd" file in the directory), and if it is neither an old or new mailer (meaning not a mailer) then I wish for it to be null.

This is what I have, I want to place an elseif instead of the else, and do an else inside that to set the null value, meaning non of the above, then i wish to return the boolean. local-build-deploy.cmd is used in the example but i wish to use the above file names

private boolean isOldMailer(File mailerFolder) {
    File localBuildAndDeploy = new File(mailerFolder,"test/local-build-deploy.cmd");
    if (localBuildAndDeploy.exists()) {
        return true;
    } else {
        return false;
    }
}
+1  A: 

This is ternary logic, not binary logic. It's typically used in relational databases.

Boolean is binary, of course - just true or false.

If you want ternary logic, wrap it in your own type.

duffymo
A: 

Use Boolean - the wrapper object on primitive boolean. In that way, you can set the reference to null or true or false.

Shamik
Shamik - every developer expects a Boolean to be either true or false. Even the language, with it's auto-unboxing assumes that. It's just very unfortunate that null is a technically possible value. I would never use that as _expected_ behavior in a public API.
extraneon
I agree that it is ugly way to do it.
Shamik
+4  A: 

There are 2 ways that you can do this.

If you insist on using Boolean, use the capital B version instead of lower case b. Capital B Boolean is an object and can be set to null and do what you describe. Lower case b boolean is a primitive and can not be set to null.

However, there is a better way that does not rely on using a boolean for 3 values when it is designed for 2.

Using an enum, you can define your types just how you want them and have exactly as many as you need. Here is an example and how you would use it.

public enum Status { NEW, OLD, NEITHER }

private Status isOldMailer(File mailerFolder) {
    File localBuildAndDeploy = new File(mailerFolder,"test/local-build-deploy.cmd");
    if (localBuildAndDeploy.exists())
        return Status.NEW;
    else if (/*Something else*/)
        return Status.OLD
    else
        return Status.NEITHER;
}
Alan Geleynse
sorry as you can tell i'm new to java, this is what i have so far.
OakvilleWork
public class MailerType { private boolean isOldMailer(File mailerFolder) { File localBuildAndDeploy = new File(mailerFolder,"test/local-build-deploy.cmd"); if (localBuildAndDeploy.exists()) { return true; } else if { return false; {else } } }
OakvilleWork
I updated this to work with the code you have. You will just need to fill in your exact conditions for each value.
Alan Geleynse
awesome!! thanks for such a quick reply. I was missing brackets and everything, suck at this stuff. does enum consume less memory than boolean in theory?
OakvilleWork
+1 for mentioning the enum. Using a Boolean as a tri-state variable in an API is just evil (and confusing for future maintainers).
extraneon