Added support for non-ByteEnum enums, which are stored using their string name.

This commit is contained in:
Michael Bayne
2010-12-01 19:36:01 +00:00
parent fd035fd463
commit eaadb3228a
2 changed files with 45 additions and 1 deletions
@@ -423,6 +423,10 @@ public abstract class FieldMarshaller<T>
@SuppressWarnings("unchecked") Class<Dummy> dtype = (Class<Dummy>)ftype;
return new ByteEnumMarshaller<Dummy>(dtype);
} else if (Enum.class.isAssignableFrom(ftype)) {
@SuppressWarnings("unchecked") Class<Dummy> dtype = (Class<Dummy>)ftype;
return new EnumMarshaller<Dummy>(dtype);
} else {
return null;
}
@@ -728,6 +732,36 @@ public abstract class FieldMarshaller<T>
protected Class<E> _eclass;
}
protected static class EnumMarshaller<E extends Enum<E>> extends FieldMarshaller<E> {
public EnumMarshaller (Class<E> clazz) {
_eclass = clazz;
}
@Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getStringType(length);
}
@Override public E getFromObject (Object po)
throws IllegalArgumentException, IllegalAccessException {
@SuppressWarnings("unchecked") E value = (E) _field.get(po);
return value;
}
@Override public E getFromSet (ResultSet rs) throws SQLException {
String svalue = rs.getString(getColumnName());
return (svalue == null) ? null : Enum.valueOf(_eclass, svalue);
}
@Override public void writeToObject (Object po, E value)
throws IllegalArgumentException, IllegalAccessException {
_field.set(po, value);
}
@Override public void writeToStatement (PreparedStatement ps, int column, E value)
throws SQLException {
String svalue = (value == null) ? null : value.name();
ps.setString(column, svalue);
}
protected Class<E> _eclass;
}
// used to fool the type system when creating ByteEnumMarshallers; look away
protected static enum Dummy implements ByteEnum {
DUMMY;
@@ -57,6 +57,7 @@ public class AllTypesRecord extends PersistentRecord
public static final ColumnExp DATE = colexp(_R, "date");
public static final ColumnExp TIME = colexp(_R, "time");
public static final ColumnExp TIMESTAMP = colexp(_R, "timestamp");
public static final ColumnExp TEST_ENUM = colexp(_R, "testEnum");
public static final ColumnExp NULL_BOXED_BOOLEAN = colexp(_R, "nullBoxedBoolean");
public static final ColumnExp NULL_BOXED_BYTE = colexp(_R, "nullBoxedByte");
public static final ColumnExp NULL_BOXED_SHORT = colexp(_R, "nullBoxedShort");
@@ -70,10 +71,13 @@ public class AllTypesRecord extends PersistentRecord
public static final ColumnExp NULL_DATE = colexp(_R, "nullDate");
public static final ColumnExp NULL_TIME = colexp(_R, "nullTime");
public static final ColumnExp NULL_TIMESTAMP = colexp(_R, "nullTimestamp");
public static final ColumnExp NULL_TEST_ENUM = colexp(_R, "nullTestEnum");
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1;
public enum TestEnum { ONE, TWO, THREE; }
@Id public int recordId;
public boolean booleanValue;
@@ -102,6 +106,8 @@ public class AllTypesRecord extends PersistentRecord
// public Blob blob; // tested by byte[]
// public Clob clob; // not clear how to test this
public TestEnum testEnum;
@Column(nullable=true) public Boolean nullBoxedBoolean;
@Column(nullable=true) public Byte nullBoxedByte;
@Column(nullable=true) public Short nullBoxedShort;
@@ -115,6 +121,7 @@ public class AllTypesRecord extends PersistentRecord
@Column(nullable=true) public Date nullDate;
@Column(nullable=true) public Time nullTime;
@Column(nullable=true) public Timestamp nullTimestamp;
@Column(nullable=true) public TestEnum nullTestEnum;
@Override public boolean equals (Object other) {
if (!(other instanceof AllTypesRecord)) {
@@ -141,6 +148,7 @@ public class AllTypesRecord extends PersistentRecord
date.equals(orec.date) &&
time.equals(orec.time) &&
timestamp.equals(orec.timestamp) &&
testEnum.equals(orec.testEnum) &&
(nullBoxedBoolean == orec.nullBoxedBoolean) &&
(nullBoxedByte == orec.nullBoxedByte) &&
(nullBoxedShort == orec.nullBoxedShort) &&
@@ -153,7 +161,8 @@ public class AllTypesRecord extends PersistentRecord
(nullString == orec.nullString) &&
(nullDate == orec.nullDate) &&
(nullTime == orec.nullTime) &&
(nullTimestamp == orec.nullTimestamp);
(nullTimestamp == orec.nullTimestamp) &&
(nullTestEnum == orec.nullTestEnum);
}
public static AllTypesRecord createRecord (int recordId)
@@ -180,6 +189,7 @@ public class AllTypesRecord extends PersistentRecord
rec.date = Date.valueOf("2010-01-18");
rec.time = Time.valueOf("19:19:19");
rec.timestamp = new Timestamp(System.currentTimeMillis()+20);
rec.testEnum = TestEnum.TWO;
return rec;
}