Global unique identifiers in Java, the easy way
A recent attempt at creating a unique identifier at work was shot down in flames by Findbugs. My naïve attempt wasn’t very thoughtful but seemed to work.
Findbugs indicated there was a possibility that I might end up with a negative value even though I “cleverly” used Math.abs().
RV: Bad attempt to compute absolute value of signed random integer (RV_ABSOLUTE_VALUE_OF_RANDOM_INT)
If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well
Even though I didn’t need this random id to be perfect — the unintended side-effect wasn’t a technical problem since I used it as a String — I still wanted to fix it. Stackoverflow to the rescue.
In the end, I happily replaced my homemade id generator with Java’s.
The output of which is something like 1c312843-8903-411f-88b2-ff1b92ca80ba.