开发者

Boolean vs boolean(s) as trilean switches

I'm developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. What I did is this:

buttonA.setOnTouchListener(new View.OnTouchListener() {
开发者_JAVA技巧                        private Boolean isActive = null;
                        private boolean isTrigger;
                        private int onLevel;
                        private int offLevel;
                        private int chIdx;

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (isActive == null) {
                                Cursor btnSettings = dbHelper.getButtonsTable().fetchButton(1, profileId, currentMode);
                                ...
...
                            }
                            return true;
                        }

Is it considered a good practice to use the Boolean object as a trilean switch (it has a null value if the listener haven't been parameterized yet) or I should use two boolean variables...or maybe an integer?

Do you have and ideas?


Best yet, use an type (probably an enum) with accurate descriptions of the three states. Booleans don't give a lot of information to the person who is calling the function (especially when used as a tristate).

public enum ActiveStatus {
  On,
  Off,
  Unknown
}


I would say either use Boolean with true, false and null or use an enum. I tend to use a Boolean being null as a kind of "don't know yet". If you are using null as something more meaningful than "don't know" you are probably semantically better off going with an enum.


Here we have to use a variable to represent any of the three status values: ON, OFF, or UNKNOWN. We also know that the variable cannot take any value other than these three values. So, we can say that those three values are the range of values that the status variable can mapped to. When we think about other data types such as int, char, et., they also have predefined range of values. Therefore, in this case we need to create a custom data type with those three values as its range. Enumerations are used in these kind of scenarios and can be defined as Jeff Foster written:

public enum ActiveState {
    ON, OFF, UNKNOWN
}


In Java 8 I'd recommend to use Optional<Boolean>:

//Declaration:
Optional<Boolean> v;

//Setting:
v = Optional.empty(); //null
v = Optional.<Boolean>empty(); //null again
v = Optional.of(true);
v = Optional.of(false);
// (you may declare Trileans class with these constants)

//Checking:
if (v.isPresent()) /*v is not null*/;
if (v.isPresent() && v.get()) /*v is true*/;
if (v.isPresent() && !v.get()) /*v is false*/;
if (v1.equals(v2)) /*v1 and v2 are the same*/;
// (you may declare Trileans class with more operations)

For Android you probably can write your own Optional class (not sure, however, whether it's the best solution).


null is not "unspecified value", it's "no value at all" for any object type -- including Boolean. Hence you still have only two meaningful values when using Boolean object.

You should either use more than one boolean flag or an enum.


It is considered a good practice by me.

Your data type is true or false or unspecified, nullable Boolean fits perfectly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜