提交 1da8c499 authored 作者: Thomas Mueller's avatar Thomas Mueller

Use signum and compare methods.

上级 ff4d721e
......@@ -22,6 +22,7 @@ import org.h2.schema.SchemaObjectBase;
import org.h2.table.Column;
import org.h2.table.IndexColumn;
import org.h2.table.Table;
import org.h2.util.MathUtils;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.value.Value;
......@@ -280,7 +281,7 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
if (isMultiVersion) {
int v1 = rowData.getVersion();
int v2 = compare.getVersion();
return v1 == v2 ? 0 : v1 < v2 ? 1 : -1;
return MathUtils.compare(v2, v1);
}
return 0;
}
......
......@@ -12,6 +12,7 @@ import org.h2.engine.Session;
import org.h2.message.Message;
import org.h2.result.Row;
import org.h2.result.SearchRow;
import org.h2.util.MathUtils;
/**
* The cursor implementation for the multi-version index.
......@@ -149,7 +150,7 @@ public class MultiVersionCursor implements Cursor {
// version would be compared as well
long k1 = deltaRow.getKey();
long k2 = baseRow.getKey();
compare = k1 == k2 ? 0 : k1 > k2 ? 1 : -1;
compare = MathUtils.compare(k1, k2);
}
if (compare == 0) {
if (isDeleted) {
......
......@@ -550,8 +550,7 @@ public class WebServer implements Service {
}
Collections.sort(settings, new Comparator<ConnectionInfo>() {
public int compare(ConnectionInfo o1, ConnectionInfo o2) {
int c = o2.lastAccess - o1.lastAccess;
return c < 0 ? -1 : c > 0 ? 1 : 0;
return MathUtils.compare(o2.lastAccess, o1.lastAccess);
}
}
);
......
......@@ -17,6 +17,7 @@ import java.util.StringTokenizer;
import org.h2.message.Message;
import org.h2.util.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.MathUtils;
import org.h2.util.New;
import org.h2.util.StringUtils;
import org.h2.util.Tool;
......@@ -45,9 +46,9 @@ public class ConvertTraceFile extends Tool {
if (other == this) {
return 0;
}
int c = other.time > time ? 1 : other.time < time ? -1 : 0;
int c = MathUtils.compare(other.time, time);
if (c == 0) {
c = other.executeCount > executeCount ? 1 : other.executeCount < executeCount ? -1 : 0;
c = MathUtils.compare(other.executeCount, executeCount);
if (c == 0) {
c = sql.compareTo(other.sql);
}
......
......@@ -212,8 +212,7 @@ public class ByteUtils {
return b > b2 ? 1 : -1;
}
}
int c = data1.length - data2.length;
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return Integer.signum(data1.length - data2.length);
}
/**
......
......@@ -22,9 +22,7 @@ public abstract class CacheObject {
*/
static class CacheComparator implements Comparator<CacheObject> {
public int compare(CacheObject a, CacheObject b) {
int pa = a.getPos();
int pb = b.getPos();
return pa == pb ? 0 : (pa < pb ? -1 : 1);
return MathUtils.compare(a.getPos(), b.getPos());
}
}
......
......@@ -205,4 +205,28 @@ public class MathUtils {
return (reverse((int) (x >>> 32L)) & 0xffffffffL) ^ (((long) reverse((int) x)) << 32L);
}
/**
* Compare two values. Returns -1 if the first value is smaller, 1 if bigger,
* and 0 if equal.
*
* @param a the first value
* @param b the second value
* @return the result
*/
public static int compare(int a, int b) {
return a == b ? 0 : a < b ? -1 : 1;
}
/**
* Compare two values. Returns -1 if the first value is smaller, 1 if bigger,
* and 0 if equal.
*
* @param a the first value
* @param b the second value
* @return the result
*/
public static int compare(long a, long b) {
return a == b ? 0 : a < b ? -1 : 1;
}
}
......@@ -11,6 +11,7 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.message.Message;
import org.h2.util.MathUtils;
/**
* Implementation of the BYTE data type.
......@@ -50,7 +51,7 @@ public class ValueByte extends Value {
}
public int getSignum() {
return value == 0 ? 0 : (value < 0 ? -1 : 1);
return Integer.signum(value);
}
public Value negate() throws SQLException {
......@@ -98,10 +99,7 @@ public class ValueByte extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueByte v = (ValueByte) o;
if (value == v.value) {
return 0;
}
return value > v.value ? 1 : -1;
return MathUtils.compare(value, v.value);
}
public String getString() {
......
......@@ -65,8 +65,7 @@ public class ValueDate extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueDate v = (ValueDate) o;
int c = value.compareTo(v.value);
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return Integer.signum(value.compareTo(v.value));
}
public String getString() {
......
......@@ -115,8 +115,7 @@ public class ValueDecimal extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueDecimal v = (ValueDecimal) o;
int c = value.compareTo(v.value);
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return value.compareTo(v.value);
}
public int getSignum() {
......
......@@ -11,6 +11,7 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.message.Message;
import org.h2.util.MathUtils;
/**
* Implementation of the INT data type.
......@@ -80,7 +81,7 @@ public class ValueInt extends Value {
}
public int getSignum() {
return value == 0 ? 0 : (value < 0 ? -1 : 1);
return Integer.signum(value);
}
public Value negate() throws SQLException {
......@@ -132,10 +133,7 @@ public class ValueInt extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueInt v = (ValueInt) o;
if (value == v.value) {
return 0;
}
return value > v.value ? 1 : -1;
return MathUtils.compare(value, v.value);
}
public String getString() {
......
......@@ -614,8 +614,7 @@ public class ValueLob extends Value {
protected int compareSecure(Value v, CompareMode mode) throws SQLException {
if (type == Value.CLOB) {
int c = getString().compareTo(v.getString());
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return Integer.signum(getString().compareTo(v.getString()));
}
byte[] v2 = v.getBytesNoCopy();
return ByteUtils.compareNotNull(getBytes(), v2);
......
......@@ -12,6 +12,7 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.message.Message;
import org.h2.util.MathUtils;
/**
* Implementation of the BIGINT data type.
......@@ -51,9 +52,9 @@ public class ValueLong extends Value {
ValueLong other = (ValueLong) v;
if (SysProperties.OVERFLOW_EXCEPTIONS) {
long result = value + other.value;
int sv = value == 0 ? 0 : (value < 0 ? -1 : 1);
int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1);
int sr = result == 0 ? 0 : (result < 0 ? -1 : 1);
int sv = Long.signum(value);
int so = Long.signum(other.value);
int sr = Long.signum(result);
// if the operands have different signs overflow can not occur
// if the operands have the same sign,
// and the result has a different sign, then it is an overflow
......@@ -67,7 +68,7 @@ public class ValueLong extends Value {
}
public int getSignum() {
return value == 0 ? 0 : (value < 0 ? -1 : 1);
return Long.signum(value);
}
public Value negate() throws SQLException {
......@@ -86,8 +87,8 @@ public class ValueLong extends Value {
public Value subtract(Value v) throws SQLException {
ValueLong other = (ValueLong) v;
if (SysProperties.OVERFLOW_EXCEPTIONS) {
int sv = value == 0 ? 0 : (value < 0 ? -1 : 1);
int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1);
int sv = Long.signum(value);
int so = Long.signum(other.value);
// if the operands have the same sign, then overflow can not occur
// if the second operand is 0, then overflow can not occur
if (sv == so || so == 0) {
......@@ -152,10 +153,7 @@ public class ValueLong extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueLong v = (ValueLong) o;
if (value == v.value) {
return 0;
}
return value > v.value ? 1 : -1;
return MathUtils.compare(value, v.value);
}
public String getString() {
......
......@@ -11,6 +11,7 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.message.Message;
import org.h2.util.MathUtils;
/**
* Implementation of the SMALLINT data type.
......@@ -50,7 +51,7 @@ public class ValueShort extends Value {
}
public int getSignum() {
return value == 0 ? 0 : (value < 0 ? -1 : 1);
return Integer.signum(value);
}
public Value negate() throws SQLException {
......@@ -98,10 +99,7 @@ public class ValueShort extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueShort v = (ValueShort) o;
if (value == v.value) {
return 0;
}
return value > v.value ? 1 : -1;
return MathUtils.compare(value, v.value);
}
public String getString() {
......
......@@ -63,8 +63,7 @@ public class ValueTime extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueTime v = (ValueTime) o;
int c = value.compareTo(v.value);
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return Integer.signum(value.compareTo(v.value));
}
public String getString() {
......
......@@ -79,8 +79,7 @@ public class ValueTimestamp extends Value {
protected int compareSecure(Value o, CompareMode mode) {
ValueTimestamp v = (ValueTimestamp) o;
int c = value.compareTo(v.value);
return c == 0 ? 0 : (c < 0 ? -1 : 1);
return Integer.signum(value.compareTo(v.value));
}
public String getString() {
......
......@@ -11,6 +11,7 @@ import java.sql.SQLException;
import java.util.UUID;
import org.h2.util.ByteUtils;
import org.h2.util.MathUtils;
import org.h2.util.RandomUtils;
import org.h2.util.StringUtils;
......@@ -147,7 +148,7 @@ public class ValueUuid extends Value {
}
ValueUuid v = (ValueUuid) o;
if (high == v.high) {
return (low == v.low) ? 0 : (low > v.low ? 1 : -1);
return MathUtils.compare(low, v.low);
}
return high > v.high ? 1 : -1;
}
......
......@@ -297,11 +297,9 @@ java org.h2.test.TestAll timer
/*
serialized patches
check if sources.jar is not in installer and zip, but in h2web
documentation: rolling review at history.html
math utils compareTo?
toArray?
mvcc merge problem
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论