diff --git a/build.xml b/build.xml index 44a6c39..7d569d4 100644 --- a/build.xml +++ b/build.xml @@ -165,6 +165,7 @@ + diff --git a/src/java/com/samskivert/depot/impl/FieldMarshaller.java b/src/java/com/samskivert/depot/impl/FieldMarshaller.java index 3c0e390..d1bc779 100644 --- a/src/java/com/samskivert/depot/impl/FieldMarshaller.java +++ b/src/java/com/samskivert/depot/impl/FieldMarshaller.java @@ -23,7 +23,6 @@ package com.samskivert.depot.impl; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.EnumSet; import java.nio.ByteBuffer; import java.sql.Blob; @@ -35,12 +34,13 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; -import com.samskivert.jdbc.ColumnDefinition; import com.samskivert.depot.ByteEnum; import com.samskivert.depot.DatabaseException; import com.samskivert.depot.annotation.Column; import com.samskivert.depot.annotation.Computed; import com.samskivert.depot.annotation.GeneratedValue; +import com.samskivert.depot.util.ByteEnumUtil; +import com.samskivert.jdbc.ColumnDefinition; import com.samskivert.util.StringUtil; @@ -437,7 +437,8 @@ public abstract class FieldMarshaller } } - protected static class ByteEnumMarshaller> extends FieldMarshaller { + protected static class ByteEnumMarshaller & ByteEnum> + extends FieldMarshaller { public ByteEnumMarshaller (Class clazz) { _eclass = clazz; } @@ -447,14 +448,7 @@ public abstract class FieldMarshaller return (ByteEnum) _field.get(po); } @Override public ByteEnum getFromSet (ResultSet rs) throws SQLException { - byte code = rs.getByte(getColumnName()); - for (E value : EnumSet.allOf(_eclass)) { - ByteEnum bvalue = (ByteEnum)value; - if (bvalue.toByte() == code) { - return bvalue; - } - } - return null; + return ByteEnumUtil.fromByte(_eclass, rs.getByte(getColumnName())); } @Override public void writeToObject (Object po, ByteEnum value) throws IllegalArgumentException, IllegalAccessException { diff --git a/src/java/com/samskivert/depot/util/ByteEnumUtil.java b/src/java/com/samskivert/depot/util/ByteEnumUtil.java new file mode 100644 index 0000000..8a6bd5d --- /dev/null +++ b/src/java/com/samskivert/depot/util/ByteEnumUtil.java @@ -0,0 +1,48 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +import java.util.EnumSet; + +import com.samskivert.depot.ByteEnum; + +/** + * {@link ByteEnum} utility methods. + */ +public class ByteEnumUtil +{ + /** + * Returns the enum value with the specified code in the supplied enum class. + * + * @exception IllegalArgumentException thrown if the enum lacks a value that maps to the + * supplied code. + */ + public static & ByteEnum> E fromByte (Class eclass, byte code) + { + for (E value : EnumSet.allOf(eclass)) { + ByteEnum bvalue = (ByteEnum)value; + if (bvalue.toByte() == code) { + return value; + } + } + throw new IllegalArgumentException(eclass + " has no value with code " + code); + } +}