是否有用于无符号数字类型包装器的 Java 库? [英] Is there a Java library for unsigned number type wrappers?

查看:32
本文介绍了是否有用于无符号数字类型包装器的 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:

所有这些都从表面上描述了它可以如何完成.但是是否有任何库实际上一直在为 UByteUShortUIntegerULong 实现合适的包装器?最好,这些包装器将扩展 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-bit16-bit32-bit64-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

我知道有些人可能认为这有点矫枉过正,但我​​真的很想拥有那些包装其他语言称为 ubyteushort 的包装器uintulong.希望通过 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!

这篇关于是否有用于无符号数字类型包装器的 Java 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆