开发者

using Boolean Object

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 开发者_如何学Goused 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;
    }
}


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;
}


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.


(I'll go over three common options and then suggest the third).

The first option is to use a Boolean and set it to true, false or null. This has a few benefits:

  1. Assuming you first check to ensure the value is not null, you can use it directly in boolean expressions.
  2. It's a somewhat controversial point, but null really isn't too far off from "none of the possible values" (i.e. neither true nor false), so it's a reasonable model. Many disagree.
  3. Concise.

However, some people, reasonably or not, expect a Boolean to be either true or false, and do not consider the null possibility, which can easily lead to bugs.

The second option is to use an enum:

  1. No real risk of misuse, since null is not an option, but...
  2. You lose the boolean semantics.
  3. Depending on what you're modelling, it may or may not be aesthetic to introduce a custom enum.

The third--and recommended--option is to use an Optional< Boolean > from Google's excellent Guava library:

  1. It's a very common library.
  2. It's self-documenting.
  3. It has well-defined semantics.
  4. Null is not an issue.
  5. Boolean semantics are just a get away.
  6. Converting to/from the null-based model in the first option, above, is very concise and easy-to-read.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜