Boolean vs boolean(s) as trilean switches

余生长醉 提交于 2020-01-03 17:48:10

问题


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() {
                        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?


回答1:


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
}



回答2:


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.




回答3:


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
}



回答4:


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).




回答5:


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.




回答6:


It is considered a good practice by me.

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



来源:https://stackoverflow.com/questions/6776002/boolean-vs-booleans-as-trilean-switches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!