处理长最小值条件

huangapple go评论94阅读模式
英文:

Handle long min value condition

问题

When I ran a program, long min value is getting persisted instead of the original value coming from the backend.

I am using the code:

if (columnName.equals(Fields.NOTIONAL)) {
    orderData.notional(getNewValue(data));

As the output of this, I am getting the long min value instead of the original value.

I tried using this method to handle the scenario:

public String getNewValue(Object data) {
    return ((Long)data).getLong("0")==Long.MIN_VALUE?"":((Long)data).toString();
}

But it doesn't work.

Please suggest.

英文:

When I ran a program, long min value is getting persisted instead of original value coming from the backend.

I am using the code:

if (columnName.equals(Fields.NOTIONAL)) {
			orderData.notional(getNewValue(data));

As output of this, i am getting long min value, instead of original value.

I tried using this method to handle the scenario

public String getNewValue(Object data) {
		return ((Long)data).getLong("0")==Long.MIN_VALUE?"":((Long)data).toString();
	}

but doesn't work.

Please suggest

答案1

得分: 2

(Long) data).getLong("0")是一个愚蠢的方式来表示null,因为它并没有执行任何操作。它检索名为'0'的系统属性,然后尝试将其解析为Long值。换句话说,如果你使用java -D0=1234 com.foo.YourClass启动VM,它会返回1234。我甚至不知道你试图用这个调用做什么。显然,它不等于Long.MIN_VALUE,因此该方法返回((Long) data).toString()。如果data实际上是表示MIN_VALUE的Long,你将得到MIN_VALUE的数字,显然不是你想要的。

尝试这个:

public String getNewValue(Object data) {
    if (data instanceof Number) {
        long v = ((Number) data).longValue();
        return v == Long.MIN_VALUE ? "" : data.toString();
    }
    // 如果输入不是数值对象,你想要返回什么?
    return "";
}
英文:

EDITED: I misread the code in the question; rereading it, I now get what the author is trying to do, and cleaned up the suggestion as a consequence.

(Long) data).getLong("0") is a silly way to write null, because that doesn't do anything. It retrieves the system property named '0', and then attempts to parse it as a Long value. As in, if you start your VM with java -D0=1234 com.foo.YourClass, that returns 1234. I don't even know what you're attempting to accomplish with this call. Obviously it is not equal to Long.MIN_VALUE, thus the method returns ((Long) data).toString(). If data is in fact a Long representing MIN_VALUE, you'll get the digits of MIN_VALUE, clearly not what you wanted.

Try this:

public String getNewValue(Object data) {
    if (data instanceof Number) {
        long v = ((Number) data).longValue();
        return v == Long.MIN_VALUE ? "" : data.toString();
    }
// what do you want to return if the input isn't a numeric object at all?
    return "";

huangapple
  • 本文由 发表于 2020年7月28日 16:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定