提交 2c903f83 authored 作者: Max Englander's avatar Max Englander

enum-support: in Transfer, use base ValueEnumBase when reading

上级 cf06d6ec
......@@ -495,17 +495,8 @@ public class Transfer {
break;
}
case Value.ENUM: {
ValueEnum ve = (ValueEnum) v;
String[] enumerators = ve.getEnumerators();
writeInt(enumerators.length);
for (String member : enumerators) {
writeString(member);
}
writeInt(ve.getInt());
writeInt(v.getInt());
writeString(v.getString());
break;
}
case Value.RESULT_SET: {
......@@ -609,12 +600,9 @@ public class Transfer {
case Value.FLOAT:
return ValueFloat.get(readFloat());
case Value.ENUM: {
int len = readInt();
String[] enumerators = new String[len];
for (int i = 0; i < len; i++) {
enumerators[i] = readString();
}
return ValueEnum.get(enumerators, readInt());
final int ordinal = readInt();
final String label = readString();
return ValueEnumBase.get(label, ordinal);
}
case Value.INT:
return ValueInt.get(readInt());
......
package org.h2.value;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.util.MathUtils;
import org.h2.value.DataType;
public class ValueEnum extends Value {
public static final int PRECISION = 10;
public static final int DISPLAY_SIZE = 11;
public class ValueEnum extends ValueEnumBase {
private static enum Validation {
DUPLICATE,
EMPTY,
......@@ -20,19 +14,10 @@ public class ValueEnum extends Value {
}
private final String[] enumerators;
private final String label;
private final int ordinal;
private ValueEnum(final String[] enumerators, final int ordinal) {
this.label = enumerators[ordinal];
super(enumerators[ordinal], ordinal);
this.enumerators = enumerators;
this.ordinal = ordinal;
}
@Override
public Value add(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).add(iv);
}
public static final void check(final String[] enumerators) {
......@@ -92,18 +77,6 @@ public class ValueEnum extends Value {
return MathUtils.compareInt(ordinal(), ev.ordinal());
}
@Override
public Value divide(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).divide(iv);
}
@Override
public boolean equals(final Object other) {
return other instanceof ValueEnum &&
ordinal() == ((ValueEnum) other).ordinal();
}
public static ValueEnum get(final String[] enumerators, final String label) {
check(enumerators, label);
......@@ -130,58 +103,13 @@ public class ValueEnum extends Value {
}
}
@Override
public int getDisplaySize() {
return DISPLAY_SIZE;
}
public String[] getEnumerators() {
return enumerators;
}
@Override
public int getInt() {
return ordinal;
}
@Override
public long getLong() {
return ordinal;
}
@Override
public Object getObject() {
return ordinal;
}
@Override
public long getPrecision() {
return PRECISION;
}
@Override
public int getSignum() {
return Integer.signum(ordinal);
}
@Override
public String getSQL() {
return getString();
}
@Override
public String getString() {
return label;
}
@Override
public int getType() {
return Value.ENUM;
}
@Override
public int hashCode() {
return enumerators.hashCode() + ordinal;
return enumerators.hashCode() + ordinal();
}
public static boolean isValid(final String enumerators[], final String label) {
......@@ -196,34 +124,6 @@ public class ValueEnum extends Value {
return validate(enumerators, value).equals(Validation.VALID);
}
protected int ordinal() {
return ordinal;
}
@Override
public Value modulus(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).modulus(iv);
}
@Override
public Value multiply(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).multiply(iv);
}
@Override
public void set(final PreparedStatement prep, final int parameterIndex)
throws SQLException {
prep.setInt(parameterIndex, ordinal);
}
@Override
public Value subtract(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).subtract(iv);
}
private static String toString(final String[] enumerators) {
String result = "(";
for (int i = 0; i < enumerators.length; i++) {
......
package org.h2.value;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.message.DbException;
import org.h2.util.MathUtils;
public class ValueEnumBase extends Value {
public static final int PRECISION = 10;
public static final int DISPLAY_SIZE = 11;
private final String label;
private final int ordinal;
protected ValueEnumBase(final String label, final int ordinal) {
this.label = label;
this.ordinal = ordinal;
}
@Override
public Value add(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).add(iv);
}
@Override
protected int compareSecure(final Value v, final CompareMode mode) {
return MathUtils.compareInt(ordinal(), v.getInt());
}
@Override
public Value divide(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).divide(iv);
}
@Override
public boolean equals(final Object other) {
return other instanceof ValueEnumBase &&
ordinal() == ((ValueEnumBase) other).ordinal() &&
getString() == ((ValueEnumBase) other).getString();
}
public static ValueEnumBase get(final String label, final int ordinal) {
return new ValueEnumBase(label, ordinal);
}
@Override
public int getDisplaySize() {
return DISPLAY_SIZE;
}
@Override
public int getInt() {
return ordinal;
}
@Override
public long getLong() {
return ordinal;
}
@Override
public Object getObject() {
return ordinal;
}
@Override
public long getPrecision() {
return PRECISION;
}
@Override
public int getSignum() {
return Integer.signum(ordinal);
}
@Override
public String getSQL() {
return getString();
}
@Override
public String getString() {
return label;
}
@Override
public int getType() {
return Value.ENUM;
}
@Override
public int hashCode() {
int results = 31;
results += getString().hashCode();
results += ordinal();
return results;
}
@Override
public Value modulus(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).modulus(iv);
}
@Override
public Value multiply(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).multiply(iv);
}
protected int ordinal() {
return ordinal;
}
@Override
public void set(final PreparedStatement prep, final int parameterIndex)
throws SQLException {
prep.setInt(parameterIndex, ordinal);
}
@Override
public Value subtract(final Value v) {
final Value iv = v.convertTo(Value.INT);
return convertTo(Value.INT).subtract(iv);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论