Scenario:
1) I have a long state variable that can either be set or not set.
2) This long variable has valid values from Long.MIN_VALUE to Long.MAX_VALUE including zero
3) This is performance sensitive code so I don't want to use a Long wrapper type
How do I write an 'is set' kind of check for this long? Do I really have to add a second boolean value to test whether the long is valid or not? This seems sloppy. I know I could use a Long wrapper here but this seems like a performance waste to be creating so many objects and checking for null.
Pseudo Code (this is sort of what I want):
class foo {
long someLong = NaN; //NaN = hypothetical not a number like Double
public reset() {
someLong = NaN;
}
public doSomethingElse() {
if(someLong !=NaN) {
//report
reset();
}
}
public doSomeStuff() {
if(someLong == NaN) {
someLong = //something
}
}
}
}
You'd have to set aside a special value for NaN. If you really use all possible values, there is none.
Are you sure Long objects are that much of a performance problem?
If so, maybe have an extra boolean to denote if the value is set or not?
Use a Long instead of a long, and use null as the NaN value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With