Added ByteEnumUtil for all your fromByte() needs, included it in the GWT jar in

case you need to do it in the land of gwit.
This commit is contained in:
Michael Bayne
2009-07-10 20:37:57 +00:00
parent 9469575ad7
commit 9fd87441ac
3 changed files with 54 additions and 11 deletions
+1
View File
@@ -165,6 +165,7 @@
<copy todir="${gwtjar.dir}"> <copy todir="${gwtjar.dir}">
<fileset dir="src/java" includes="com/samskivert/Depot.gwt.xml"/> <fileset dir="src/java" includes="com/samskivert/Depot.gwt.xml"/>
<fileset dir="src/java" includes="com/samskivert/depot/ByteEnum.java"/> <fileset dir="src/java" includes="com/samskivert/depot/ByteEnum.java"/>
<fileset dir="src/java" includes="com/samskivert/depot/util/ByteEnumUtil.java"/>
</copy> </copy>
<jar basedir="${gwtjar.dir}" destfile="${deploy.dir}/${lib.name}-gwt.jar"/> <jar basedir="${gwtjar.dir}" destfile="${deploy.dir}/${lib.name}-gwt.jar"/>
<delete dir="${gwtjar.dir}"/> <delete dir="${gwtjar.dir}"/>
@@ -23,7 +23,6 @@ package com.samskivert.depot.impl;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.EnumSet;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.sql.Blob; import java.sql.Blob;
@@ -35,12 +34,13 @@ import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import com.samskivert.jdbc.ColumnDefinition;
import com.samskivert.depot.ByteEnum; import com.samskivert.depot.ByteEnum;
import com.samskivert.depot.DatabaseException; import com.samskivert.depot.DatabaseException;
import com.samskivert.depot.annotation.Column; import com.samskivert.depot.annotation.Column;
import com.samskivert.depot.annotation.Computed; import com.samskivert.depot.annotation.Computed;
import com.samskivert.depot.annotation.GeneratedValue; import com.samskivert.depot.annotation.GeneratedValue;
import com.samskivert.depot.util.ByteEnumUtil;
import com.samskivert.jdbc.ColumnDefinition;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
@@ -437,7 +437,8 @@ public abstract class FieldMarshaller<T>
} }
} }
protected static class ByteEnumMarshaller<E extends Enum<E>> extends FieldMarshaller<ByteEnum> { protected static class ByteEnumMarshaller<E extends Enum<E> & ByteEnum>
extends FieldMarshaller<ByteEnum> {
public ByteEnumMarshaller (Class<E> clazz) { public ByteEnumMarshaller (Class<E> clazz) {
_eclass = clazz; _eclass = clazz;
} }
@@ -447,14 +448,7 @@ public abstract class FieldMarshaller<T>
return (ByteEnum) _field.get(po); return (ByteEnum) _field.get(po);
} }
@Override public ByteEnum getFromSet (ResultSet rs) throws SQLException { @Override public ByteEnum getFromSet (ResultSet rs) throws SQLException {
byte code = rs.getByte(getColumnName()); return ByteEnumUtil.fromByte(_eclass, rs.getByte(getColumnName()));
for (E value : EnumSet.allOf(_eclass)) {
ByteEnum bvalue = (ByteEnum)value;
if (bvalue.toByte() == code) {
return bvalue;
}
}
return null;
} }
@Override public void writeToObject (Object po, ByteEnum value) @Override public void writeToObject (Object po, ByteEnum value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
@@ -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 <E extends Enum<E> & ByteEnum> E fromByte (Class<E> 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);
}
}