Boolean vs boolean (s) as triline switches

I am developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. I have done the following:

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 good practice to use a Boolean as a trilean switch (it is null if the listener has not been parameterized yet), or should I use two booleans ... or maybe integers?

Do you have any ideas?

+4
source share
6 answers

It is best to use a type (possibly an enumeration) with exact descriptions of the three states. Booleans do not provide much information to the person who causes the function (especially when used as a tristate).

 public enum ActiveStatus { On, Off, Unknown } 
+14
source

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

+4
source

Here we must use a variable to represent any of the three status values: ON, OFF, or UNKNOWN. We also know that a variable cannot take values ​​other than these three values. Thus, we can say that these three values ​​represent a range of values ​​to which a state variable can be bound. When we think of other types of data, such as int, char, et., They also have a predefined range of values. Therefore, in this case, we need to create a custom data type with these three values ​​as a range. Enumerations are used in such scenarios and can be defined as Jeff Foster:

 public enum ActiveState { ON, OFF, UNKNOWN } 
+2
source

In Java 8, I recommend using 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 can probably write your own extra class (not sure, however, if this is the best solution).

+1
source

null not an "undefined value"; it is "not important at all" for any type of object, including Boolean . Therefore, when using the Boolean object, you still have only two meaningful values.

You must use more than one boolean flag or enumeration.

0
source

This is considered good practice.

Your data type is true or false or unspecified, with a null Boolean value perfect.

0
source

All Articles