M
Mohun Biswas
A relational-database boolean can have three values: true, false, and
"unknown" aka NULL. Is there a commonly accepted idiom for representing
the analogous situation in Java? There's no primitive type which seems
to fit the bill. Of course one could use an integer type and use 0 for
false, 1 for true, and -1 for null but that's not pretty. The best I can
think of is to use an exception for the third value, e.g.
class MyClass {
private boolean setting;
private boolean settingInitialized = false;
public void setSetting(boolean setting) {
this.setting = setting;
settingInitialized = true;
}
public boolean getSetting() {
if (settingInitialized == false)
throw new NullSettingException();
return setting;
}
// In case the user would prefer to avoid handling exceptions.
public boolean isSettingInitialized() {
return settingInitialized ;
}
}
Is there another, better way?
Thanks,
MB
"unknown" aka NULL. Is there a commonly accepted idiom for representing
the analogous situation in Java? There's no primitive type which seems
to fit the bill. Of course one could use an integer type and use 0 for
false, 1 for true, and -1 for null but that's not pretty. The best I can
think of is to use an exception for the third value, e.g.
class MyClass {
private boolean setting;
private boolean settingInitialized = false;
public void setSetting(boolean setting) {
this.setting = setting;
settingInitialized = true;
}
public boolean getSetting() {
if (settingInitialized == false)
throw new NullSettingException();
return setting;
}
// In case the user would prefer to avoid handling exceptions.
public boolean isSettingInitialized() {
return settingInitialized ;
}
}
Is there another, better way?
Thanks,
MB