简体   繁体   English

Nullsafe Long valueOf

[英]Nullsafe Long valueOf

Imagine the following scenario Long critId = Long.valueOf(criteriaIdentifier);想象以下场景Long critId = Long.valueOf(criteriaIdentifier);

If the view is initially loaded criteriaIdentifier may be null, afterwards I set the value depending of the selectedItem in the JSF View.如果视图最初加载, criteriaIdentifier可能为空,之后我根据 JSF 视图中的selectedItem设置值。

However, Long.valueOf(criteriaIdentifier) throws a NumberFormatException - If the string cannot be parsed as a long(ie null ).但是, Long.valueOf(criteriaIdentifier)抛出NumberFormatException - 如果字符串不能被解析为 long(即null )。

I've thought of default setting the Id to default -1 , but I wanted to know if there is better practice .我想过将 Id 默认设置为 default -1 ,但我想知道是否有更好的做法。

PS: technically the criteriaIdentifier can't be -1, since these values are set from the Database, with a Sequence Generator, so I should know -1 was the default value and properly do the default operations PS:从技术上讲, criteriaIdentifier不能是 -1,因为这些值是从数据库中设置的,使用序列生成器,所以我应该知道 -1 是默认值并正确执行默认操作

You can use the NumberUtils from Apache Commons.您可以使用 Apache Commons 中的NumberUtils It's null-safe and you can optionally specify a default value.它是空安全的,您可以选择指定默认值。

Example:示例:

NumberUtils.toLong(null) = 0L
NumberUtils.toLong("")   = 0L
NumberUtils.toLong("1")  = 1L

NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L)   = 1L
NumberUtils.toLong("1", 0L)  = 1L

For more info, check the API .有关更多信息,请查看API

Long.valueOf(null) will throw NumberFormatException , not NullPointerException . Long.valueOf(null)将抛出NumberFormatException ,而不是NullPointerException

In Java 8, you have the possibility to declaratively choose a default evaluation for a nullable inline, eg在 Java 8 中,您可以声明性地为可空内联选择默认评估,例如

Long.valueOf(
    Optional.ofNullable(criteriaIdentifier).orElseGet(() -> "-1")
)

It's a little verbose, but will allow you to default to -1l if criteriaIdentifier is null (no need for additional dependencies).它有点冗长,但如果criteriaIdentifiernull ,则允许您默认为-1l (不需要额外的依赖项)。

API here and here . API 在这里这里

I'm assuming criteriaIdentifier is a String.我假设criteriaIdentifier 是一个字符串。 You could use org.apache.commons.lang3.math.NumberUtils.toLong(String, long) which gives you the default value if the String argument is null.您可以使用org.apache.commons.lang3.math.NumberUtils.toLong(String, long)如果 String 参数为空它会为您提供默认值。 Or you use org.apache.commons.lang3.math.NumberUtils.toLong(String) which returns zero if the String is null.或者您使用org.apache.commons.lang3.math.NumberUtils.toLong(String)如果字符串为空则返回零。

It depends on your scenario but usually there are ways to avoid such problems.这取决于您的情况,但通常有办法避免此类问题。

Generally setting IDs to -1 is a common practice but I don't think it is a good one.通常将 ID 设置为 -1 是一种常见做法,但我认为这不是一个好的做法。 You know that -1 is not a possible value in your scenario but considering just the type Long it is a valid value.您知道 -1 在您的场景中不是一个可能的值,但仅考虑 Long 类型,它是一个有效值。 So everywhere in the code you will need to implement tests like if id!=-1 which makes the code harder to understand and generally the null value is more suitable for that scenario - Long object without a value.所以在代码中的任何地方你都需要实现像 if id!=-1这样的测试,这使得代码更难理解,通常null值更适合这种情况 - 没有值的长对象。

If it was up to me for example in the method that receives an ID and waits a valid ID if I get null I would throw an exception and let the caller of the method handle that.如果这取决于我,例如在接收 ID 并等待有效 ID 的方法中,如果我得到 null,我会抛出异常并让方法的调用者处理它。 If it is a web API for example (otherwise why would you get a String?) then you can throw an HTTP error.例如,如果它是一个 Web API(否则为什么你会得到一个字符串?)那么你可以抛出一个 HTTP 错误。 Or if you want to handle bad arguments then you better return null or use null as default value.或者,如果您想处理错误的参数,则最好返回 null 或使用 null 作为默认值。

When we say nullSafe we usually desire to receive a null from the api when passing null through it, not a 0 or -1 or any other value.当我们说 nullSafe 时,我们通常希望在通过它传递null时从 api 接收一个null ,而不是0-1或任何其他值。

For this, use createLong static method of the org.apache.commons.lang3.math.NumberUtils .为此,请使用org.apache.commons.lang3.math.NumberUtils createLong静态方法。

CAUSION*: This throws NumberFormatException if the input String is NOT null and is NOT numeric. CAUSION*:如果输入字符串不为null且不是数字,则抛出 NumberFormatException。

CAUSION**: since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations.小心**:从 3.1 开始,它处理十六进制 (0Xhhhh) 和八进制 (0ddd) 符号。 NB a leading zero means octal; NB 前导零表示八进制; spaces are not trimmed.没有修剪空格。

Sample:样品:

NumberUtils.createLong(null) --> null
NumberUtils.createLong("1") --> 1
NumberUtils.createLong("hi") --> NumberFormatException
NumberUtils.createLong(" 11") --> NumberFormatException (does not trim)
NumberUtils.createLong("023") --> 19 (leading 0 means octal)
NumberUtils.createLong("0x23") --> 35 (leading 0x means hex)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM