提交 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 { ...@@ -495,17 +495,8 @@ public class Transfer {
break; break;
} }
case Value.ENUM: { case Value.ENUM: {
ValueEnum ve = (ValueEnum) v; writeInt(v.getInt());
String[] enumerators = ve.getEnumerators(); writeString(v.getString());
writeInt(enumerators.length);
for (String member : enumerators) {
writeString(member);
}
writeInt(ve.getInt());
break; break;
} }
case Value.RESULT_SET: { case Value.RESULT_SET: {
...@@ -609,12 +600,9 @@ public class Transfer { ...@@ -609,12 +600,9 @@ public class Transfer {
case Value.FLOAT: case Value.FLOAT:
return ValueFloat.get(readFloat()); return ValueFloat.get(readFloat());
case Value.ENUM: { case Value.ENUM: {
int len = readInt(); final int ordinal = readInt();
String[] enumerators = new String[len]; final String label = readString();
for (int i = 0; i < len; i++) { return ValueEnumBase.get(label, ordinal);
enumerators[i] = readString();
}
return ValueEnum.get(enumerators, readInt());
} }
case Value.INT: case Value.INT:
return ValueInt.get(readInt()); return ValueInt.get(readInt());
......
package org.h2.value; package org.h2.value;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
public class ValueEnum extends Value { public class ValueEnum extends ValueEnumBase {
public static final int PRECISION = 10;
public static final int DISPLAY_SIZE = 11;
private static enum Validation { private static enum Validation {
DUPLICATE, DUPLICATE,
EMPTY, EMPTY,
...@@ -20,19 +14,10 @@ public class ValueEnum extends Value { ...@@ -20,19 +14,10 @@ public class ValueEnum extends Value {
} }
private final String[] enumerators; private final String[] enumerators;
private final String label;
private final int ordinal;
private ValueEnum(final String[] enumerators, final int ordinal) { private ValueEnum(final String[] enumerators, final int ordinal) {
this.label = enumerators[ordinal]; super(enumerators[ordinal], ordinal);
this.enumerators = enumerators; 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) { public static final void check(final String[] enumerators) {
...@@ -92,18 +77,6 @@ public class ValueEnum extends Value { ...@@ -92,18 +77,6 @@ public class ValueEnum extends Value {
return MathUtils.compareInt(ordinal(), ev.ordinal()); 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) { public static ValueEnum get(final String[] enumerators, final String label) {
check(enumerators, label); check(enumerators, label);
...@@ -130,58 +103,13 @@ public class ValueEnum extends Value { ...@@ -130,58 +103,13 @@ public class ValueEnum extends Value {
} }
} }
@Override
public int getDisplaySize() {
return DISPLAY_SIZE;
}
public String[] getEnumerators() { public String[] getEnumerators() {
return enumerators; 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 @Override
public int hashCode() { public int hashCode() {
return enumerators.hashCode() + ordinal; return enumerators.hashCode() + ordinal();
} }
public static boolean isValid(final String enumerators[], final String label) { public static boolean isValid(final String enumerators[], final String label) {
...@@ -196,34 +124,6 @@ public class ValueEnum extends Value { ...@@ -196,34 +124,6 @@ public class ValueEnum extends Value {
return validate(enumerators, value).equals(Validation.VALID); 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) { private static String toString(final String[] enumerators) {
String result = "("; String result = "(";
for (int i = 0; i < enumerators.length; i++) { 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论