Is there a Java library for unsigned number type wrappers?

Lukas Eder picture Lukas Eder · Nov 19, 2011 · Viewed 10.1k times · Source

Obviously, Java doesn't support unsigned number types natively, and that's not going to change soon (comments starting in 2002). However, when working with databases, such as MySQL, they may come in handy every now and then. There are a lot of questions dealing with how to simulate unsigned numbers. For example:

All of them superficially describe how it could be done. But is there any library actually going all the way and implementing suitable wrappers for UByte, UShort, UInteger, ULong? Preferably, those wrappers would extend java.lang.Number and provide an arithmetic API similar to that of java.math.BigInteger.

As can be seen in this document, there's a lot to think of, and a lot that can go wrong (e.g. how to bitwise shift, how to multiply, etc), so I don't want to do it myself. Also, I don't want to just use the next higher type (e.g. Short instead of Byte, etc.). I want the notion of an 8-bit, 16-bit, 32-bit, 64-bit number preserved, for best interaction with databases, for instance.

UPDATE:

Before you answer! Consider that I know all the workarounds, but I'd really really like to have exactly those 4 types with exactly the above properties. And maybe someone has already done that, so that's why I ask. No need to remind me of the workarounds.

Answer

Lukas Eder picture Lukas Eder · Nov 20, 2011

When I needed this functionality inside of jOOQ, I haven't found anything like it, so I rolled my own Open Source library that I call jOOU (U for Unsigned):

http://github.com/jOOQ/jOOU

I understand that some may think this is overkill, but I'd really like to have precisely those wrappers wrapping what other languages call ubyte, ushort, uint, ulong. Hopefully with Valhalla, those wrappers can be turned into value types.

Of course, contributions to the arithmetics / bitwise operation implementations are very welcome!