提交 bc1b4559 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not use BigInteger in Sequence.isValid()

上级 fc6ac01e
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
*/ */
package org.h2.schema; package org.h2.schema;
import java.math.BigInteger;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -139,17 +138,14 @@ public class Sequence extends SchemaObjectBase { ...@@ -139,17 +138,14 @@ public class Sequence extends SchemaObjectBase {
* @param maxValue the prospective max value * @param maxValue the prospective max value
* @param increment the prospective increment * @param increment the prospective increment
*/ */
private static boolean isValid(long value, long minValue, long maxValue, private static boolean isValid(long value, long minValue, long maxValue, long increment) {
long increment) {
return minValue <= value && return minValue <= value &&
maxValue >= value && maxValue >= value &&
maxValue > minValue && maxValue > minValue &&
increment != 0 && increment != 0 &&
// Math.abs(increment) < maxValue - minValue // Math.abs(increment) < maxValue - minValue
// use BigInteger to avoid overflows when maxValue and minValue // Can use Long.compareUnsigned() on Java 8
// are really big Math.abs(increment) + Long.MIN_VALUE < maxValue - minValue + Long.MIN_VALUE;
BigInteger.valueOf(increment).abs().compareTo(
BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0;
} }
private static long getDefaultMinValue(Long startValue, long increment) { private static long getDefaultMinValue(Long startValue, long increment) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论