是否有用于无符号数字类型包装器的 Java 库?
显然,Java 本身不支持无符号数字类型,这不是很快就会改变(从 2002 年开始的评论).但是,在使用数据库(例如 MySQL)时,它们可能会时不时地派上用场.关于如何模拟无符号数有很多问题.例如:
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:
- java中的unsigned short
- Java:无符号数
- 理解 Java 无符号数
所有这些都从表面上描述了它可以如何完成.但是是否有任何库实际上一直在为 UByte
、UShort
、UInteger
、ULong
实现合适的包装器?最好,这些包装器将扩展 java.lang.Number
并提供类似于 java.math.BigInteger
的算术 API.
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
.
从本文档中可以看出,有很多事情要考虑,还有一个很多可能出错(例如如何按位移位,如何乘法等),所以我不想自己做.另外,我不想只使用下一个更高的类型(例如 Short
而不是 Byte
等).我想要 8-bit
、16-bit
、32-bit
、64-bit
数字的概念例如,为了与数据库进行最佳交互而保留.
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.
更新:
在你回答之前!考虑到我知道所有的解决方法,但我真的很想拥有完全具有上述属性的这 4 种类型.也许有人已经这样做了,所以这就是我问的原因.无需提醒我解决方法.
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.
推荐答案
当我在 jOOQ 中需要这个功能时,我还没有找到类似的东西,所以我推出了自己的开源库,我称之为 jOOU(U 代表无符号):
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
我知道有些人可能认为这有点矫枉过正,但我真的很想拥有那些包装其他语言称为 ubyte
、ushort
、 的包装器uint
,ulong
.希望通过 Valhalla,这些包装器可以转换为值类型.
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!
相关文章