签入范围前提条件

发布于 2024-12-17 07:50:26 字数 464 浏览 4 评论 0原文

我喜欢番石榴前提条件,但我真正需要的是另一种方法 - 检查数字是否在范围内。 我相信

//probably there should be checkStateInRange also
public static void checkArgumentInRange(double value, int min, int max) {
    if (value < min || value > max) {
        throw new IllegalArgumentException(String.format("%s must be in range [%s, %s]", value, min, max));
    }
}

我并不孤单,而且这是一个很常见的情况。但这样的方法并不存在。有什么理由不将此类方法放入 com.google.common.base.Preconditions 中?

I like guava preconditions, but what I really need from it is one more method - check that the number is in range. Smt like this

//probably there should be checkStateInRange also
public static void checkArgumentInRange(double value, int min, int max) {
    if (value < min || value > max) {
        throw new IllegalArgumentException(String.format("%s must be in range [%s, %s]", value, min, max));
    }
}

I believe I'm not alone and it's a pretty common case. But such method doesn't exist. Is there any reasons to not put such methods in com.google.common.base.Preconditions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-12-24 07:50:26

我想说的原因有很多。主要如下:

  • 对于“值超出范围”,没有标准的 Java 异常类型。请注意,每个 Preconditions 方法都会针对其检查的内容引发特定的异常类型:NullPointerExceptionIllegalArgumentExceptionIllegalStateExceptionIndexOutOfBoundsException。广义范围检查不会抛出比 IllegalArgumentException 更具体的异常。
  • checkArgumentcheckState 可以完成您需要的一切。您只需编写 checkArgument(value >= min && value <= max, ...) 即可。您要检查的内容简单明了。

另外:

  • 您可能需要太多不同的组合。 @rsp 提到的独占/包含边界等。
  • 限制为仅允许 int 为边界,因此您确实希望允许任何 Comparable 存在。
  • 此时您会注意到您只是检查该值是否包含在 范围

There are quite a few reasons I'd say. Here are the main ones:

  • There's no standard Java exception type for "value out of range". Note that each of the Preconditions methods throws a specific exception type for what it checks: NullPointerException, IllegalArgumentException, IllegalStateException or IndexOutOfBoundsException. A generalized range check would have no exception more specific than IllegalArgumentException to throw.
  • checkArgument and checkState do everything you need. You can just write checkArgument(value >= min && value <= max, ...). It's simple and obvious what you're checking.

Additionally:

  • There are too many different combinations you might want here. Exclusive/inclusive bounds as @rsp mentions, etc.
  • It's limiting to only allow ints for the bounds, so you would really like to allow any Comparable there.
  • At this point you notice you're just checking if the value is contained in a Range.
别想她 2024-12-24 07:50:26

我认为您可以通过使用 checkArgument() 来非常接近:

checkArgument(min < value && value < max, "%s must be in range [%s, %s]", value, min, max);

如果您将消息字符串添加到您自己的常量定义中,则不会有太多样板文件,并且具有您需要的所有灵活性。

I think you can get very close by using checkArgument():

checkArgument(min < value && value < max, "%s must be in range [%s, %s]", value, min, max);

which, if you add the message strings to your own constant definitions, isn't much boilerplate and has all the flexibility you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文